Skip to content

Commit f17edb8

Browse files
authored
Merge pull request #8 from palkan/chore/rename-plane-processor
Plane -> processor
2 parents 0fff87d + e906dfb commit f17edb8

11 files changed

+133
-83
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## master
44

5+
- Improve naming by using "processor" instead of "plane". ([@palkan][])
6+
7+
See [the discussion](https://github.com/palkan/rubanok/issues/3).
8+
9+
**NOTE**: Older API is still available without deprecation.
10+
511
- Add `fail_when_no_matches` parameter to `match` method. ([@Earendil95][])
612

713
## 0.1.3 (2019-03-05)

README.md

+35-36
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,30 @@ You have:
2828
```ruby
2929
class CourseSessionController < ApplicationController
3030
def index
31-
@sessions = planish(
31+
@sessions = rubanok_process(
3232
# pass input
3333
CourseSession.all,
3434
# pass params
3535
params,
36-
# provide a plane to use
37-
with: CourseSessionsPlane
36+
# provide a processor to use
37+
with: CourseSessionsProcessor
3838
)
3939
end
4040
end
4141
```
4242

4343
Or we can try to infer all the configuration for you:
4444

45-
4645
```ruby
4746
class CourseSessionController < ApplicationController
4847
def index
49-
@sessions = planish(CourseSession.all)
48+
@sessions = rubanok_process(CourseSession.all)
5049
end
5150
end
5251
```
5352

5453
Requirements:
54+
5555
- Ruby ~> 2.5
5656
- Rails >= 4.2 (only for using with Rails)
5757

@@ -70,12 +70,12 @@ And run `bundle install`.
7070

7171
## Usage
7272

73-
The core concept of this library is a _plane_ (or _hand plane_, or "рубанок" in Russian). Plane is responsible for mapping parameters to transformations.
73+
The core concept of this library is a processor (previously called _plane_ or _hand plane_, or "рубанок" in Russian). Processor is responsible for mapping parameters to transformations.
7474

7575
From the example above:
7676

7777
```ruby
78-
class CourseSessionsPlane < Rubanok::Plane
78+
class CourseSessionsProcessor < Rubanok::Processor
7979
# You can map keys
8080
map :q do |q:|
8181
# `raw` is an accessor for input data
@@ -84,7 +84,7 @@ class CourseSessionsPlane < Rubanok::Plane
8484
end
8585

8686
# The following code
87-
CourseSessionsPlane.call(CourseSession.all, q: "xyz")
87+
CourseSessionsProcessor.call(CourseSession.all, q: "xyz")
8888

8989
# is equal to
9090
CourseSession.all.search("xyz")
@@ -93,7 +93,7 @@ CourseSession.all.search("xyz")
9393
You can map multiple keys at once:
9494

9595
```ruby
96-
class CourseSessionsPlane < Rubanok::Plane
96+
class CourseSessionsProcessor < Rubanok::Processor
9797
DEFAULT_PAGE_SIZE = 25
9898

9999
map :page, :per_page do |page:, per_page: DEFAULT_PAGE_SIZE|
@@ -105,7 +105,7 @@ end
105105
There is also `match` method to handle values:
106106

107107
```ruby
108-
class CourseSessionsPlane < Rubanok::Plane
108+
class CourseSessionsProcessor < Rubanok::Processor
109109
SORT_ORDERS = %w(asc desc).freeze
110110
SORTABLE_FIELDS = %w(id name created_at).freeze
111111

@@ -133,7 +133,7 @@ class CourseSessionsPlane < Rubanok::Plane
133133
end
134134
end
135135

136-
# strict matching; if Plane will not match parameter, it will raise Rubanok::UnexpectedInputError
136+
# strict matching; if Processor will not match parameter, it will raise Rubanok::UnexpectedInputError
137137
# You can handle it in controller, for example, with sending 422 Unprocessable Entity to client
138138
match :filter, fail_when_no_matches: true do
139139
having "active" do
@@ -148,7 +148,7 @@ end
148148
```
149149

150150
By default, Rubanok will not fail if no matches found in `match` rule. You can change it by setting: `Rubanok.fail_when_no_matches = true`.
151-
If in example above you will call `CourseSessionsPlane.call(CourseSession, filter: 'acitve')`, you will get `Rubanok::UnexpectedInputError: Unexpected input: {:filter=>'acitve'}`.
151+
If in example above you will call `CourseSessionsProcessor.call(CourseSession, filter: 'acitve')`, you will get `Rubanok::UnexpectedInputError: Unexpected input: {:filter=>'acitve'}`.
152152

153153
**NOTE:** Rubanok only matches exact values; more complex matching could be added in the future.
154154

@@ -180,7 +180,7 @@ One of the benefits of having modification logic contained in its own class is t
180180

181181
```ruby
182182
# For example, with RSpec
183-
RSpec.describe CourseSessionsPlane do
183+
RSpec.describe CourseSessionsProcessor do
184184
let(:input ) { CourseSession.all }
185185
let(:params) { {} }
186186

@@ -201,49 +201,36 @@ RSpec.describe CourseSessionController do
201201
subject { get :index }
202202

203203
specify do
204-
expect { subject }.to have_planished(CourseSession.all).
205-
with(CourseSessionsPlane)
204+
expect { subject }.to have_rubanok_processed(CourseSession.all).
205+
with(CourseSessionsProcessor)
206206
end
207207
end
208208
```
209209

210210
**NOTE**: input matching only checks for the class equality.
211211

212-
To use `have_planished` matcher you must add the following line to your `spec_helper.rb` / `rails_helper.rb` (it's added automatically if RSpec defined and `RAILS_ENV`/`RACK_ENV` is equal to `"test"`):
212+
To use `have_rubanok_processed` matcher you must add the following line to your `spec_helper.rb` / `rails_helper.rb` (it's added automatically if RSpec defined and `RAILS_ENV`/`RACK_ENV` is equal to `"test"`):
213213

214214
```ruby
215215
require "rubanok/rspec"
216216
```
217217

218218
### Rails vs. non-Rails
219219

220-
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`).
220+
Rubanok does not require Rails, but it has some useful Rails extensions such as `rubanok_process` helper for controllers (included automatically into `ActionController::Base` and `ActionController::API`).
221221

222222
If you use `ActionController::Metal` you must include the `Rubanok::Controller` module yourself.
223223

224-
## Questions & Answers
225-
226-
- **🧐"Planish"? Is there a word?**
227-
228-
Yes, [it is](https://en.wiktionary.org/wiki/planish).
229-
230-
- **Where to put my _plane_ classes?**
231-
232-
I put mine under `app/planes` (as `<resources>_plane.rb`) in my Rails app.
233-
234-
- **I don't like the naming ("planes" ✈️?), can I still use the library?**
224+
### Processor class inference in Rails controllers
235225

236-
First, feel free to [propose your variant](https://github.com/palkan/rubanok/issues). We would be glad to discuss it.
226+
By default, `rubanok_process` uses the following algorithm to define a processor class: `"#{controller_path.classify.pluralize}Processor".safe_constantize`.
237227

238-
Secondly, you can easily avoid it by adding a few lines to your `ApplicationController`:
228+
You can change this by overriding the `#implicit_rubanok_class` method:
239229

240230
```ruby
241231
class ApplicationController < ActionController::Smth
242-
# add `planish` alias
243-
alias transform_scope planish
244-
245-
# override the `implicit_plane_class` method
246-
def implicit_plane_class
232+
# override the `implicit_rubanok_class` method
233+
def implicit_rubanok_class
247234
"#{controller_path.classify.pluralize}Scoper".safe_constantize
248235
end
249236
end
@@ -254,13 +241,25 @@ Now you can use it like this:
254241
```ruby
255242
class CourseSessionsController < ApplicationController
256243
def index
257-
@sessions = transform_scope(CourseSession.all, params)
244+
@sessions = rubanok_process(CourseSession.all, params)
258245
# which equals to
259246
@sessions = CourseSessionsScoper.call(CourseSession.all, params.to_unsafe_h)
260247
end
261248
end
262249
```
263250

251+
**NOTE:** the `planish` method is still available and it uses `#{controller_path.classify.pluralize}Plane".safe_constantize` under the hood (via the `#implicit_plane_class` method).
252+
253+
## Questions & Answers
254+
255+
- **Where to put my processor/plane classes?**
256+
257+
I put mine under `app/planes` (as `<resources>_plane.rb`) in my Rails app.
258+
259+
- **I don't like the naming ("planes" ✈️?), can I still use the library?**
260+
261+
Good news—the default naming has been changed. "Planes" are still available if you prefer them (just like me 😉).
262+
264263
## Contributing
265264

266265
Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/rubanok.

lib/rubanok.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require "rubanok/version"
4-
require "rubanok/plane"
4+
require "rubanok/processor"
55

66
require "rubanok/railtie" if defined?(Rails)
77

@@ -13,15 +13,15 @@
1313
#
1414
# Example:
1515
#
16-
# class CourseSessionPlane < Rubanok::Plane
16+
# class CourseSessionProcessor < Rubanok::Processor
1717
# map :q do |q:|
1818
# raw.searh(q)
1919
# end
2020
# end
2121
#
2222
# class CourseSessionController < ApplicationController
2323
# def index
24-
# @sessions = planish(CourseSession.all)
24+
# @sessions = rubanok_process(CourseSession.all)
2525
# end
2626
# end
2727
module Rubanok

lib/rubanok/dsl/mapping.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Rubanok
44
module DSL
5-
# Adds `.map` method to Plane to define key-matching rules:
5+
# Adds `.map` method to Processor to define key-matching rules:
66
#
77
# map :q do |q:|
88
# # this rule is activated iff "q" (or :q) param is present

lib/rubanok/dsl/matching.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Rubanok
44
class UnexpectedInputError < StandardError; end
55

66
module DSL
7-
# Adds `.match` method to Plane class to define key-value-matching rules:
7+
# Adds `.match` method to Processor class to define key-value-matching rules:
88
#
99
# match :sort, :sort_by do |sort:, sort_by:|
1010
# # this rule is activated iff both "sort" and "sort_by" params are present

lib/rubanok/plane.rb lib/rubanok/processor.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@
99
using Rubanok::SymbolizeKeys
1010

1111
module Rubanok
12-
# Base class for transformers (_planes_)
12+
# Base class for processors (_planes_)
1313
#
1414
# Define transformation rules via `map` and `match` methods
15-
# and apply them by calling the plane:
15+
# and apply them by calling the processor:
1616
#
17-
# class MyPlane < Rubanok::Plane
17+
# class MyTransformer < Rubanok::Processor
1818
# map :type do
1919
# raw.where(type: type)
2020
# end
2121
# end
2222
#
23-
# MyPlane.call(MyModel.all, {type: "public"})
23+
# MyTransformer.call(MyModel.all, {type: "public"})
2424
#
2525
# NOTE: the second argument (`params`) MUST be a Hash. Keys could be either Symbols
2626
# or Strings (we automatically transform strings to symbols while matching rules).
2727
#
2828
# All transformation methods are called within the context of the instance of
29-
# a plane class.
29+
# a processor class.
3030
#
3131
# You can access the input data via `raw` method.
32-
class Plane
32+
class Processor
3333
include DSL::Matching
3434
include DSL::Mapping
3535

@@ -46,7 +46,7 @@ def rules
4646
return @rules if instance_variable_defined?(:@rules)
4747

4848
@rules =
49-
if superclass <= Plane
49+
if superclass <= Processor
5050
superclass.rules.dup
5151
else
5252
[]
@@ -89,4 +89,6 @@ def rules
8989
self.class.rules
9090
end
9191
end
92+
93+
Plane = Processor
9294
end

lib/rubanok/rails/controller.rb

+18-10
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44

55
module Rubanok
66
# Controller concern.
7-
# Adds `planish` method.
7+
# Adds `rubanok_process` method.
88
module Controller
99
extend ActiveSupport::Concern
1010

11-
# Planish passed data (e.g. ActiveRecord relation) using
12-
# the corresponding Plane class.
11+
# This method passed data (e.g. ActiveRecord relation) using
12+
# the corresponding Processor class.
1313
#
14-
# Plane is inferred from controller name, e.g.
15-
# "PostsController -> PostPlane".
14+
# Processor is inferred from controller name, e.g.
15+
# "PostsController -> PostProcessor".
1616
#
17-
# You can specify the Plane class explicitly via `with` option.
17+
# You can specify the Processor class explicitly via `with` option.
1818
#
1919
# By default, `params` object is passed as parameters, but you
2020
# can specify the params via `params` option.
21-
def planish(data, plane_params = nil, with: implicit_plane_class)
21+
def rubanok_process(data, plane_params = nil, with: implicit_rubanok_class)
2222
if with.nil?
23-
raise ArgumentError, "Failed to find a plane class for #{self.class.name}. " \
24-
"Please, specify the plane class explicitly via `with` option"
23+
raise ArgumentError, "Failed to find a processor class for #{self.class.name}. " \
24+
"Please, specify the processor class explicitly via `with` option"
2525
end
2626

2727
plane_params ||= params
@@ -30,7 +30,15 @@ def planish(data, plane_params = nil, with: implicit_plane_class)
3030
with.call(data, plane_params)
3131
end
3232

33-
# Tries to infer the plane class from controller path
33+
def planish(*args, with: implicit_plane_class)
34+
rubanok_process(*args, with: with)
35+
end
36+
37+
# Tries to infer the rubanok processor class from controller path
38+
def implicit_rubanok_class
39+
"#{controller_path.classify.pluralize}Processor".safe_constantize
40+
end
41+
3442
def implicit_plane_class
3543
"#{controller_path.classify.pluralize}Plane".safe_constantize
3644
end

0 commit comments

Comments
 (0)