Class: LifecycleVM::CondBase

Inherits:
Object
  • Object
show all
Defined in:
lib/lifecycle_vm/cond_base.rb

Overview

Base class for all conditionals in a vm. A conditional may read from vm emory by declare reads using the DSL. The lifecycle of a conditional is initialize -> call, and both methods will always be called. All reads and logger will be provided before initialize is called.

A conditional may not write to vm memory, and must not error.

Defined Under Namespace

Classes: InvalidAttr

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



65
66
67
# File 'lib/lifecycle_vm/cond_base.rb', line 65

def logger
  @logger
end

Class Method Details

.call(memory) ⇒ Object

Execute the current op with the gien vm memory. This will read out all declared reads from memory and return the result of #call.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lifecycle_vm/cond_base.rb', line 48

def self.call(memory)
  obj = allocate

  @reads ||= {}

  @reads.each do |(attribute, ivar)|
    raise InvalidAttr.new(self, attribute) unless memory.respond_to?(attribute)

    obj.instance_variable_set(ivar, memory.send(attribute).clone)
  end

  obj.instance_variable_set(:"@logger", memory.logger)

  obj.send(:initialize)
  obj.send(:call)
end

.reads(*attrs) ⇒ Object

Read one or more values out of vm memory and provide them as readable attributes



40
41
42
43
44
# File 'lib/lifecycle_vm/cond_base.rb', line 40

def self.reads(*attrs)
  @reads ||= {}
  @reads.merge!(Hash[attrs.map { |attribute| [attribute, :"@#{attribute}"] }])
  attr_reader(*attrs)
end

Instance Method Details

#callObject



67
# File 'lib/lifecycle_vm/cond_base.rb', line 67

def call; end