Class: Grape::Util::StackableValues
- Inherits:
-
Object
- Object
- Grape::Util::StackableValues
- Defined in:
- lib/grape/util/stackable_values.rb
Instance Attribute Summary collapse
-
#froozen_values ⇒ Object
readonly
Returns the value of attribute froozen_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 = {} @froozen_values = {} end |
Instance Attribute Details
#froozen_values ⇒ Object (readonly)
Returns the value of attribute froozen_values.
6 7 8 |
# File 'lib/grape/util/stackable_values.rb', line 6 def froozen_values @froozen_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 |
# File 'lib/grape/util/stackable_values.rb', line 14 def [](name) return @froozen_values[name] if @froozen_values.key? name value = [@inherited_values[name], @new_values[name]] value.compact! value.flatten!(1) value end |
#[]=(name, value) ⇒ Object
22 23 24 25 26 |
# File 'lib/grape/util/stackable_values.rb', line 22 def []=(name, value) fail if @froozen_values.key? name @new_values[name] ||= [] @new_values[name].push value end |
#delete(key) ⇒ Object
28 29 30 |
# File 'lib/grape/util/stackable_values.rb', line 28 def delete(key) new_values.delete key end |
#freeze_value(key) ⇒ Object
44 45 46 |
# File 'lib/grape/util/stackable_values.rb', line 44 def freeze_value(key) @froozen_values[key] = self[key].freeze end |
#initialize_copy(other) ⇒ Object
48 49 50 51 52 |
# File 'lib/grape/util/stackable_values.rb', line 48 def initialize_copy(other) super self.inherited_values = other.inherited_values self.new_values = other.new_values.dup end |
#keys ⇒ Object
34 35 36 |
# File 'lib/grape/util/stackable_values.rb', line 34 def keys (@new_values.keys + @inherited_values.keys).sort.uniq end |
#to_hash ⇒ Object
38 39 40 41 42 |
# File 'lib/grape/util/stackable_values.rb', line 38 def to_hash keys.each_with_object({}) do |key, result| result[key] = self[key] end end |