Class: Grape::Util::StackableValues
- Inherits:
-
BaseInheritable
- Object
- BaseInheritable
- Grape::Util::StackableValues
- Defined in:
- lib/grape/util/stackable_values.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#frozen_values ⇒ Object
readonly
Returns the value of attribute frozen_values.
Attributes inherited from BaseInheritable
#inherited_values, #new_values
Instance Method Summary collapse
-
#[](name) ⇒ Object
Even if there is no value, an empty array will be returned.
- #[]=(name, value) ⇒ Object
- #freeze_value(key) ⇒ Object
-
#initialize(*_args) ⇒ StackableValues
constructor
A new instance of StackableValues.
- #to_hash ⇒ Object
Methods inherited from BaseInheritable
#delete, #initialize_copy, #keys
Constructor Details
#initialize(*_args) ⇒ StackableValues
Returns a new instance of StackableValues.
10 11 12 13 14 |
# File 'lib/grape/util/stackable_values.rb', line 10 def initialize(*_args) super @frozen_values = {} end |
Instance Attribute Details
#frozen_values ⇒ Object (readonly)
Returns the value of attribute frozen_values.
8 9 10 |
# File 'lib/grape/util/stackable_values.rb', line 8 def frozen_values @frozen_values end |
Instance Method Details
#[](name) ⇒ Object
Even if there is no value, an empty array will be returned.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/grape/util/stackable_values.rb', line 17 def [](name) return @frozen_values[name] if @frozen_values.key? name inherited_value = @inherited_values[name] new_value = @new_values[name] || [] return new_value unless inherited_value concat_values(inherited_value, new_value) end |
#[]=(name, value) ⇒ Object
28 29 30 31 32 |
# File 'lib/grape/util/stackable_values.rb', line 28 def []=(name, value) raise if @frozen_values.key? name @new_values[name] ||= [] @new_values[name].push value end |
#freeze_value(key) ⇒ Object
40 41 42 |
# File 'lib/grape/util/stackable_values.rb', line 40 def freeze_value(key) @frozen_values[key] = self[key].freeze end |
#to_hash ⇒ Object
34 35 36 37 38 |
# File 'lib/grape/util/stackable_values.rb', line 34 def to_hash keys.each_with_object({}) do |key, result| result[key] = self[key] end end |