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
-
#missions ⇒ Object
readonly
The array of missions that will be searched when a completion occurs.
-
#weapon ⇒ Object
readonly
An agent’s best friend a.k.a.
Instance Method Summary collapse
-
#call(input, line_buffer = nil) ⇒ Object
This is where the action starts when a completion is initiated.
-
#complete(options = {}, &block) ⇒ Object
Creates a mission.
-
#default_mission ⇒ Object
Default mission used by agent.
- #find_mission(input) ⇒ Object
-
#initialize(options = {}) ⇒ Agent
constructor
A new instance of Agent.
-
#recomplete(options = {}, &block) ⇒ Object
Creates a mission and replaces the mission it matches if possible.
-
#reset ⇒ Object
Resets an agent’s missions.
-
#spy(input) ⇒ Object
Given a hypothetical user input, reports back what mission it would have found and executed.
Constructor Details
#initialize(options = {}) ⇒ Agent
Returns a new instance of Agent.
11 12 13 14 15 16 17 |
# File 'lib/bond/agent.rb', line 11 def initialize(={}) #@private setup_readline([:readline]) @default_mission_action = [:default_mission] if [:default_mission] Mission.eval_binding = [:eval_binding] if [:eval_binding] Search.default_search = [:default_search] || :normal @missions = [] end |
Instance Attribute Details
#missions ⇒ Object (readonly)
The array of missions that will be searched when a completion occurs.
7 8 9 |
# File 'lib/bond/agent.rb', line 7 def missions @missions end |
#weapon ⇒ Object (readonly)
An agent’s best friend a.k.a. the readline plugin.
9 10 11 |
# File 'lib/bond/agent.rb', line 9 def weapon @weapon end |
Instance Method Details
#call(input, line_buffer = nil) ⇒ Object
This is where the action starts when a completion is initiated. Optional line_buffer overrides line buffer from readline plugin.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bond/agent.rb', line 43 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.}") rescue completion_error "Failed internally with '#{$!.}'.", "Please report this issue with debug on: Bond.config[:debug] = true." end |
#complete(options = {}, &block) ⇒ Object
Creates a mission.
20 21 22 23 24 25 26 |
# File 'lib/bond/agent.rb', line 20 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 |
#default_mission ⇒ Object
Default mission used by agent. An instance of DefaultMission.
73 74 75 |
# File 'lib/bond/agent.rb', line 73 def default_mission @default_mission ||= DefaultMission.new(:action => @default_mission_action) end |
#find_mission(input) ⇒ Object
68 69 70 |
# File 'lib/bond/agent.rb', line 68 def find_mission(input) #@private @missions.find {|mission| mission.matches?(input) } end |
#recomplete(options = {}, &block) ⇒ Object
Creates a mission and replaces the mission it matches if possible.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/bond/agent.rb', line 29 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 |
#reset ⇒ Object
Resets an agent’s missions
78 79 80 |
# File 'lib/bond/agent.rb', line 78 def reset @missions = [] end |
#spy(input) ⇒ Object
Given a hypothetical user input, reports back what mission it would have found and executed.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bond/agent.rb', line 56 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 |