Class: Chewy::Search::Parameters
- Defined in:
- lib/chewy/search/parameters.rb,
lib/chewy/search/parameters/aggs.rb,
lib/chewy/search/parameters/load.rb,
lib/chewy/search/parameters/none.rb,
lib/chewy/search/parameters/limit.rb,
lib/chewy/search/parameters/order.rb,
lib/chewy/search/parameters/query.rb,
lib/chewy/search/parameters/types.rb,
lib/chewy/search/parameters/filter.rb,
lib/chewy/search/parameters/offset.rb,
lib/chewy/search/parameters/source.rb,
lib/chewy/search/parameters/explain.rb,
lib/chewy/search/parameters/profile.rb,
lib/chewy/search/parameters/rescore.rb,
lib/chewy/search/parameters/storage.rb,
lib/chewy/search/parameters/suggest.rb,
lib/chewy/search/parameters/timeout.rb,
lib/chewy/search/parameters/version.rb,
lib/chewy/search/parameters/highlight.rb,
lib/chewy/search/parameters/min_score.rb,
lib/chewy/search/parameters/preference.rb,
lib/chewy/search/parameters/post_filter.rb,
lib/chewy/search/parameters/search_type.rb,
lib/chewy/search/parameters/search_after.rb,
lib/chewy/search/parameters/track_scores.rb,
lib/chewy/search/parameters/indices_boost.rb,
lib/chewy/search/parameters/request_cache.rb,
lib/chewy/search/parameters/script_fields.rb,
lib/chewy/search/parameters/stored_fields.rb,
lib/chewy/search/parameters/docvalue_fields.rb,
lib/chewy/search/parameters/terminate_after.rb,
lib/chewy/search/parameters/concerns/bool_storage.rb,
lib/chewy/search/parameters/concerns/hash_storage.rb,
lib/chewy/search/parameters/concerns/query_storage.rb,
lib/chewy/search/parameters/concerns/string_storage.rb,
lib/chewy/search/parameters/concerns/integer_storage.rb,
lib/chewy/search/parameters/concerns/string_array_storage.rb
Overview
This class is basically a compound storage of the request parameter storages. It encapsulates some storage-collection-handling logic.
Defined Under Namespace
Modules: BoolStorage, HashStorage, IntegerStorage, QueryStorage, StringArrayStorage, StringStorage Classes: Aggs, DocvalueFields, Explain, Filter, Highlight, IndicesBoost, Limit, Load, MinScore, None, Offset, Order, PostFilter, Preference, Profile, Query, RequestCache, Rescore, ScriptFields, SearchAfter, SearchType, Source, Storage, StoredFields, Suggest, TerminateAfter, Timeout, TrackScores, Types, Version
Instance Attribute Summary collapse
Class Method Summary collapse
-
.storages ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Default storage classes warehouse.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Compares storages by their values.
-
#except!(names) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Keeps only specified storages removing everything else.
-
#initialize(initial = {}) ⇒ Parameters
constructor
Accepts an initial hash as basic values or parameter storages.
-
#merge!(other) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Takes all the storages and merges them one by one using Storage#merge! method.
-
#modify!(name) { ... } ⇒ Chewy::Search::Parameters::Storage
Clones the specified storage, performs the operation defined by block on the clone.
-
#only!(names) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Removes specified storages from the storages hash.
-
#render ⇒ Hash
Renders and merges all the parameter storages into a single hash.
Constructor Details
#initialize(initial = {}) ⇒ Parameters
Accepts an initial hash as basic values or parameter storages.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/chewy/search/parameters.rb', line 36 def initialize(initial = {}) @storages = Hash.new do |hash, name| hash[name] = self.class.storages[name].new end initial.each_with_object(@storages) do |(name, value), result| storage_class = self.class.storages[name] storage = value.is_a?(storage_class) ? value : storage_class.new(value) result[name] = storage end end |
Instance Attribute Details
#storages ⇒ {Symbol => Chewy::Search::Parameters::Storage}
24 25 26 |
# File 'lib/chewy/search/parameters.rb', line 24 def storages @storages end |
Class Method Details
.storages ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Default storage classes warehouse. It is probably possible to add your own classes here if necessary, but I'm not sure it will work.
17 18 19 20 21 |
# File 'lib/chewy/search/parameters.rb', line 17 def self.storages @storages ||= Hash.new do |hash, name| hash[name] = "Chewy::Search::Parameters::#{name.to_s.camelize}".constantize end end |
Instance Method Details
#==(other) ⇒ true, false
Compares storages by their values.
51 52 53 |
# File 'lib/chewy/search/parameters.rb', line 51 def ==(other) super || other.is_a?(self.class) && compare_storages(other) end |
#except!(names) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Keeps only specified storages removing everything else.
79 80 81 |
# File 'lib/chewy/search/parameters.rb', line 79 def except!(names) @storages.except!(*assert_storages(names)) end |
#merge!(other) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Takes all the storages and merges them one by one using Chewy::Search::Parameters::Storage#merge! method. Merging is implemented in different ways for different storages: for limit, offset and other single-value classes it is a simple value replacement, for boolean storages (explain, none) it uses a disjunction result, for compound values - merging and concatenation, for query, filter, post_filter - it is the "and" operation.
94 95 96 97 98 |
# File 'lib/chewy/search/parameters.rb', line 94 def merge!(other) other.storages.each do |name, storage| modify!(name) { merge!(storage) } end end |
#modify!(name) { ... } ⇒ Chewy::Search::Parameters::Storage
Clones the specified storage, performs the operation defined by block on the clone.
61 62 63 64 65 |
# File 'lib/chewy/search/parameters.rb', line 61 def modify!(name, &block) @storages[name] = @storages[name].clone.tap do |s| s.instance_exec(&block) end end |
#only!(names) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Removes specified storages from the storages hash.
71 72 73 |
# File 'lib/chewy/search/parameters.rb', line 71 def only!(names) @storages.slice!(*assert_storages(names)) end |
#render ⇒ Hash
Renders and merges all the parameter storages into a single hash.
103 104 105 106 107 108 109 |
# File 'lib/chewy/search/parameters.rb', line 103 def render body = @storages.except(:filter, :query, :none).values.inject({}) do |result, storage| result.merge!(storage.render || {}) end body.merge!(render_query || {}) body.present? ? {body: body} : {} end |