Class: Aristotle::Command
- Inherits:
-
Object
- Object
- Aristotle::Command
- Defined in:
- lib/aristotle/command.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#action_proc ⇒ Object
readonly
Returns the value of attribute action_proc.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#condition_proc ⇒ Object
readonly
Returns the value of attribute condition_proc.
Instance Method Summary collapse
- #condition_passes_with?(object) ⇒ Boolean
- #do_action_with(object) ⇒ Object
- #has_action? ⇒ Boolean
- #has_condition? ⇒ Boolean
-
#initialize(line, conditions, actions) ⇒ Command
constructor
A new instance of Command.
Constructor Details
#initialize(line, conditions, actions) ⇒ Command
Returns a new instance of Command.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/aristotle/command.rb', line 5 def initialize(line, conditions, actions) @action, @condition = line.split(' if ', 2).map(&:strip) raise 'Badly formatted line' if @action == '' || @condition == '' conditions.each do |condition_regexp, condition_proc| match_data = condition_regexp.match(@condition) if match_data @condition_proc = condition_proc @condition_attributes = match_data.to_a[1..-1] end end actions.each do |action_regexp, action_proc| match_data = action_regexp.match(@action) if match_data @action_proc = action_proc @action_attributes = match_data.to_a[1..-1] end end end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/aristotle/command.rb', line 3 def action @action end |
#action_proc ⇒ Object (readonly)
Returns the value of attribute action_proc.
3 4 5 |
# File 'lib/aristotle/command.rb', line 3 def action_proc @action_proc end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
3 4 5 |
# File 'lib/aristotle/command.rb', line 3 def condition @condition end |
#condition_proc ⇒ Object (readonly)
Returns the value of attribute condition_proc.
3 4 5 |
# File 'lib/aristotle/command.rb', line 3 def condition_proc @condition_proc end |
Instance Method Details
#condition_passes_with?(object) ⇒ Boolean
35 36 37 38 39 40 41 |
# File 'lib/aristotle/command.rb', line 35 def condition_passes_with?(object) if @condition_proc @condition_proc.call(object, *@condition_attributes) else raise "Condition not found: #{@condition}" end end |
#do_action_with(object) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/aristotle/command.rb', line 27 def do_action_with(object) if @action_proc @action_proc.call(object, *@action_attributes) else raise "Action not found: #{@action}" end end |
#has_action? ⇒ Boolean
43 44 45 |
# File 'lib/aristotle/command.rb', line 43 def has_action? !@action_proc.nil? end |
#has_condition? ⇒ Boolean
47 48 49 |
# File 'lib/aristotle/command.rb', line 47 def has_condition? !@condition_proc.nil? end |