rack-reducer 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0be5dfa4acf3c9a2c7755c67f2bbc15a2126b5d3
4
- data.tar.gz: 6bd09c026cd5e5b9fb1d8ac0425d2e525afb3a36
2
+ SHA256:
3
+ metadata.gz: d400c7b68a3ecf7da56af02f7f5e86ab18177ff306d6871a10ef8f9e7bcb7109
4
+ data.tar.gz: ff1b1f50ccb165d041a04fced63adbd946d8976f06460ce83c30b66c91599c17
5
5
  SHA512:
6
- metadata.gz: e03f7c4766706254c287c7dbb0b664cf29d8c3b7d1e210189d4c17a9cd3a013c579d4e5ee2d0cda56cbf146e639d7515aff3092cb6832621e058375316a2d692
7
- data.tar.gz: 15c82a76f5a7218393656ddb4abfaa5584bc11b483326452d08ff16a4a9c3efdecbd07ed810af51401a03b07a95dadd89b76199dfc4564afb04c1f4db9a7b2ec
6
+ metadata.gz: 0dadfa90dce4dfd009c5dab96bc6c32557863a8413cf7888a10fe0f5af9d1479c2a93c29b79ea501abaa89dafa0de1daf95ac2665bae2534884d3c9a8cd4bf17
7
+ data.tar.gz: da77f4e36e9a979be6a52d37a996f10636935dc9097dc47dc22cb972d5e59a5634b327dcae60d7c9573a4e62b6b9fa789dbd0511459724a15103a619b49ccc63
data/README.md CHANGED
@@ -39,7 +39,8 @@ Rack::Reducer can help. It maps incoming URL params to an array of filter
39
39
  functions you define, applies only the applicable filters, and returns your
40
40
  filtered data.
41
41
 
42
- You can use Rack::Reducer in your choice of two styles: mixin or functional.
42
+ You can use Rack::Reducer in your choice of two styles: **mixin** or
43
+ **functional**.
43
44
 
44
45
  ### Mixin style
45
46
  Call `Model.reduce(params)` in your controllers...
@@ -151,8 +152,8 @@ class SinatraFunctionalApp < Sinatra::Base
151
152
  }
152
153
 
153
154
  get '/artists' do
154
- @artists = Rack::Reducer.call(params, QUERY).to_a
155
- @artists.to_json
155
+ @artists = Rack::Reducer.call(params, QUERY)
156
+ @artists.to_a.to_json
156
157
  end
157
158
  end
158
159
  ```
data/lib/rack/reducer.rb CHANGED
@@ -4,7 +4,21 @@ require_relative 'reducer/middleware'
4
4
  module Rack
5
5
  # Use request params to apply filters to a dataset
6
6
  module Reducer
7
- # Call Rack::Reducer as a function
7
+ # Filter a dataset
8
+ # @param params [Hash] Rack-compatible URL params
9
+ # @param dataset [Object] A dataset, e.g. one of your App's models
10
+ # @param filters [Array<Proc>] An array of lambdas with keyword arguments
11
+ # @example Call Rack::Reducer as a function in a Sinatra app
12
+ # ArtistReducer = {
13
+ # dataset: Artist,
14
+ # filters: [
15
+ # lambda { |name:| where(name: name) },
16
+ # lambda { |genre:| where(genre: genre) },
17
+ # ]
18
+ # }
19
+ # get '/artists' do
20
+ # @artists = Rack::Reducer.call(params, ArtistReducer)
21
+ # end
8
22
  def self.call(params, dataset:, filters:)
9
23
  Reduction.new(
10
24
  params: params,
@@ -18,26 +32,25 @@ module Rack
18
32
  Middleware.new(app, options)
19
33
  end
20
34
 
21
- # Extend Rack::Reducer to get `reduce` and `reduces` as class-methods
35
+ # Extend Rack::Reducer to get +reduce+ and +reduces+ as class-methods
36
+ # @example Make an "Artists" model reducible
37
+ # class Artist < SomeORM::Model
38
+ # extend Rack::Reducer
39
+ # reduces self.all, filters: [
40
+ # lambda { |name:| where(name: name) },
41
+ # lambda { |genre:| where(genre: genre) },
42
+ # ]
43
+ # end
22
44
  #
23
- # class Artist < SomeORM::Model
24
- # extend Rack::Reducer
25
- # reduces self.all, filters: [
26
- # lambda { |name:| where(name: name) },
27
- # lambda { |genre:| where(genre: genre) },
28
- # ]
29
- # end
30
- def reduce(params)
31
- Reduction.new(
32
- params: params,
33
- filters: @rack_reducer_filters,
34
- dataset: @rack_reducer_dataset
35
- ).reduce
36
- end
37
-
45
+ # Artist.reduce(params)
38
46
  def reduces(dataset, filters:)
39
- @rack_reducer_dataset = dataset
40
- @rack_reducer_filters = filters
47
+ define_singleton_method :reduce do |params|
48
+ Reduction.new(
49
+ params: params,
50
+ filters: filters,
51
+ dataset: dataset,
52
+ ).reduce
53
+ end
41
54
  end
42
55
  end
43
56
  end
@@ -1,6 +1,6 @@
1
1
  module Rack
2
2
  module Reducer
3
- # convert params from Sinatra, Rails, Roda, etc into a symbol hash
3
+ # Convert params from Sinatra, Rails, Roda, etc into a symbol hash.
4
4
  module Parser
5
5
  def self.call(data)
6
6
  data.is_a?(Hash) ? symbolize(data) : hashify(data)
@@ -12,13 +12,15 @@ module Rack
12
12
  end
13
13
  end
14
14
 
15
- # turns out a Rails params hash is not really a hash
16
- # it's safe to call .to_unsafe_hash here, because params
17
- # are automatically sanitized by the lambda keywords
15
+ # Turns out a Rails params hash is not really a hash.
16
+ # It's safe to call .to_unsafe_hash here, because params
17
+ # are automatically sanitized by the lambda keywords.
18
18
  def self.hashify(data)
19
19
  fn = %i[to_unsafe_h to_h].find { |name| data.respond_to?(name) }
20
20
  symbolize(data.send(fn))
21
21
  end
22
22
  end
23
+
24
+ private_constant :Parser
23
25
  end
24
26
  end
@@ -20,16 +20,14 @@ module Rack
20
20
  end
21
21
 
22
22
  def reduce
23
- @props[:filters].reduce(@props[:dataset], &method(:apply_filter))
24
- end
25
-
26
- private
23
+ @props[:filters].reduce(@props[:dataset]) do |data, filter|
24
+ next data unless filter.satisfies?(@params)
27
25
 
28
- def apply_filter(data, filter)
29
- return data unless filter.satisfies?(@params)
30
-
31
- data.instance_exec(@params.slice(*filter.all_argument_names), &filter)
26
+ data.instance_exec(@params.slice(*filter.all_argument_names), &filter)
27
+ end
32
28
  end
33
29
  end
30
+
31
+ private_constant :Reduction
34
32
  end
35
33
  end
@@ -26,5 +26,7 @@ module Rack
26
26
  end
27
27
  end
28
28
  end
29
+
30
+ private_constant :Refinements
29
31
  end
30
32
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module Reducer
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.0.1'.freeze
4
4
  end
5
5
  end
@@ -8,8 +8,8 @@ class SinatraFunctional < Sinatra::Base
8
8
  end
9
9
 
10
10
  get '/artists' do
11
- @artists = Rack::Reducer.call(params, SEQUEL_QUERY).to_a
12
- @artists.to_json
11
+ @artists = Rack::Reducer.call(params, SEQUEL_QUERY)
12
+ @artists.to_a.to_json
13
13
  end
14
14
  end
15
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-reducer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-31 00:00:00.000000000 Z
11
+ date: 2019-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -317,7 +317,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
317
317
  version: '0'
318
318
  requirements: []
319
319
  rubyforge_project:
320
- rubygems_version: 2.4.5.5
320
+ rubygems_version: 2.7.6
321
321
  signing_key:
322
322
  specification_version: 4
323
323
  summary: Dynamically filter data via URL params, in any Rack app.