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.


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

Returns:

  • (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_hObject


74
75
76
# File 'spaceship/lib/spaceship/base.rb', line 74

def to_h
  @hash.dup
end

#to_json(*a) ⇒ Object


65
66
67
68
69
70
71
72
# File 'spaceship/lib/spaceship/base.rb', line 65

def to_json(*a)
  h = @hash.dup
  h.delete(:application)
  h.to_json(*a)
rescue JSON::GeneratorError => e
  puts("Failed to jsonify #{h} (#{a})") if Spaceship::Globals.verbose?
  raise e
end