Class: Grape::Util::StackableValues
- Inherits:
-
Object
- Object
- Grape::Util::StackableValues
- Defined in:
- lib/grape/util/stackable_values.rb
Instance Attribute Summary collapse
-
#frozen_values ⇒ Object
readonly
Returns the value of attribute frozen_values.
-
#inherited_values ⇒ Object
Returns the value of attribute inherited_values.
-
#new_values ⇒ Object
Returns the value of attribute new_values.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #delete(key) ⇒ Object
- #freeze_value(key) ⇒ Object
-
#initialize(inherited_values = {}) ⇒ StackableValues
constructor
A new instance of StackableValues.
- #initialize_copy(other) ⇒ Object
- #keys ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(inherited_values = {}) ⇒ StackableValues
Returns a new instance of StackableValues.
8 9 10 11 12 |
# File 'lib/grape/util/stackable_values.rb', line 8 def initialize(inherited_values = {}) @inherited_values = inherited_values @new_values = {} @frozen_values = {} end |
Instance Attribute Details
#frozen_values ⇒ Object (readonly)
Returns the value of attribute frozen_values.
6 7 8 |
# File 'lib/grape/util/stackable_values.rb', line 6 def frozen_values @frozen_values end |
#inherited_values ⇒ Object
Returns the value of attribute inherited_values.
4 5 6 |
# File 'lib/grape/util/stackable_values.rb', line 4 def inherited_values @inherited_values end |
#new_values ⇒ Object
Returns the value of attribute new_values.
5 6 7 |
# File 'lib/grape/util/stackable_values.rb', line 5 def new_values @new_values end |
Instance Method Details
#[](name) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/grape/util/stackable_values.rb', line 14 def [](name) return @frozen_values[name] if @frozen_values.key? name value = [] value.concat(@inherited_values[name] || []) value.concat(@new_values[name] || []) value end |
#[]=(name, value) ⇒ Object
23 24 25 26 27 |
# File 'lib/grape/util/stackable_values.rb', line 23 def []=(name, value) raise if @frozen_values.key? name @new_values[name] ||= [] @new_values[name].push value end |
#delete(key) ⇒ Object
29 30 31 |
# File 'lib/grape/util/stackable_values.rb', line 29 def delete(key) new_values.delete key end |
#freeze_value(key) ⇒ Object
43 44 45 |
# File 'lib/grape/util/stackable_values.rb', line 43 def freeze_value(key) @frozen_values[key] = self[key].freeze end |
#initialize_copy(other) ⇒ Object
47 48 49 50 51 |
# File 'lib/grape/util/stackable_values.rb', line 47 def initialize_copy(other) super self.inherited_values = other.inherited_values self.new_values = other.new_values.dup end |
#keys ⇒ Object
33 34 35 |
# File 'lib/grape/util/stackable_values.rb', line 33 def keys (@new_values.keys + @inherited_values.keys).sort.uniq end |
#to_hash ⇒ Object
37 38 39 40 41 |
# File 'lib/grape/util/stackable_values.rb', line 37 def to_hash keys.each_with_object({}) do |key, result| result[key] = self[key] end end |