Class: Spree::Core::VersionedValue

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/core/versioned_value.rb

Overview

Wrapper for a value that can be different depending on the Solidus version

Some configuration defaults can be added or changed when a new Solidus version is released. This class encapsulates getting the correct value for a given Solidus version.

The way it works is you provide an initial value in time, plus the version boundary where it got changed. Then you can fetch the value providing the desired Solidus version:

Remember that you must provide the exact boundary when a value got changed, which could easily be during a pre-release:

Multiple boundaries can also be provided:

Examples:

value = VersionedValue.new(true, "3.0.0" => false)
value.call("2.7.0") # => true
value.call("3.0.0") # => false
value.call("3.1.0") # => false
value = VersionedValue.new(true, "3.0.0" => false)
value.call("3.0.0.alpha") # => true

value = VersionedValue.new(true, "3.0.0.alpha" => false)
value.call("3.0.0.alpha") # => false
value = VersionedValue.new(0, "2.0.0" => 1, "3.0.0" => 2)
value.call("1.0.0") # => 0
value.call("2.1.0") # => 1
value.call("3.0.0") # => 2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_value, boundaries = {}) ⇒ VersionedValue

Returns a new instance of VersionedValue.

Parameters:

  • initial_value (Any)
  • boundary (Hash<String, Any>)

    Map from version number to new value



43
44
45
46
47
48
49
50
# File 'lib/spree/core/versioned_value.rb', line 43

def initialize(initial_value, boundaries = {})
  @boundaries = Hash[
                  { '0' => initial_value }
                  .merge(boundaries)
                  .transform_keys { |version| to_gem_version(version) }
                  .sort
                ]
end

Instance Attribute Details

#boundariesObject (readonly)

Returns the value of attribute boundaries.



39
40
41
# File 'lib/spree/core/versioned_value.rb', line 39

def boundaries
  @boundaries
end

Instance Method Details

#call(solidus_version = Spree.solidus_version) ⇒ Object

Parameters:

  • solidus_version (String) (defaults to: Spree.solidus_version)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spree/core/versioned_value.rb', line 53

def call(solidus_version = Spree.solidus_version)
  solidus_version = to_gem_version(solidus_version)
  boundaries.fetch(
    boundaries
      .keys
      .reduce do |target, following|
        if target <= solidus_version && solidus_version < following
          target
        else
          following
        end
      end
  )
end