Class: Grape::Util::StackableValues

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

Direct Known Subclasses

ReverseStackableValues

Constant Summary collapse

EMPTY =
[].freeze

Instance Attribute Summary

Attributes inherited from BaseInheritable

#inherited_values, #new_values

Instance Method Summary collapse

Methods inherited from BaseInheritable

#delete, #initialize, #initialize_copy, #key?, #keys

Constructor Details

This class inherits a constructor from Grape::Util::BaseInheritable

Instance Method Details

#[](name) ⇒ Object

Even if there is no value, an empty (frozen) array will be returned.



9
10
11
12
13
14
15
16
# File 'lib/grape/util/stackable_values.rb', line 9

def [](name)
  inherited_value = @inherited_values[name]
  new_value = @new_values && @new_values[name]

  return new_value || EMPTY unless inherited_value

  concat_values(inherited_value, new_value)
end

#[]=(name, value) ⇒ Object



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

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

#to_hashObject



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

def to_hash
  keys.to_h do |key|
    [key, self[key]]
  end
end