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.
-
#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
99 100 101 102 103 104 105 106 |
# File 'lib/grape/util/hash_stack.rb', line 99 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
81 82 83 84 |
# File 'lib/grape/util/hash_stack.rb', line 81 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.
91 92 93 |
# File 'lib/grape/util/hash_stack.rb', line 91 def gather(key) stack.map{|s| s[key] }.flatten.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
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/grape/util/hash_stack.rb', line 63 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 |
#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
75 76 77 78 |
# File 'lib/grape/util/hash_stack.rb', line 75 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.
49 50 51 |
# File 'lib/grape/util/hash_stack.rb', line 49 def set(key, value) peek[key.to_sym] = value end |
#to_s ⇒ Object
95 96 97 |
# File 'lib/grape/util/hash_stack.rb', line 95 def to_s @stack.to_s end |
#update(hash) ⇒ Object
Replace multiple values on the top hash of the stack.
57 58 59 60 |
# File 'lib/grape/util/hash_stack.rb', line 57 def update(hash) peek.merge!(hash) self end |