Class: LifecycleVM::OpBase
- Inherits:
-
Object
- Object
- LifecycleVM::OpBase
- Defined in:
- lib/lifecycle_vm/op_base.rb
Overview
Base class for all ops in a vm. An op may read and write to vm memory by declaring reads and writes using the DSL. The lifecycle of an op is initialize -> call -> validate, and all three methods will always be called. All reads and logger will be provided before initialize is called.
An op may indicate that it encountered an error or otherwise failed by calling #error. If an op has any errors, no writes will be persisted to the vm memory.
Defined Under Namespace
Classes: InvalidAttr
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
-
.call(memory) ⇒ Object
Execute the current op with the gien vm memory.
-
.reads(*attrs) ⇒ Object
Read one or more values out of vm memory and provide them as readable attributes.
-
.writes(*attrs) ⇒ Object
Allow writing one or more values to vm memory and provide them as writeable attributes.
Instance Method Summary collapse
- #call ⇒ Object
-
#error(field, message) ⇒ Object
Declare that an error has occurred.
-
#errors? ⇒ Boolean
Did any errors occur in the execution of this op?.
- #validate ⇒ Object
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
118 119 120 |
# File 'lib/lifecycle_vm/op_base.rb', line 118 def errors @errors end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
118 119 120 |
# File 'lib/lifecycle_vm/op_base.rb', line 118 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 if executed without any errors will write all declared writes to memory.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/lifecycle_vm/op_base.rb', line 83 def self.call(memory) obj = allocate @reads ||= {} @writes ||= {} @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 @writes.each do |(attribute, setter)| raise InvalidAttr.new(self, attribute) unless memory.respond_to?(setter) end obj.instance_variable_set(:"@logger", memory.logger) obj.instance_variable_set(:"@errors", nil) obj.send(:initialize) obj.send(:call) obj.send(:validate) unless obj.errors? @writes.each do |(attribute, setter)| memory.send(setter, obj.send(attribute)) end end obj end |
.reads(*attrs) ⇒ Object
Read one or more values out of vm memory and provide them as readable attributes
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lifecycle_vm/op_base.rb', line 43 def self.reads(*attrs) @reads ||= {} @writes ||= {} attrs.each do |attribute| attribute = attribute.to_sym attr_reader attribute unless @writes.key?(attribute) @reads[attribute] = :"@#{attribute}" end @reads end |
.writes(*attrs) ⇒ Object
You may not write to any slot in LifecycleVM::Memory::PROTECTED
Allow writing one or more values to vm memory and provide them as writeable attributes
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/lifecycle_vm/op_base.rb', line 60 def self.writes(*attrs) @reads ||= {} @writes ||= {} attrs.each do |attribute| attribute = attribute.to_sym raise InvalidAttr.new(self, attribute) if LifecycleVM::Memory::PROTECTED.include?(attribute) if @reads.key?(attribute) attr_writer attribute else attr_accessor attribute end @writes[attribute] = :"#{attribute}=" end @writes end |
Instance Method Details
#call ⇒ Object
115 |
# File 'lib/lifecycle_vm/op_base.rb', line 115 def call; end |
#error(field, message) ⇒ Object
Declare that an error has occurred.
124 125 126 127 128 |
# File 'lib/lifecycle_vm/op_base.rb', line 124 def error(field, ) @errors ||= {} @errors[field] ||= [] @errors[field] << end |
#errors? ⇒ Boolean
Did any errors occur in the execution of this op?
131 132 133 |
# File 'lib/lifecycle_vm/op_base.rb', line 131 def errors? !(@errors.nil? || @errors.empty?) end |
#validate ⇒ Object
116 |
# File 'lib/lifecycle_vm/op_base.rb', line 116 def validate; end |