Sha256: d74566d077e7208b7b137ad73344f45b85ba16590aa056051b02ee3d4c7b387b

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require 'rack/request'
require_relative 'reducer/reduction'
require_relative 'reducer/middleware'

module Rack
  # Use request params to apply filters to a dataset
  module Reducer
    # Call Rack::Reducer as a function
    def self.call(params, dataset:, filters:)
      Reduction.new(
        params: params,
        filters: filters,
        dataset: dataset
      ).reduce
    end

    # 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
    #
    # 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

    def reduces(dataset, filters:)
      @rack_reducer_dataset = dataset
      @rack_reducer_filters = filters
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-reducer-0.1.2 lib/rack/reducer.rb
rack-reducer-0.1.1 lib/rack/reducer.rb