Class: Chewy::Search::Parameters::Storage
- Inherits:
-
Object
- Object
- Chewy::Search::Parameters::Storage
- Defined in:
- lib/chewy/search/parameters/storage.rb
Overview
Base parameter storage, defines a conventional API and its default behavior.
Direct Known Subclasses
Aggs, AllowPartialSearchResults, Collapse, DocvalueFields, Explain, Filter, Highlight, IgnoreUnavailable, Indices, IndicesBoost, Knn, Limit, Load, MinScore, None, Offset, Order, PostFilter, Preference, Profile, Query, RequestCache, Rescore, ScriptFields, SearchAfter, SearchType, Source, StoredFields, Suggest, TerminateAfter, Timeout, TrackScores, TrackTotalHits, Version
Class Attribute Summary collapse
-
.param_name ⇒ Symbol
The parameter name is used on rendering, derived from the class name by default, but can be easily redefined for child classes.
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns normalized storage value.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Compares two storages, basically, classes and values should be identical.
-
#initialize(value = nil) ⇒ Storage
constructor
A new instance of Storage.
-
#merge!(other) ⇒ Object
Merges one storage with another one using update by default.
-
#render ⇒ {Symbol => Object}?
Basic parameter rendering logic, don't need to return anything if parameter doesn't require rendering for the current value.
-
#replace!(new_value) ⇒ Object
Replaces current value with normalized provided one.
-
#update!(other_value) ⇒ Object
Implements the storage update logic, picks the first present value by default, but can be redefined if necessary.
Constructor Details
#initialize(value = nil) ⇒ Storage
Returns a new instance of Storage.
28 29 30 |
# File 'lib/chewy/search/parameters/storage.rb', line 28 def initialize(value = nil) replace!(value) end |
Class Attribute Details
.param_name ⇒ Symbol
The parameter name is used on rendering, derived from the class name by default, but can be easily redefined for child classes.
19 20 21 |
# File 'lib/chewy/search/parameters/storage.rb', line 19 def param_name @param_name ||= name.demodulize.underscore.to_sym end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns normalized storage value.
25 26 27 |
# File 'lib/chewy/search/parameters/storage.rb', line 25 def value @value end |
Instance Method Details
#==(other) ⇒ true, false
Compares two storages, basically, classes and values should be identical.
37 38 39 |
# File 'lib/chewy/search/parameters/storage.rb', line 37 def ==(other) super || (other.class == self.class && other.value == value) end |
#merge!(other) ⇒ Object
Merges one storage with another one using update by default. Requires redefinition sometimes.
69 70 71 |
# File 'lib/chewy/search/parameters/storage.rb', line 69 def merge!(other) update!(other.value) end |
#render ⇒ {Symbol => Object}?
Basic parameter rendering logic, don't need to return anything if parameter doesn't require rendering for the current value.
79 80 81 |
# File 'lib/chewy/search/parameters/storage.rb', line 79 def render {self.class.param_name => value} if value.present? end |
#replace!(new_value) ⇒ Object
Replaces current value with normalized provided one. Doesn't make sense to redefine it in child classes, the replacement logic should be kept as is.
48 49 50 |
# File 'lib/chewy/search/parameters/storage.rb', line 48 def replace!(new_value) @value = normalize(new_value) end |
#update!(other_value) ⇒ Object
Implements the storage update logic, picks the first present value by default, but can be redefined if necessary.
58 59 60 |
# File 'lib/chewy/search/parameters/storage.rb', line 58 def update!(other_value) replace!([value, normalize(other_value)].compact.last) end |