Class: Ibrain::Core::VersionedValue
- Inherits:
-
Object
- Object
- Ibrain::Core::VersionedValue
- Defined in:
- lib/ibrain/core/versioned_value.rb
Overview
Wrapper for a value that can be different depending on the Ibrain version
Some configuration defaults can be added or changed when a new Ibrain version is released. This class encapsulates getting the correct value for a given Ibrain 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 Ibrain 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:
Instance Attribute Summary collapse
-
#boundaries ⇒ Object
readonly
Returns the value of attribute boundaries.
Instance Method Summary collapse
- #call(ibrain_version = Ibrain.ibrain_version) ⇒ Object
-
#initialize(initial_value, boundaries = {}) ⇒ VersionedValue
constructor
A new instance of VersionedValue.
Constructor Details
#initialize(initial_value, boundaries = {}) ⇒ VersionedValue
Returns a new instance of VersionedValue.
43 44 45 46 47 48 |
# File 'lib/ibrain/core/versioned_value.rb', line 43 def initialize(initial_value, boundaries = {}) @boundaries = { '0' => initial_value } .merge(boundaries) .transform_keys { |version| to_gem_version(version) } .sort.to_h end |
Instance Attribute Details
#boundaries ⇒ Object (readonly)
Returns the value of attribute boundaries.
39 40 41 |
# File 'lib/ibrain/core/versioned_value.rb', line 39 def boundaries @boundaries end |
Instance Method Details
#call(ibrain_version = Ibrain.ibrain_version) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ibrain/core/versioned_value.rb', line 51 def call(ibrain_version = Ibrain.ibrain_version) ibrain_version = to_gem_version(ibrain_version) boundaries.fetch( boundaries .keys .reduce do |target, following| if target <= ibrain_version && ibrain_version < following target else following end end ) end |