Class: Bond::Agent
- Inherits:
-
Object
- Object
- Bond::Agent
- Defined in:
- lib/bond/agent.rb
Overview
Every time a completion is attempted, the Agent searches its missions for the first one that matches the user input. Using either the found mission or Agent.default_mission, the Agent executes the mission’s action.
Instance Attribute Summary (collapse)
-
- (Object) missions
readonly
The array of missions that will be searched when a completion occurs.
-
- (Object) weapon
readonly
An agent’s best friend a.k.a.
Instance Method Summary (collapse)
-
- (Object) call(input, line_buffer = nil)
This is where the action starts when a completion is initiated.
-
- (Object) complete(options = {}, &block)
Creates a mission.
-
- (Object) default_mission
Default mission used by agent.
-
- (Object) recomplete(options = {}, &block)
Creates a mission and replaces the mission it matches if possible.
-
- (Object) reset
Resets an agent’s missions.
-
- (Object) spy(input)
Given a hypothetical user input, reports back what mission it would have found and executed.
Instance Attribute Details
- (Object) missions (readonly)
The array of missions that will be searched when a completion occurs.
6 7 8 |
# File 'lib/bond/agent.rb', line 6 def missions @missions end |
- (Object) weapon (readonly)
An agent’s best friend a.k.a. the readline plugin.
8 9 10 |
# File 'lib/bond/agent.rb', line 8 def weapon @weapon end |
Instance Method Details
- (Object) call(input, line_buffer = nil)
This is where the action starts when a completion is initiated. Optional line_buffer overrides line buffer from readline plugin.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bond/agent.rb', line 42 def call(input, line_buffer=nil) mission_input = line_buffer || @weapon.line_buffer mission_input = $1 if mission_input !~ /#{Regexp.escape(input)}$/ && mission_input =~ /^(.*#{Regexp.escape(input)})/ (mission = find_mission(mission_input)) ? mission.execute : default_mission.execute(Input.new(input)) rescue FailedMissionError=>e completion_error(e., "Completion Info: #{e.mission.match_message}") rescue completion_error "Failed internally with '#{$!.message}'.", "Please report this issue with debug on: Bond.config[:debug] = true." end |
- (Object) complete(options = {}, &block)
Creates a mission.
19 20 21 22 23 24 25 |
# File 'lib/bond/agent.rb', line 19 def complete(={}, &block) if (mission = create_mission(, &block)).is_a?(Mission) mission.place.is_a?(Integer) ? @missions.insert(mission.place - 1, mission).compact! : @missions << mission sort_last_missions end mission end |
- (Object) default_mission
Default mission used by agent. An instance of DefaultMission.
71 72 73 |
# File 'lib/bond/agent.rb', line 71 def default_mission @default_mission ||= DefaultMission.new(:action=>@default_mission_action) end |
- (Object) recomplete(options = {}, &block)
Creates a mission and replaces the mission it matches if possible.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bond/agent.rb', line 28 def recomplete(={}, &block) if (mission = create_mission(, &block)).is_a?(Mission) if (existing_mission = @missions.find {|e| e.name == mission.name }) @missions[@missions.index(existing_mission)] = mission sort_last_missions else return "No existing mission found to recomplete." end end mission end |
- (Object) reset
Resets an agent’s missions
76 77 78 |
# File 'lib/bond/agent.rb', line 76 def reset @missions = [] end |
- (Object) spy(input)
Given a hypothetical user input, reports back what mission it would have found and executed.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bond/agent.rb', line 54 def spy(input) if (mission = find_mission(input)) puts mission., "Possible completions: #{mission.execute.inspect}", "Matches for #{mission.condition.inspect} are #{mission.matched.to_a.inspect}" else puts "Doesn't match a completion." end rescue FailedMissionError=>e puts e.mission., e., "Matches for #{e.mission.condition.inspect} are #{e.mission.matched.to_a.inspect}" end |