Class: Might::Fetcher
- Inherits:
-
Object
- Object
- Might::Fetcher
- 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
- #params ⇒ Hash readonly
Class Method Summary collapse
-
.after(middleware_or_index = nil, &block) ⇒ Object
Add middleware to the end of middleware chane.
-
.before(middleware_or_index = 0, &block) ⇒ Object
Add middleware to the beginning of middleware chane.
-
.filter(*args) ⇒ Object
Register collection filter by its name.
-
.middleware(&change) ⇒ Might
Alter middleware chain with the given block.
- .run(params) {|collection| ... } ⇒ Object
-
.sort(name, options = {}) ⇒ void
Register collection sorting by its name.
Instance Method Summary collapse
-
#call {|collection| ... } ⇒ Might::Result
Filtered and sorted collection.
-
#initialize(params) ⇒ Fetcher
constructor
A new instance of Fetcher.
Constructor Details
#initialize(params) ⇒ Fetcher
Returns a new instance of Fetcher.
55 56 57 |
# File 'lib/might/fetcher.rb', line 55 def initialize(params) @params = params.with_indifferent_access end |
Instance Attribute Details
#params ⇒ Hash (readonly)
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
186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/might/fetcher.rb', line 186 def filter(*args) = args. if args.empty? filter_name = .keys.first predicates = .values.first = .except(filter_name).merge(predicates: predicates) else filter_name = args.first end definition = FilterParameterDefinition.new(filter_name, ) filter_parameters_definition.add(definition) end |
.middleware(&change) ⇒ Might
Alter middleware chain with the given block
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
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
163 164 165 166 |
# File 'lib/might/fetcher.rb', line 163 def sort(name, = {}) definition = SortParameterDefinition.new(name, ) sort_parameters_definition.add(definition) end |
Instance Method Details
#call {|collection| ... } ⇒ Might::Result
Returns filtered and sorted collection.
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 |