Class: Rubot::DSL::BehaviorContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rubot/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, accepted_args, fire, sensors, robot, adapter, given_args = {}) ⇒ BehaviorContext

Returns a new instance of BehaviorContext.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubot/dsl.rb', line 59

def initialize(name, accepted_args, fire, sensors, robot, adapter, given_args={})
  @name = name
  @accepted_args = accepted_args
  @fire = fire
  @sensors = sensors
  @robot = robot
  @given_args = given_args
  @behavior = Rubot::Adapters.const_get(adapter)::Behavior.new name.to_s
  # Have the behavior execute the fire proc in context of this object.
  @behavior.set_fire_proc Proc.new { self.instance_eval(&@fire) }
  @behavior.set_sensors @sensors
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Return the named sensor or argument.



73
74
75
76
77
78
79
80
81
# File 'lib/rubot/dsl.rb', line 73

def method_missing(sym, *args)
  if @sensors.include? sym
    @behavior.get_sensor(sym)
  elsif @accepted_args.include? sym
    @given_args[sym]
  else
    super
  end
end

Instance Attribute Details

#behaviorObject (readonly)

Returns the value of attribute behavior.



58
59
60
# File 'lib/rubot/dsl.rb', line 58

def behavior
  @behavior
end

#robotObject (readonly)

Returns the value of attribute robot.



58
59
60
# File 'lib/rubot/dsl.rb', line 58

def robot
  @robot
end

Instance Method Details

#desiredObject



83
84
85
# File 'lib/rubot/dsl.rb', line 83

def desired
  @behavior.get_desired
end