Class: Spaceship::Base::DataHash
- Inherits:
-
Object
- Object
- Spaceship::Base::DataHash
- Defined in:
- lib/spaceship/base.rb
Instance Method Summary collapse
- #get(*keys) ⇒ Object (also: #[])
-
#initialize(hash) ⇒ DataHash
constructor
A new instance of DataHash.
- #lookup(keys) ⇒ Object
- #set(keys, value) ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(hash) ⇒ DataHash
Returns a new instance of DataHash.
23 24 25 |
# File 'lib/spaceship/base.rb', line 23 def initialize(hash) @hash = hash end |
Instance Method Details
#get(*keys) ⇒ Object Also known as: []
27 28 29 |
# File 'lib/spaceship/base.rb', line 27 def get(*keys) lookup(keys) end |
#lookup(keys) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/spaceship/base.rb', line 38 def lookup(keys) head, *tail = *keys if tail.empty? @hash[head] else DataHash.new(@hash[head]).lookup(tail) end end |
#set(keys, value) ⇒ Object
32 33 34 35 36 |
# File 'lib/spaceship/base.rb', line 32 def set(keys, value) last = keys.pop ref = lookup(keys) || @hash ref[last] = value end |
#to_json ⇒ Object
47 48 49 |
# File 'lib/spaceship/base.rb', line 47 def to_json @hash.to_json end |