-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathstackable_values.rb
51 lines (39 loc) · 1.06 KB
/
stackable_values.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require_relative 'base_inheritable'
module Grape
module Util
class StackableValues < BaseInheritable
attr_reader :frozen_values
def initialize(*_args)
super
@frozen_values = {}
end
def [](name)
return @frozen_values[name] if @frozen_values.key? name
inherited_value = @inherited_values[name]
new_value = @new_values[name] || []
return new_value unless inherited_value
concat_values(inherited_value, new_value)
end
def []=(name, value)
raise if @frozen_values.key? name
@new_values[name] ||= []
@new_values[name].push value
end
def to_hash
keys.each_with_object({}) do |key, result|
result[key] = self[key]
end
end
def freeze_value(key)
@frozen_values[key] = self[key].freeze
end
protected
def concat_values(inherited_value, new_value)
[].tap do |value|
value.concat(inherited_value)
value.concat(new_value)
end
end
end
end
end