Class: Aristotle::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/aristotle/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/aristotle/command.rb', line 3

def action
  @action
end

#action_procObject (readonly)

Returns the value of attribute action_proc.



3
4
5
# File 'lib/aristotle/command.rb', line 3

def action_proc
  @action_proc
end

#conditionObject (readonly)

Returns the value of attribute condition.



3
4
5
# File 'lib/aristotle/command.rb', line 3

def condition
  @condition
end

#condition_procObject (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

Returns:

  • (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

Returns:

  • (Boolean)


43
44
45
# File 'lib/aristotle/command.rb', line 43

def has_action?
  !@action_proc.nil?
end

#has_condition?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/aristotle/command.rb', line 47

def has_condition?
  !@condition_proc.nil?
end