Class: Spaceship::Base::DataHash

Inherits:
Object
  • Object
show all
Defined in:
lib/spaceship/base.rb

Instance Method Summary collapse

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



40
41
42
43
44
45
46
47
# File 'lib/spaceship/base.rb', line 40

def lookup(keys)
  head, *tail = *keys
  if tail.empty?
    @hash[head]
  else
    DataHash.new(@hash[head]).lookup(tail)
  end
end

#set(keys, value) ⇒ Object



33
34
35
36
37
38
# File 'lib/spaceship/base.rb', line 33

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_json(*a) ⇒ Object



49
50
51
52
53
# File 'lib/spaceship/base.rb', line 49

def to_json(*a)
  h = @hash.dup
  h.delete(:application)
  h.to_json(*a)
end