Class: Grape::Util::HashStack
- Inherits:
-
Object
- Object
- Grape::Util::HashStack
- Defined in:
- lib/grape/util/hash_stack.rb
Overview
HashStack is a stack of hashes. When retrieving a value, keys of the top hash on the stack take precendent over the lower keys.
Instance Attribute Summary collapse
-
#stack ⇒ Object
readonly
Unmerged array of hashes to represent the stack.
Instance Method Summary collapse
- #clone ⇒ Object
-
#concat(hash_stack) ⇒ Object
Concatenate another HashStack's to self.
-
#gather(key) ⇒ Array
Looks through the stack for all instances of a given key and returns them as a flat Array.
-
#get(key) ⇒ Object
(also: #[])
Looks through the stack for the first frame that matches :key.
-
#imbue(key, value) ⇒ Object
Adds addition value into the top hash of the stack.
-
#initialize ⇒ HashStack
constructor
TODO: handle aggregates.
-
#key?(key) ⇒ Boolean
Looks through the stack for the first frame that matches :key.
-
#peek ⇒ Object
Returns the top hash on the stack.
- #pop ⇒ Object
-
#prepend(hash_stack) ⇒ Object
Prepend another HashStack's to self.
-
#push(hash = {}) ⇒ HashStack
Add a new hash to the top of the stack.
-
#set(key, value) ⇒ Object
(also: #[]=)
Replace a value on the top hash of the stack.
- #to_s ⇒ Object
-
#update(hash) ⇒ Object
Replace multiple values on the top hash of the stack.
Constructor Details
#initialize ⇒ HashStack
TODO: handle aggregates
11 12 13 |
# File 'lib/grape/util/hash_stack.rb', line 11 def initialize @stack = [{}] end |
Instance Attribute Details
#stack ⇒ Object (readonly)
Unmerged array of hashes to represent the stack. The top of the stack is the last element.
8 9 10 |
# File 'lib/grape/util/hash_stack.rb', line 8 def stack @stack end |
Instance Method Details
#clone ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/grape/util/hash_stack.rb', line 110 def clone new_stack = HashStack.new stack.each do |frame| new_stack.push frame.clone end new_stack.stack.shift new_stack end |
#concat(hash_stack) ⇒ Object
Concatenate another HashStack's to self
92 93 94 95 |
# File 'lib/grape/util/hash_stack.rb', line 92 def concat(hash_stack) @stack.concat hash_stack.stack self end |
#gather(key) ⇒ Array
Looks through the stack for all instances of a given key and returns them as a flat Array.
102 103 104 |
# File 'lib/grape/util/hash_stack.rb', line 102 def gather(key) stack.flat_map { |s| s[key] }.compact.uniq end |
#get(key) ⇒ Object Also known as: []
Looks through the stack for the first frame that matches :key
37 38 39 40 41 42 |
# File 'lib/grape/util/hash_stack.rb', line 37 def get(key) (@stack.length - 1).downto(0).each do |i| return @stack[i][key] if @stack[i].key? key end nil end |
#imbue(key, value) ⇒ Object
Adds addition value into the top hash of the stack
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/grape/util/hash_stack.rb', line 74 def imbue(key, value) current = peek[key.to_sym] if current.is_a?(Array) current.concat(value) elsif current.is_a?(Hash) current.merge!(value) else set(key, value) end end |
#key?(key) ⇒ Boolean
Looks through the stack for the first frame that matches :key
49 50 51 52 53 54 |
# File 'lib/grape/util/hash_stack.rb', line 49 def key?(key) (@stack.length - 1).downto(0).each do |i| return true if @stack[i].key? key end false end |
#peek ⇒ Object
Returns the top hash on the stack
16 17 18 |
# File 'lib/grape/util/hash_stack.rb', line 16 def peek @stack.last end |
#pop ⇒ Object
29 30 31 |
# File 'lib/grape/util/hash_stack.rb', line 29 def pop @stack.pop end |
#prepend(hash_stack) ⇒ Object
Prepend another HashStack's to self
86 87 88 89 |
# File 'lib/grape/util/hash_stack.rb', line 86 def prepend(hash_stack) @stack.unshift(*hash_stack.stack) self end |
#push(hash = {}) ⇒ HashStack
Add a new hash to the top of the stack.
24 25 26 27 |
# File 'lib/grape/util/hash_stack.rb', line 24 def push(hash = {}) @stack.push(hash) self end |
#set(key, value) ⇒ Object Also known as: []=
Replace a value on the top hash of the stack.
60 61 62 |
# File 'lib/grape/util/hash_stack.rb', line 60 def set(key, value) peek[key.to_sym] = value end |
#to_s ⇒ Object
106 107 108 |
# File 'lib/grape/util/hash_stack.rb', line 106 def to_s @stack.to_s end |
#update(hash) ⇒ Object
Replace multiple values on the top hash of the stack.
68 69 70 71 |
# File 'lib/grape/util/hash_stack.rb', line 68 def update(hash) peek.merge!(hash) self end |