lib/rack/reducer.rb in rack-reducer-1.0.0 vs lib/rack/reducer.rb in rack-reducer-1.0.1
- old
+ new
@@ -2,11 +2,25 @@
require_relative 'reducer/middleware'
module Rack
# Use request params to apply filters to a dataset
module Reducer
- # Call Rack::Reducer as a function
+ # Filter a dataset
+ # @param params [Hash] Rack-compatible URL params
+ # @param dataset [Object] A dataset, e.g. one of your App's models
+ # @param filters [Array<Proc>] An array of lambdas with keyword arguments
+ # @example Call Rack::Reducer as a function in a Sinatra app
+ # ArtistReducer = {
+ # dataset: Artist,
+ # filters: [
+ # lambda { |name:| where(name: name) },
+ # lambda { |genre:| where(genre: genre) },
+ # ]
+ # }
+ # get '/artists' do
+ # @artists = Rack::Reducer.call(params, ArtistReducer)
+ # end
def self.call(params, dataset:, filters:)
Reduction.new(
params: params,
filters: filters,
dataset: dataset
@@ -16,28 +30,27 @@
# Mount Rack::Reducer as middleware
def self.new(app, options = {})
Middleware.new(app, options)
end
- # Extend Rack::Reducer to get `reduce` and `reduces` as class-methods
+ # Extend Rack::Reducer to get +reduce+ and +reduces+ as class-methods
+ # @example Make an "Artists" model reducible
+ # class Artist < SomeORM::Model
+ # extend Rack::Reducer
+ # reduces self.all, filters: [
+ # lambda { |name:| where(name: name) },
+ # lambda { |genre:| where(genre: genre) },
+ # ]
+ # end
#
- # class Artist < SomeORM::Model
- # extend Rack::Reducer
- # reduces self.all, filters: [
- # lambda { |name:| where(name: name) },
- # lambda { |genre:| where(genre: genre) },
- # ]
- # end
- def reduce(params)
- Reduction.new(
- params: params,
- filters: @rack_reducer_filters,
- dataset: @rack_reducer_dataset
- ).reduce
- end
-
+ # Artist.reduce(params)
def reduces(dataset, filters:)
- @rack_reducer_dataset = dataset
- @rack_reducer_filters = filters
+ define_singleton_method :reduce do |params|
+ Reduction.new(
+ params: params,
+ filters: filters,
+ dataset: dataset,
+ ).reduce
+ end
end
end
end