Class: Spaceship::Base::DataHash

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

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ DataHash

Returns a new instance of DataHash.


25
26
27
# File 'spaceship/lib/spaceship/base.rb', line 25

def initialize(hash)
  @hash = hash || {}
end

Instance Method Details

#delete(key) ⇒ Object


46
47
48
# File 'spaceship/lib/spaceship/base.rb', line 46

def delete(key)
  @hash.delete(key)
end

#each(&block) ⇒ Object


59
60
61
# File 'spaceship/lib/spaceship/base.rb', line 59

def each(&block)
  @hash.each(&block)
end

#get(*keys) ⇒ Object Also known as: []


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

def get(*keys)
  lookup(keys)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)

29
30
31
# File 'spaceship/lib/spaceship/base.rb', line 29

def key?(key)
  @hash.key?(key)
end

#lookup(keys) ⇒ Object


50
51
52
53
54
55
56
57
# File 'spaceship/lib/spaceship/base.rb', line 50

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

#set(keys, value) ⇒ Object


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

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_hObject


69
70
71
# File 'spaceship/lib/spaceship/base.rb', line 69

def to_h
  @hash.dup
end

#to_json(*a) ⇒ Object


63
64
65
66
67
# File 'spaceship/lib/spaceship/base.rb', line 63

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