Class: Grape::Util::StackableValues

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inherited_values = {}) ⇒ 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_valuesObject (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_valuesObject

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_valuesObject

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

#keysObject


33
34
35
# File 'lib/grape/util/stackable_values.rb', line 33

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

#to_hashObject


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