Object
HashStack is a stack of hashes. When retrieving a value, keys of the top hash on the stack take precendent over the lower keys.
# 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
Concatenate another HashStack’s to self
# File lib/grape/util/hash_stack.rb, line 92 def concat(hash_stack) @stack.concat hash_stack.stack self end
Looks through the stack for all instances of a given key and returns them as a flat Array.
@param key [Symbol] The key to gather @return [Array]
# File lib/grape/util/hash_stack.rb, line 102 def gather(key) stack.flat_map { |s| s[key] }.compact.uniq end
Looks through the stack for the first frame that matches :key
@param key [Symbol] key to look for in hash frames @return value of given key after merging the stack
# 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
Adds addition value into the top hash of the stack
# 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
Looks through the stack for the first frame that matches :key
@param key [Symbol] key to look for in hash frames @return true if key exists, false otherwise
# 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
Returns the top hash on the stack
# File lib/grape/util/hash_stack.rb, line 16 def peek @stack.last end
Prepend another HashStack’s to self
# File lib/grape/util/hash_stack.rb, line 86 def prepend(hash_stack) @stack.unshift(*hash_stack.stack) self end
Add a new hash to the top of the stack.
@param hash [Hash] optional hash to be pushed. Defaults to empty hash @return [HashStack]
# File lib/grape/util/hash_stack.rb, line 24 def push(hash = {}) @stack.push(hash) self end
Replace a value on the top hash of the stack.
@param key [Symbol] The key to set. @param value [Object] The value to set.
# File lib/grape/util/hash_stack.rb, line 60 def set(key, value) peek[key.to_sym] = value end
# File lib/grape/util/hash_stack.rb, line 106 def to_s @stack.to_s end
Replace multiple values on the top hash of the stack.
@param hash [Hash] Hash of values to be merged in.
# File lib/grape/util/hash_stack.rb, line 68 def update(hash) peek.merge!(hash) self end
Generated with the Darkfish Rdoc Generator 2.