Class: Constants::ConstantLibrary::Collection
- Inherits:
-
Array
- Object
- Array
- Constants::ConstantLibrary::Collection
- Includes:
- Stash
- Defined in:
- lib/constants/constant_library.rb
Overview
An array-based Stash for collections of library objects.
– Note: comparison of Index and Collection indicates that these are highly related classes. Why no exclusion value and why no modifiable nil_value for Collection? Simply because an array ALWAYS returns nil for uninitialized locations (esp out-of-bounds locations). This means that Stash, which uses the value at self[] to determine when to stash and when not to stash, must have nil as it’s nil_value to behave correctly. Effectively treating nil as an exclusion value for collection works well in this case since nils cannot be stashed.
Hashes (ie Index) do not share this behavior. Since you can define a default value for missing keys, self[] can return something other than nil… hence there is an opportunity to use non-nil nil_values and non-nil exclusion values.
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
The block used to calculate keys during stash.
Attributes included from Stash
Instance Method Summary collapse
-
#initialize(&block) ⇒ Collection
constructor
Initializes a new Collection (a type of Array).
-
#stash(values) ⇒ Object
Stashes the specified values in self using values calculated by the block.
Constructor Details
#initialize(&block) ⇒ Collection
Initializes a new Collection (a type of Array). The block is used by stash to calculate the values in a collection.
88 89 90 91 92 |
# File 'lib/constants/constant_library.rb', line 88 def initialize(&block) super(&nil) @nil_value = nil @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
The block used to calculate keys during stash
84 85 86 |
# File 'lib/constants/constant_library.rb', line 84 def block @block end |
Instance Method Details
#stash(values) ⇒ Object
Stashes the specified values in self using values calculated by the block. Values are skipped if the block returns nil.
See Constants::ConstantLibrary#collect for more details.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/constants/constant_library.rb', line 98 def stash(values) values.each do |value| value, index = block.call(value) next if value == nil super(index == nil ? self.length : index, value) end self end |