Class: Goat::ComponentSkeleton

Inherits:
Object
  • Object
show all
Defined in:
lib/goat/common.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ ComponentSkeleton

Returns a new instance of ComponentSkeleton.



25
26
27
28
29
30
# File 'lib/goat/common.rb', line 25

def initialize(h={})
  ensure_keys(h,
    :required => %w{pgid class id live_spec live_state dom}, :optional => []
  )
  @hash = h
end

Class Method Details

.from_hash(h) ⇒ Object



43
44
45
46
47
# File 'lib/goat/common.rb', line 43

def self.from_hash(h)
  hash = h.clone
  hash['dom'] = Marshal.load(h['dom'])
  self.new(hash)
end

Instance Method Details

#clsObject

in which we fail at DRY:



33
# File 'lib/goat/common.rb', line 33

def cls; @hash['class']; end

#domObject



38
# File 'lib/goat/common.rb', line 38

def dom; @hash['dom']; end

#dom=(d) ⇒ Object



40
# File 'lib/goat/common.rb', line 40

def dom=(d); @hash['dom'] = d; end

#ensure_keys(h, spec) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/goat/common.rb', line 14

def ensure_keys(h, spec)
  hash = h.clone
  spec[:required].each do |k|
    raise "#{k} not present" unless h.include?(k)
    hash.delete(k)
  end
  spec[:optional].each {|k| hash.delete(k)}

  raise "Unknown keys supplied: #{hash.keys.join(', ')}" unless hash.keys.empty?
end

#idObject



35
# File 'lib/goat/common.rb', line 35

def id; @hash['id']; end

#live_specObject



36
# File 'lib/goat/common.rb', line 36

def live_spec; @hash['live_spec']; end

#live_stateObject



37
# File 'lib/goat/common.rb', line 37

def live_state; @hash['live_state']; end

#live_state=(s) ⇒ Object



41
# File 'lib/goat/common.rb', line 41

def live_state=(s); @hash['live_state'] = s; end

#pgidObject



34
# File 'lib/goat/common.rb', line 34

def pgid; @hash['pgid']; end

#to_hashObject



49
50
51
52
53
# File 'lib/goat/common.rb', line 49

def to_hash
  hash = @hash.clone
  hash['dom'] = Marshal.dump(@hash['dom'])
  hash
end