rubanok 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/CHANGELOG.md +4 -0
- data/README.md +55 -7
- data/gemfiles/rails6.gemfile +6 -0
- data/lib/rubanok/dsl/matching.rb +1 -1
- data/lib/rubanok/railtie.rb +6 -0
- data/lib/rubanok/version.rb +1 -1
- data/rubanok.gemspec +1 -1
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76b1edc5c67f7a783dc3db022fee6aba015af2745a877d0b05089ce2c2ca2df7
|
4
|
+
data.tar.gz: d3e055c5453c793e17d0c6efea3b0bd6bcb3c8b16ada1461544b28431058a3a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab19e7909a899d1831f246164888e76c29c1dfd6f9d3864a801b69fd3caba8cc754402a3e3e11ae6a0185bb87cec76711dadcfd8762e32eb4923cf24d3e16bae
|
7
|
+
data.tar.gz: 91c17c4695b0ccc7ff6ab0b75cace311c61b9ca3edf29a43bbd2c747dbb5995bc6f2af45ca6966117dc45304c8cf1ca2f58401bd7329d966a180d8c2942b24dc
|
data/.travis.yml
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
|
+
cache: bundler
|
3
4
|
rvm:
|
4
5
|
- 2.6.0
|
5
6
|
|
@@ -15,6 +16,8 @@ matrix:
|
|
15
16
|
include:
|
16
17
|
- rvm: ruby-head
|
17
18
|
gemfile: gemfiles/railsmaster.gemfile
|
19
|
+
- rvm: 2.6.1
|
20
|
+
gemfile: gemfiles/rails6.gemfile
|
18
21
|
- rvm: 2.6.0
|
19
22
|
gemfile: gemfiles/rails52.gemfile
|
20
23
|
- rvm: 2.5.1
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,8 @@
|
|
4
4
|
|
5
5
|
Rubanok provides a DSL to build parameters-based data transformers.
|
6
6
|
|
7
|
+
📖 Read the introduction post: ["Carve your controllers like Papa Carlo"](https://dev.to/evilmartians/carve-your-controllers-like-papa-carlo-32m6)
|
8
|
+
|
7
9
|
The typical usage is to describe all the possible collection manipulation for REST `index` action, e.g. filtering, sorting, searching, pagination, etc..
|
8
10
|
|
9
11
|
So, instead of:
|
@@ -58,11 +60,17 @@ Requirements:
|
|
58
60
|
|
59
61
|
## Installation
|
60
62
|
|
61
|
-
|
63
|
+
Add to your `Gemfile`:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
gem "rubanok"
|
67
|
+
```
|
68
|
+
|
69
|
+
And run `bundle install`.
|
62
70
|
|
63
71
|
## Usage
|
64
72
|
|
65
|
-
The core concept of this library is a _plane_ (or _hand plane_, or "рубанок" in Russian). Plane is responsible for mapping parameters to
|
73
|
+
The core concept of this library is a _plane_ (or _hand plane_, or "рубанок" in Russian). Plane is responsible for mapping parameters to transformations.
|
66
74
|
|
67
75
|
From the example above:
|
68
76
|
|
@@ -127,13 +135,13 @@ class CourseSessionsPlane < Rubanok::Plane
|
|
127
135
|
end
|
128
136
|
```
|
129
137
|
|
130
|
-
**NOTE:**
|
138
|
+
**NOTE:** Rubanok only matches exact values; more complex matching could be added in the future.
|
131
139
|
|
132
140
|
### Rule activation
|
133
141
|
|
134
142
|
Rubanok _activates_ a rule by checking whether the corresponding keys are present in the params object. All the fields must be present to apply the rule.
|
135
143
|
|
136
|
-
|
144
|
+
Some fields may be optional, or perhaps even all of them. You can use `activate_on` and `activate_always` options to mark something as an optional key instead of a required one:
|
137
145
|
|
138
146
|
```ruby
|
139
147
|
# Always apply the rule; use default values for keyword args
|
@@ -147,13 +155,13 @@ match :sort_by, :sort, activate_on: :sort_by do
|
|
147
155
|
end
|
148
156
|
```
|
149
157
|
|
150
|
-
By default, Rubanok ignores empty param values (using `#empty?` under the hood) and
|
158
|
+
By default, Rubanok ignores empty param values (using `#empty?` under the hood) and will not run matching rules on those values. For example: `{ q: "" }` and `{ q: nil }` won't activate the `map :q` rule.
|
151
159
|
|
152
160
|
You can change this behaviour by setting: `Rubanok.ignore_empty_values = false`.
|
153
161
|
|
154
162
|
### Testing
|
155
163
|
|
156
|
-
One of the benefits of having
|
164
|
+
One of the benefits of having modification logic contained in its own class is the ability to test modifications in isolation:
|
157
165
|
|
158
166
|
```ruby
|
159
167
|
# For example, with RSpec
|
@@ -194,10 +202,50 @@ require "rubanok/rspec"
|
|
194
202
|
|
195
203
|
### Rails vs. non-Rails
|
196
204
|
|
197
|
-
Rubanok
|
205
|
+
Rubanok does not require Rails, but it has some useful Rails extensions such as `planish` helper for controllers (included automatically into `ActionController::Base` and `ActionController::API`).
|
198
206
|
|
199
207
|
If you use `ActionController::Metal` you must include the `Rubanok::Controller` module yourself.
|
200
208
|
|
209
|
+
## Questions & Answers
|
210
|
+
|
211
|
+
- **🧐"Planish"? Is there a word?**
|
212
|
+
|
213
|
+
Yes, [it is](https://en.wiktionary.org/wiki/planish).
|
214
|
+
|
215
|
+
- **Where to put my _plane_ classes?**
|
216
|
+
|
217
|
+
I put mine under `app/planes` (as `<resources>_plane.rb`) in my Rails app.
|
218
|
+
|
219
|
+
- **I don't like the naming ("planes" ✈️?), can I still use the library?**
|
220
|
+
|
221
|
+
First, feel free to [propose your variant](https://github.com/palkan/rubanok/issues). We would be glad to discuss it.
|
222
|
+
|
223
|
+
Secondly, you can easily avoid it by adding a few lines to your `ApplicationController`:
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
class ApplicationController < ActionController::Smth
|
227
|
+
# add `planish` alias
|
228
|
+
alias transform_scope planish
|
229
|
+
|
230
|
+
# override the `implicit_plane_class` method
|
231
|
+
def implicit_plane_class
|
232
|
+
"#{controller_path.classify.pluralize}Scoper".safe_constantize
|
233
|
+
end
|
234
|
+
end
|
235
|
+
```
|
236
|
+
|
237
|
+
Now you can use it like this:
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
class CourseSessionsController < ApplicationController
|
241
|
+
def index
|
242
|
+
@sessions = transform_scope(CourseSession.all, params)
|
243
|
+
# which equals to
|
244
|
+
@sessions = CourseSessionsScoper.call(CourseSession.all, params.to_unsafe_h)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
```
|
248
|
+
|
201
249
|
## Contributing
|
202
250
|
|
203
251
|
Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/rubanok.
|
data/lib/rubanok/dsl/matching.rb
CHANGED
data/lib/rubanok/railtie.rb
CHANGED
@@ -8,6 +8,12 @@ module Rubanok # :nodoc:
|
|
8
8
|
|
9
9
|
ActionController::Base.include Rubanok::Controller
|
10
10
|
end
|
11
|
+
|
12
|
+
ActiveSupport.on_load(:action_controller_api) do
|
13
|
+
require "rubanok/rails/controller"
|
14
|
+
|
15
|
+
ActionController::API.include Rubanok::Controller
|
16
|
+
end
|
11
17
|
end
|
12
18
|
end
|
13
19
|
end
|
data/lib/rubanok/version.rb
CHANGED
data/rubanok.gemspec
CHANGED
@@ -31,5 +31,5 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.add_development_dependency "rspec", "~> 3.0"
|
32
32
|
spec.add_development_dependency "rspec-rails"
|
33
33
|
spec.add_development_dependency "rubocop-rspec"
|
34
|
-
spec.add_development_dependency "standard"
|
34
|
+
spec.add_development_dependency "standard", "~> 0.0.36"
|
35
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubanok
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Dementyev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -112,16 +112,16 @@ dependencies:
|
|
112
112
|
name: standard
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- - "
|
115
|
+
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
117
|
+
version: 0.0.36
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- - "
|
122
|
+
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
124
|
+
version: 0.0.36
|
125
125
|
description: Parameters-based transformation DSL
|
126
126
|
email:
|
127
127
|
- dementiev.vm@gmail.com
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- bin/setup
|
144
144
|
- gemfiles/rails42.gemfile
|
145
145
|
- gemfiles/rails52.gemfile
|
146
|
+
- gemfiles/rails6.gemfile
|
146
147
|
- gemfiles/railsmaster.gemfile
|
147
148
|
- lib/rubanok.rb
|
148
149
|
- lib/rubanok/dsl/mapping.rb
|
@@ -174,8 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
175
|
- !ruby/object:Gem::Version
|
175
176
|
version: '0'
|
176
177
|
requirements: []
|
177
|
-
|
178
|
-
rubygems_version: 2.7.6
|
178
|
+
rubygems_version: 3.0.2
|
179
179
|
signing_key:
|
180
180
|
specification_version: 4
|
181
181
|
summary: Parameters-based transformation DSL
|