Class: ChainReactor::Reaction
- Inherits:
-
Object
- Object
- ChainReactor::Reaction
- Defined in:
- lib/chain-reactor/reaction.rb
Overview
Represents a single reaction block, defined in the chain file with the ‘react_to’ method.
Instance Attribute Summary collapse
-
#options ⇒ Object
Options used by this reaction.
-
#previous_data ⇒ Object
The previous data set sent to this reaction, if executed before.
-
#previous_result ⇒ Object
The previous return value from this reaction, if executed before.
Instance Method Summary collapse
-
#execute(data_string) ⇒ Object
Executes the block of code after parsing the data string.
-
#initialize(options, block, logger) ⇒ Reaction
constructor
Create a new reaction, with the options and code block to run.
Constructor Details
#initialize(options, block, logger) ⇒ Reaction
Create a new reaction, with the options and code block to run.
27 28 29 30 31 32 |
# File 'lib/chain-reactor/reaction.rb', line 27 def initialize(,block,logger) @options = { :parser => :json, :required_keys => [], :keys_to_sym => true }.merge() @block = block @log = logger @log.debug { "Created reaction with options: #{}" } end |
Instance Attribute Details
#options ⇒ Object
Options used by this reaction.
24 25 26 |
# File 'lib/chain-reactor/reaction.rb', line 24 def @options end |
#previous_data ⇒ Object
The previous data set sent to this reaction, if executed before.
22 23 24 |
# File 'lib/chain-reactor/reaction.rb', line 22 def previous_data @previous_data end |
#previous_result ⇒ Object
The previous return value from this reaction, if executed before.
20 21 22 |
# File 'lib/chain-reactor/reaction.rb', line 20 def previous_result @previous_result end |
Instance Method Details
#execute(data_string) ⇒ Object
Executes the block of code after parsing the data string.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/chain-reactor/reaction.rb', line 35 def execute(data_string) parser = ParserFactory.get_parser(@options[:parser],@log) data = parser.parse(data_string,@options[:required_keys],@options[:keys_to_sym]) begin @previous_result = @block.call(data) @previous_data = data rescue StandardError => e raise ReactionError.new(e) end end |