Class: Hyde::Util::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/hyde/util/lookup.rb

Overview

Value container with recursive lookup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, hash = {}) ⇒ Lookup

Returns a new instance of Lookup.

Parameters:

  • parent (Lookup, nil) (defaults to: nil)


9
10
11
12
# File 'lib/hyde/util/lookup.rb', line 9

def initialize(parent = nil, hash = {})
  @parent = (parent or Hash.new(nil))
  @storage = hash
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



34
35
36
# File 'lib/hyde/util/lookup.rb', line 34

def parent
  @parent
end

Class Method Details

.[](hash) ⇒ Object

Initialize a Lookup from Hash

Parameters:

  • hash (Hash)


16
17
18
# File 'lib/hyde/util/lookup.rb', line 16

def self.[](hash)
  Lookup.new(nil, Hash[hash])
end

Instance Method Details

#[](key) ⇒ Object?

Get a value by key

Parameters:

  • key (#hash)

    key for value

Returns:

  • (Object, nil)


23
24
25
# File 'lib/hyde/util/lookup.rb', line 23

def [](key)
  @storage[key] or @parent[key]
end

#[]=(key, value) ⇒ Object

Set a value by key

Parameters:

  • key (#hash)

    key for value

  • value (Object)

    value value



30
31
32
# File 'lib/hyde/util/lookup.rb', line 30

def []=(key, value)
  @storage[key] = value
end