Class: Might::Fetcher

Inherits:
Object
  • Object
show all
Extended by:
Uber::InheritableAttr
Defined in:
lib/might/fetcher.rb

Overview

Configure your own fetcher

PagesFetcher < Might::Fetcher
  self.resource_class = Page
end

You can configure filterable attributes for model

filter :id, validates: { presence: true }
filter :name
filter :start_at, validates: { presence: true }

If your property name doesn’t match the name in the query string, use the :as option:

filter :kind, as: :type

So the Movie#kind property would be exposed to API as :type

You may specify allowed sorting order:

sort :id
sort :name

If your property name doesn’t match the name in the query string, use the :as option:

sort :position, as: :relevance

So client should pass ?sort=relevance in order to sort by position

It’s also possible to reverse meaning of the order direction. For example it’s not make sense to order by position from lower value to higher. The meaning default for that sorting is reversed order by default, so more relevant elenents would be the first.

sort :position, as: :relevance, reverse_direction: true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Fetcher

Returns a new instance of Fetcher.

Parameters:

  • params (Hash)


55
56
57
# File 'lib/might/fetcher.rb', line 55

def initialize(params)
  @params = params.with_indifferent_access
end

Instance Attribute Details

#paramsHash (readonly)

Returns:

  • (Hash)


52
53
54
# File 'lib/might/fetcher.rb', line 52

def params
  @params
end

Class Method Details

.after(middleware_or_index = nil, &block) ⇒ Object

Add middleware to the end of middleware chane

class MovieFetcher
  after do |scope, params|
    # do something with scope and params
    [scope.map(&:resource), params]
  end
end


215
216
217
218
# File 'lib/might/fetcher.rb', line 215

def after(middleware_or_index = nil, &block)
  args = middleware_or_index ? [:insert_after, middleware_or_index] : [:use]
  alter_middleware(*args, &block)
end

.before(middleware_or_index = 0, &block) ⇒ Object

Add middleware to the beginning of middleware chane

class MovieFetcher
  before do |scope, params|
    # do something with scope and params
    [scope.map(&:resource), params]
  end
end


229
230
231
# File 'lib/might/fetcher.rb', line 229

def before(middleware_or_index = 0, &block)
  alter_middleware(:insert_before, middleware_or_index, &block)
end

.filter(filter_name, options) ⇒ void .filter(filter_name: predicates, **options) ⇒ void

Register collection filter by its name

Examples:

filter :genre_name, on: :resource
filter genre_name: [:eq, :in], on: :resource

Overloads:

  • .filter(filter_name, options) ⇒ void

    This method returns an undefined value.

    Parameters:

    • filter_name (Symbol)
    • options (Hash)
  • .filter(filter_name: predicates, **options) ⇒ void

    This method returns an undefined value.

    Parameters:

    • filter_name (Symbol) (defaults to: predicates)
    • predicates (<Symbol>)
    • other (Hash)

      options options

See Also:

  • for details


186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/might/fetcher.rb', line 186

def filter(*args)
  options = args.extract_options!
  if args.empty?
    filter_name = options.keys.first
    predicates = options.values.first
    options = options.except(filter_name).merge(predicates: predicates)
  else
    filter_name = args.first
  end

  definition = FilterParameterDefinition.new(filter_name, options)
  filter_parameters_definition.add(definition)
end

.middleware(&change) ⇒ Might

Alter middleware chain with the given block

Examples:

class ChannelsFetcher
  middleware do
    use CustomMiddleware
  end
end

Parameters:

  • change (Proc)

Returns:



152
153
154
155
156
# File 'lib/might/fetcher.rb', line 152

def middleware(&change)
  fail ArgumentError unless block_given?
  middleware_changes << change
  self
end

.run(params) {|collection| ... } ⇒ Object

Parameters:

  • params (Hash)

    user provided input

Yield Parameters:

  • collection (ActiveRecord::Relation)


202
203
204
# File 'lib/might/fetcher.rb', line 202

def run(params, &block)
  new(params).call(&block)
end

.sort(name, options = {}) ⇒ void

This method returns an undefined value.

Register collection sorting by its name

Parameters:

  • name (Symbol)

    of the field

See Also:

  • for details


163
164
165
166
# File 'lib/might/fetcher.rb', line 163

def sort(name, options = {})
  definition = SortParameterDefinition.new(name, options)
  sort_parameters_definition.add(definition)
end

Instance Method Details

#call {|collection| ... } ⇒ Might::Result

Returns filtered and sorted collection.

Examples:

PagesFetcher.new(params).call #=> Result

block syntax

PagesFetcher.new(params) do |result|
  if result.success?
    result.get
  else
    result.errors
  end
end

Yield Parameters:

  • collection (Result)

    if a block given

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/might/fetcher.rb', line 74

def call
  processed_params, errors = process_params(params)
  result = if errors.any?
             Failure.new(errors)
           else
             Success.new(fetch(processed_params))
           end

  if block_given?
    yield result
  else
    result
  end
end