Module: Rack::Reducer
- Defined in:
- lib/rack/reducer.rb,
lib/rack/reducer/version.rb,
lib/rack/reducer/warnings.rb,
lib/rack/reducer/reduction.rb,
lib/rack/reducer/middleware.rb,
lib/rack/reducer/refinements.rb
Overview
Declaratively filter data via URL params, in any Rack app.
Defined Under Namespace
Modules: Warnings Classes: Middleware, Reduction
Constant Summary collapse
- VERSION =
'1.1.2'
Class Method Summary collapse
-
.call(params, dataset:, filters:) ⇒ Object
Filter a dataset without creating a Reducer first.
-
.create(dataset, *filters) ⇒ Object
Create a Reduction object that can filter
dataset
via#apply
. -
.new(app, options = {}) ⇒ Object
deprecated
Deprecated.
Rack::Reducer.new will become an alias of ::create in v2.0. To mount middleware that will still work in 2.0, write “use Rack::Reducer::Middleware” instead of “use Rack::Reducer”
Instance Method Summary collapse
- #reduces(dataset, filters:) ⇒ Object deprecated Deprecated.
Class Method Details
.call(params, dataset:, filters:) ⇒ Object
Filter a dataset without creating a Reducer first. Note that this approach is a bit slower and less memory-efficient than creating a Reducer via ::create. Use ::create when you can.
46 47 48 |
# File 'lib/rack/reducer.rb', line 46 def self.call(params, dataset:, filters:) Reduction.new(dataset, *filters).apply(params) end |
.create(dataset, *filters) ⇒ Object
Create a Reduction object that can filter dataset
via #apply
.
28 29 30 |
# File 'lib/rack/reducer.rb', line 28 def self.create(dataset, *filters) Reduction.new(dataset, *filters) end |
.new(app, options = {}) ⇒ Object
Rack::Reducer.new will become an alias of ::create in v2.0. To mount middleware that will still work in 2.0, write “use Rack::Reducer::Middleware” instead of “use Rack::Reducer”
Mount Rack::Reducer as middleware
55 56 57 58 |
# File 'lib/rack/reducer.rb', line 55 def self.new(app, = {}) warn "#{caller(1..1).first}}\n#{Warnings[:new]}" Middleware.new(app, ) end |
Instance Method Details
#reduces(dataset, filters:) ⇒ Object
Extend Rack::Reducer to get reduce
and reduces
as class-methods
79 80 81 82 83 84 85 |
# File 'lib/rack/reducer.rb', line 79 def reduces(dataset, filters:) warn "#{caller(1..1).first}}\n#{Warnings[:reduces]}" reducer = Reduction.new(dataset, *filters) define_singleton_method :reduce do |params| reducer.apply(params) end end |