class Bond::Mission

Represents a completion rule, given a condition (:on) on which to match and an action (block or :action) with which to generate possible completions.

Constants

OBJECTS

Regular expressions which describe common objects for MethodMission and ObjectMission

OPERATORS

All known operator methods

Attributes

eval_binding[RW]

eval binding used across missions

action[R]

Generates array of possible completions and searches them if search is disabled. Any values that aren't strings are automatically converted with to_s.

matched[R]

A MatchData object generated from matching the user input with the condition.

on[R]

Regexp condition

place[R]

See {Bond#complete}'s :place.

Public Class Methods

create(options) click to toggle source

Handles creation of proper Mission class depending on the options passed.

# File lib/bond/mission.rb, line 18
def create(options)
  if options[:method] || options[:methods] then MethodMission.create(options)
  elsif options[:object]                   then ObjectMission.new(options)
  elsif options[:anywhere]                 then AnywhereMission.new(options)
  elsif options[:all_methods]              then MethodMission.new(options)
  elsif options[:all_operator_methods]     then OperatorMethodMission.new(options)
  else                                          new(options)
  end
end
current_eval(string, ebinding=Mission.eval_binding) click to toggle source

Calls eval with either the irb's current workspace binding or TOPLEVEL_BINDING.

# File lib/bond/mission.rb, line 29
def current_eval(string, ebinding=Mission.eval_binding)
  ebinding = ebinding.call if ebinding.is_a?(Proc)
  eval(string, ebinding)
end
new(options) click to toggle source

Takes same options as {Bond#complete}.

# File lib/bond/mission.rb, line 55
def initialize(options)
  raise InvalidMissionError, ":action" unless (options[:action] || respond_to?(:default_action, true))
  raise InvalidMissionError, ":on" unless (options[:on] && options[:on].is_a?(Regexp)) || respond_to?(:default_on, true)
  @action, @on = options[:action], options[:on]
  @place = options[:place] if options[:place]
  @name = options[:name] if options[:name]
  @search = options.has_key?(:search) ? options[:search] : Search.default_search
end

Public Instance Methods

after_match(input) click to toggle source

Stuff a mission needs to do after matching successfully, in preparation for #execute.

# File lib/bond/mission.rb, line 126
def after_match(input)
  create_input(input[/\S+$/])
end
call_action(input) click to toggle source

Calls the action to generate an array of possible completions.

# File lib/bond/mission.rb, line 95
def call_action(input)
  @action.respond_to?(:call) ? @action.call(input) : Rc.send(@action, input)
rescue StandardError, SyntaxError
  message = $!.is_a?(NoMethodError) && !@action.respond_to?(:call) &&
    !Rc.respond_to?(@action) ? "Completion action '#{@action}' doesn't exist." :
    "Failed during completion action '#{name}' with '#{$!.message}'."
  raise FailedMissionError.new(self), message
end
condition() click to toggle source

A regexp representing the condition under which a mission matches the input.

# File lib/bond/mission.rb, line 111
def condition
  self.class.const_defined?(:CONDITION) ? Regexp.new(self.class.const_get(:CONDITION)) : @on
end
do_match(input) click to toggle source

Method which must return non-nil for a mission to match.

# File lib/bond/mission.rb, line 121
def do_match(input)
  @matched = input.match(@on)
end
execute(input=@input) click to toggle source

Called when a mission has been chosen to autocomplete.

# File lib/bond/mission.rb, line 72
def execute(input=@input)
  completions = Array(call_action(input)).map {|e| e.to_s }
  completions = call_search(@search, input, completions) if @search
  if @completion_prefix
    # Everything up to last break char stays on the line.
    # Must ensure only chars after break are prefixed
    @completion_prefix = @completion_prefix[/([^#{Readline::DefaultBreakCharacters}]+)$/,1] || ''
    completions = completions.map {|e| @completion_prefix + e }
  end
  completions
end
match_message() click to toggle source

A message used to explains under what conditions a mission matched the user input. Useful for spying and debugging.

# File lib/bond/mission.rb, line 106
def match_message
  "Matches completion with condition #{condition.inspect}."
end
matches?(input) click to toggle source

Returns a boolean indicating if a mission matches the given Input and should be executed for completion.

# File lib/bond/mission.rb, line 65
def matches?(input)
  @matched = @input = @completion_prefix = nil
  (match = do_match(input)) && after_match(@line = input)
  !!match
end
name() click to toggle source

The name or generated #unique_id for a mission. Mostly for use with Bond#recomplete.

# File lib/bond/mission.rb, line 116
def name
  @name ? @name.to_s : unique_id
end

Private Instance Methods

condition_with_objects() click to toggle source
# File lib/bond/mission.rb, line 131
def condition_with_objects
  self.class.const_get(:CONDITION).sub('OBJECTS', self.class.const_get(:OBJECTS).join('|'))
end
create_input(input, options={}) click to toggle source
# File lib/bond/mission.rb, line 147
def create_input(input, options={})
  @input = Input.new(input, options.merge(:line => @line, :matched => @matched))
end
eval_object(obj) click to toggle source
# File lib/bond/mission.rb, line 135
def eval_object(obj)
  @evaled_object = self.class.current_eval(obj)
  true
rescue Exception
  raise FailedMissionError.new(self), "Match failed during eval of '#{obj}'." if Bond.config[:eval_debug]
  false
end
unique_id() click to toggle source
# File lib/bond/mission.rb, line 143
def unique_id
  @on.inspect
end