Class: Bond::Agent

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options={}) #@private
  setup_readline(options[:readline])
  @default_mission_action = options[:default_mission] if options[:default_mission]
  Mission.eval_binding = options[:eval_binding] if options[:eval_binding]
  Search.default_search = options[:default_search] || :normal
  @missions = []
end

Instance Attribute Details

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

#weaponObject (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.message, "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

#complete(options = {}, &block) ⇒ Object

Creates a mission.



20
21
22
23
24
25
26
# File 'lib/bond/agent.rb', line 20

def complete(options={}, &block)
  if (mission = create_mission(options, &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_missionObject

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(options={}, &block)
  if (mission = create_mission(options, &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

#resetObject

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.match_message, "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.match_message, e.message,
    "Matches for #{e.mission.condition.inspect} are #{e.mission.matched.to_a.inspect}"
end