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 +5 -5
- data/README.md +4 -3
- data/lib/rack/reducer.rb +32 -19
- data/lib/rack/reducer/parser.rb +6 -4
- data/lib/rack/reducer/reduction.rb +6 -8
- data/lib/rack/reducer/refinements.rb +2 -0
- data/lib/rack/reducer/version.rb +1 -1
- data/spec/sinatra_functional_spec.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d400c7b68a3ecf7da56af02f7f5e86ab18177ff306d6871a10ef8f9e7bcb7109
|
4
|
+
data.tar.gz: ff1b1f50ccb165d041a04fced63adbd946d8976f06460ce83c30b66c91599c17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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)
|
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
|
-
#
|
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
|
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
|
-
#
|
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
|
-
|
40
|
-
|
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
|
data/lib/rack/reducer/parser.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Rack
|
2
2
|
module Reducer
|
3
|
-
#
|
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
|
-
#
|
16
|
-
#
|
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],
|
24
|
-
|
25
|
-
|
26
|
-
private
|
23
|
+
@props[:filters].reduce(@props[:dataset]) do |data, filter|
|
24
|
+
next data unless filter.satisfies?(@params)
|
27
25
|
|
28
|
-
|
29
|
-
|
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
|
data/lib/rack/reducer/version.rb
CHANGED
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.
|
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:
|
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.
|
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.
|