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

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_valuesObject (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_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
# File 'lib/grape/util/stackable_values.rb', line 14

def [](name)
  return @froozen_values[name] if @froozen_values.key? name
  [@inherited_values[name], @new_values[name]].compact.flatten(1)
end

#[]=(name, value) ⇒ Object



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

def []=(name, value)
  fail if @froozen_values.key? name
  @new_values[name] ||= []
  @new_values[name].push value
end

#delete(key) ⇒ Object



25
26
27
# File 'lib/grape/util/stackable_values.rb', line 25

def delete(key)
  new_values.delete key
end

#freeze_value(key) ⇒ Object



41
42
43
# File 'lib/grape/util/stackable_values.rb', line 41

def freeze_value(key)
  @froozen_values[key] = self[key].freeze
end

#initialize_copy(other) ⇒ Object



45
46
47
48
49
# File 'lib/grape/util/stackable_values.rb', line 45

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

#keysObject



31
32
33
# File 'lib/grape/util/stackable_values.rb', line 31

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

#to_hashObject



35
36
37
38
39
# File 'lib/grape/util/stackable_values.rb', line 35

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