Class: Spaceship::Base::DataHash
- Inherits:
-
Object
- Object
- Spaceship::Base::DataHash
- Includes:
- Enumerable
- Defined in:
- spaceship/lib/spaceship/base.rb
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #each(&block) ⇒ Object
- #get(*keys) ⇒ Object (also: #[])
-
#initialize(hash) ⇒ DataHash
constructor
A new instance of DataHash.
- #key?(key) ⇒ Boolean
- #lookup(keys) ⇒ Object
- #set(keys, value) ⇒ Object
- #to_h ⇒ Object
- #to_json(*a) ⇒ Object
Constructor Details
#initialize(hash) ⇒ DataHash
Returns a new instance of DataHash.
27 28 29 |
# File 'spaceship/lib/spaceship/base.rb', line 27 def initialize(hash) @hash = hash || {} end |
Instance Method Details
#delete(key) ⇒ Object
48 49 50 |
# File 'spaceship/lib/spaceship/base.rb', line 48 def delete(key) @hash.delete(key) end |
#each(&block) ⇒ Object
61 62 63 |
# File 'spaceship/lib/spaceship/base.rb', line 61 def each(&block) @hash.each(&block) end |
#get(*keys) ⇒ Object Also known as: []
35 36 37 |
# File 'spaceship/lib/spaceship/base.rb', line 35 def get(*keys) lookup(keys) end |
#key?(key) ⇒ Boolean
31 32 33 |
# File 'spaceship/lib/spaceship/base.rb', line 31 def key?(key) @hash.key?(key) end |
#lookup(keys) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'spaceship/lib/spaceship/base.rb', line 52 def lookup(keys) head, *tail = *keys if tail.empty? @hash[head] else DataHash.new(@hash[head]).lookup(tail) end end |
#set(keys, value) ⇒ Object
41 42 43 44 45 46 |
# File 'spaceship/lib/spaceship/base.rb', line 41 def set(keys, value) raise "'keys' must be an array, got #{keys.class} instead" unless keys.kind_of?(Array) last = keys.pop ref = lookup(keys) || @hash ref[last] = value end |
#to_h ⇒ Object
74 75 76 |
# File 'spaceship/lib/spaceship/base.rb', line 74 def to_h @hash.dup end |