Parent

Class/Module Index [+]

Quicksearch

Grape::Util::HashStack

HashStack is a stack of hashes. When retrieving a value, keys of the top hash on the stack take precendent over the lower keys.

Attributes

stack[R]

Unmerged array of hashes to represent the stack. The top of the stack is the last element.

Public Class Methods

new() click to toggle source

TODO: handle aggregates

# File lib/grape/util/hash_stack.rb, line 11
def initialize
  @stack = [{}]
end

Public Instance Methods

[](key) click to toggle source
Alias for: get
[]=(key, value) click to toggle source
Alias for: set
clone() click to toggle source
# 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) click to toggle source

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
gather(key) click to toggle source

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
get(key) click to toggle source

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
Also aliased as: []
imbue(key, value) click to toggle source

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
key?(key) click to toggle source

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
peek() click to toggle source

Returns the top hash on the stack

# File lib/grape/util/hash_stack.rb, line 16
def peek
  @stack.last
end
pop() click to toggle source
# File lib/grape/util/hash_stack.rb, line 29
def pop
  @stack.pop
end
prepend(hash_stack) click to toggle source

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
push(hash = {}) click to toggle source

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
set(key, value) click to toggle source

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
Also aliased as: []=
to_s() click to toggle source
# File lib/grape/util/hash_stack.rb, line 106
def to_s
  @stack.to_s
end
update(hash) click to toggle source

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.