Class: Zen::Service::Plugins::Executable::State

Inherits:
Object
  • Object
show all
Defined in:
lib/zen/service/plugins/executable.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ State

Returns a new instance of State.



26
27
28
# File 'lib/zen/service/plugins/executable.rb', line 26

def initialize(values = {})
  @values = values
end

Class Method Details

.add_prop(*props) ⇒ Object



15
16
17
18
# File 'lib/zen/service/plugins/executable.rb', line 15

def self.add_prop(*props)
  prop_names.push(*props)
  props.each { |prop| def_prop_accessor(prop) }
end

.def_prop_accessor(name) ⇒ Object



20
21
22
23
24
# File 'lib/zen/service/plugins/executable.rb', line 20

def self.def_prop_accessor(name)
  define_method(name) { @values[name] }
  define_method("#{name}=") { |value| @values[name] = value }
  define_method("has_#{name}?") { @values.key?(name) }
end

.prop_namesObject



11
12
13
# File 'lib/zen/service/plugins/executable.rb', line 11

def self.prop_names
  @prop_names ||= []
end

Instance Method Details

#clear!Object



30
31
32
# File 'lib/zen/service/plugins/executable.rb', line 30

def clear!
  @values.clear
end

#prop_namesObject



34
35
36
# File 'lib/zen/service/plugins/executable.rb', line 34

def prop_names
  self.class.prop_names
end

#replace(other) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zen/service/plugins/executable.rb', line 38

def replace(other)
  missing_props = prop_names - other.prop_names

  unless missing_props.empty?
    raise ArgumentError, "cannot accept execution state #{other} due to missing props: #{missing_props}"
  end

  prop_names.each do |prop|
    @values[prop] = other.public_send(prop)
  end
end