Class: Grape::Util::HashStack

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeHashStack

TODO: handle aggregates



11
12
13
# File 'lib/grape/util/hash_stack.rb', line 11

def initialize
  @stack = [{}]
end

Instance Attribute Details

#stackObject (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

#cloneObject



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.

Parameters:

  • key (Symbol)

    The key to gather

Returns:

  • (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

Parameters:

  • key (Symbol)

    key to look for in hash frames

Returns:

  • value of given key after merging the stack



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

#peekObject

Returns the top hash on the stack



16
17
18
# File 'lib/grape/util/hash_stack.rb', line 16

def peek
  @stack.last
end

#popObject



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.

Parameters:

  • hash (Hash) (defaults to: {})

    optional hash to be pushed. Defaults to empty hash

Returns:



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.

Parameters:

  • key (Symbol)

    The key to set.

  • value (Object)

    The value to set.



49
50
51
# File 'lib/grape/util/hash_stack.rb', line 49

def set(key, value)
  peek[key.to_sym] = value
end

#to_sObject



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.

Parameters:

  • hash (Hash)

    Hash of values to be merged in.



57
58
59
60
# File 'lib/grape/util/hash_stack.rb', line 57

def update(hash)
  peek.merge!(hash)
  self
end