Class: Chewy::Search::Parameters::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/search/parameters/storage.rb

Overview

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Storage

Returns a new instance of Storage.

Parameters:

  • value (Object) (defaults to: nil)

    any acceptable storage value



28
29
30
# File 'lib/chewy/search/parameters/storage.rb', line 28

def initialize(value = nil)
  replace!(value)
end

Class Attribute Details

.param_nameSymbol

The parameter name is used on rendering, derived from the class name by default, but can be easily redefined for child classes.

Examples:

class Limit < Storage
  self.param_name = :size
end

Returns:



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

#valueObject (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.

Parameters:

Returns:

  • (true, false)

    the result of comparision



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.

Parameters:

Returns:

See Also:



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.

Returns:

  • ({Symbol => Object}, nil)

    rendered value with the parameter name

See Also:



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.

Parameters:

  • new_value (Object)

    any acceptable storage value

Returns:

  • (Object)

    new normalized value

See Also:



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.

Parameters:

  • other_value (Object)

    any acceptable storage value

Returns:

See Also:



58
59
60
# File 'lib/chewy/search/parameters/storage.rb', line 58

def update!(other_value)
  replace!([value, normalize(other_value)].compact.last)
end