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



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.

Parameters:

  • key (Symbol)

    The key to gather

Returns:

  • (Array)


102
103
104
# File 'lib/grape/util/hash_stack.rb', line 102

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

#has_key?(key) ⇒ Boolean

Looks through the stack for the first frame that matches :key

Parameters:

  • key (Symbol)

    key to look for in hash frames

Returns:

  • (Boolean)

    true if key exists, false otherwise



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

def has_key?(key)
  (@stack.length - 1).downto(0).each do |i|
    return true if @stack[i].key? key
  end
  false
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

#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



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.

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.



60
61
62
# File 'lib/grape/util/hash_stack.rb', line 60

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

#to_sObject



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.

Parameters:

  • hash (Hash)

    Hash of values to be merged in.



68
69
70
71
# File 'lib/grape/util/hash_stack.rb', line 68

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