class Chewy::Search::Parameters::Storage

Base parameter storage, defines a conventional API and its default behavior.

Attributes

param_name[W]
value[R]

Returns normalized storage value.

Public Class Methods

new(value = nil) click to toggle source

@param value [Object] any acceptable storage value

# File lib/chewy/search/parameters/storage.rb, line 28
def initialize(value = nil)
  replace!(value)
end
param_name() click to toggle source

@!attribute [rw] param_name The parameter name is used on rendering, derived from the class name by default, but can be easily redefined for child classes.

@example

class Limit < Storage
  self.param_name = :size
end

@return [Symbol] parameter name

# File lib/chewy/search/parameters/storage.rb, line 19
def param_name
  @param_name ||= name.demodulize.underscore.to_sym
end

Public Instance Methods

==(other) click to toggle source

Compares two storages, basically, classes and values should be identical.

@param other [Chewy::Search::Parameters::Storage] any storage instance @return [true, false] the result of comparision

Calls superclass method
# File lib/chewy/search/parameters/storage.rb, line 37
def ==(other)
  super || other.class == self.class && other.value == value
end
merge!(other) click to toggle source

Merges one storage with another one using update by default. Requires redefinition sometimes.

@see Chewy::Search::Parameters#merge! @see Chewy::Search::Request#merge @param other [Chewy::Search::Parameters::Storage] other storage @return [Object] updated value

# File lib/chewy/search/parameters/storage.rb, line 69
def merge!(other)
  update!(other.value)
end
render() click to toggle source

Basic parameter rendering logic, don't need to return anything if parameter doesn't require rendering for the current value.

@see Chewy::Search::Parameters#render @see Chewy::Search::Request#render @return [{Symbol => Object}, nil] rendered value with the parameter name

# File lib/chewy/search/parameters/storage.rb, line 79
def render
  {self.class.param_name => value} if value.present?
end
replace!(new_value) click to toggle source

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.

@see Chewy::Search::Request @param new_value [Object] any acceptable storage value @return [Object] new normalized value

# File lib/chewy/search/parameters/storage.rb, line 48
def replace!(new_value)
  @value = normalize(new_value)
end
update!(other_value) click to toggle source

Implements the storage update logic, picks the first present value by default, but can be redefined if necessary.

@see Chewy::Search::Request @param other_value [Object] any acceptable storage value @return [Object] updated value

# File lib/chewy/search/parameters/storage.rb, line 58
def update!(other_value)
  replace!([value, normalize(other_value)].compact.last)
end

Private Instance Methods

initialize_clone(origin) click to toggle source
# File lib/chewy/search/parameters/storage.rb, line 85
def initialize_clone(origin)
  @value = origin.value.deep_dup
end
normalize(value) click to toggle source
# File lib/chewy/search/parameters/storage.rb, line 89
def normalize(value)
  value
end