Class: Grape::Util::ReverseStackableValues

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/util/reverse_stackable_values.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inherited_values = {}) ⇒ ReverseStackableValues

Returns a new instance of ReverseStackableValues


7
8
9
10
# File 'lib/grape/util/reverse_stackable_values.rb', line 7

def initialize(inherited_values = {})
  @inherited_values = inherited_values
  @new_values = {}
end

Instance Attribute Details

#inherited_valuesObject

Returns the value of attribute inherited_values


4
5
6
# File 'lib/grape/util/reverse_stackable_values.rb', line 4

def inherited_values
  @inherited_values
end

#new_valuesObject

Returns the value of attribute new_values


5
6
7
# File 'lib/grape/util/reverse_stackable_values.rb', line 5

def new_values
  @new_values
end

Instance Method Details

#[](name) ⇒ Object


12
13
14
15
16
17
# File 'lib/grape/util/reverse_stackable_values.rb', line 12

def [](name)
  [].tap do |value|
    value.concat(@new_values[name] || [])
    value.concat(@inherited_values[name] || [])
  end
end

#[]=(name, value) ⇒ Object


19
20
21
22
# File 'lib/grape/util/reverse_stackable_values.rb', line 19

def []=(name, value)
  @new_values[name] ||= []
  @new_values[name].push value
end

#delete(key) ⇒ Object


24
25
26
# File 'lib/grape/util/reverse_stackable_values.rb', line 24

def delete(key)
  new_values.delete key
end

#initialize_copy(other) ⇒ Object


38
39
40
41
42
# File 'lib/grape/util/reverse_stackable_values.rb', line 38

def initialize_copy(other)
  super
  self.inherited_values = other.inherited_values
  self.new_values = other.new_values.dup
end

#keysObject


28
29
30
# File 'lib/grape/util/reverse_stackable_values.rb', line 28

def keys
  (@new_values.keys + @inherited_values.keys).sort.uniq
end

#to_hashObject


32
33
34
35
36
# File 'lib/grape/util/reverse_stackable_values.rb', line 32

def to_hash
  keys.each_with_object({}) do |key, result|
    result[key] = self[key]
  end
end