Class: Bond::Mission
- Inherits:
-
Object
- Object
- Bond::Mission
- Defined in:
- lib/bond/mission.rb
Overview
Represents a completion rule, given a condition (:on) on which to match and an action (block or :action) with which to generate possible completions.
Direct Known Subclasses
AnywhereMission, DefaultMission, MethodMission, ObjectMission
Constant Summary collapse
- OPERATORS =
All known operator methods
%w{% & * ** + - / < << <= <=> == === =~ > >= >> [] []= ^ | ~ ! != !~}
- OBJECTS =
Regular expressions which describe common objects for MethodMission and ObjectMission
%w<\([^\)]*\) '[^']*' "[^"]*" \/[^\/]*\/> + %w<(?:%q|%r|%Q|%w|%s|%)?\[[^\]]*\] (?:proc|lambda|%q|%r|%Q|%w|%s|%)?\s*\{[^\}]*\}>
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Generates array of possible completions and searches them if search is disabled.
-
#matched ⇒ Object
readonly
A MatchData object generated from matching the user input with the condition.
-
#on ⇒ Object
readonly
Regexp condition.
-
#place ⇒ Object
readonly
See #complete‘s :place.
Class Method Summary collapse
-
.create(options) ⇒ Object
Handles creation of proper Mission class depending on the options passed.
-
.current_eval(string, ebinding = Mission.eval_binding) ⇒ Object
Calls eval with either the irb’s current workspace binding or TOPLEVEL_BINDING.
Instance Method Summary collapse
-
#after_match(input) ⇒ Object
Stuff a mission needs to do after matching successfully, in preparation for Mission.execute.
-
#call_action(input) ⇒ Object
Calls the action to generate an array of possible completions.
-
#call_search(search, input, list) ⇒ Object
Searches possible completions from the action which match the input.
-
#condition ⇒ Object
A regexp representing the condition under which a mission matches the input.
-
#do_match(input) ⇒ Object
Method which must return non-nil for a mission to match.
-
#execute(input = @input) ⇒ Object
Called when a mission has been chosen to autocomplete.
-
#initialize(options) ⇒ Mission
constructor
Takes same options as #complete.
-
#match_message ⇒ Object
A message used to explains under what conditions a mission matched the user input.
-
#matches?(input) ⇒ Boolean
Returns a boolean indicating if a mission matches the given Input and should be executed for completion.
-
#name ⇒ Object
The name or generated unique_id for a mission.
Constructor Details
#initialize(options) ⇒ Mission
Takes same options as Bond#complete.
55 56 57 58 59 60 61 62 |
# File 'lib/bond/mission.rb', line 55 def initialize() raise InvalidMissionError, ":action" unless ([:action] || respond_to?(:default_action, true)) raise InvalidMissionError, ":on" unless ([:on] && [:on].is_a?(Regexp)) || respond_to?(:default_on, true) @action, @on = [:action], [:on] @place = [:place] if [:place] @name = [:name] if [:name] @search = .has_key?(:search) ? [:search] : Search.default_search end |
Class Attribute Details
.eval_binding ⇒ Object
16 17 18 |
# File 'lib/bond/mission.rb', line 16 def eval_binding @eval_binding end |
Instance Attribute Details
#action ⇒ Object (readonly)
Generates array of possible completions and searches them if search is disabled. Any values that aren’t strings are automatically converted with to_s.
47 48 49 |
# File 'lib/bond/mission.rb', line 47 def action @action end |
#matched ⇒ Object (readonly)
A MatchData object generated from matching the user input with the condition.
51 52 53 |
# File 'lib/bond/mission.rb', line 51 def matched @matched end |
#on ⇒ Object (readonly)
Regexp condition
53 54 55 |
# File 'lib/bond/mission.rb', line 53 def on @on end |
#place ⇒ Object (readonly)
See Bond#complete‘s :place.
49 50 51 |
# File 'lib/bond/mission.rb', line 49 def place @place end |
Class Method Details
.create(options) ⇒ Object
Handles creation of proper Mission class depending on the options passed.
18 19 20 21 22 23 24 25 26 |
# File 'lib/bond/mission.rb', line 18 def create() if [:method] || [:methods] then MethodMission.create() elsif [:object] then ObjectMission.new() elsif [:anywhere] then AnywhereMission.new() elsif [:all_methods] then MethodMission.new() elsif [:all_operator_methods] then OperatorMethodMission.new() else new() end end |
.current_eval(string, ebinding = Mission.eval_binding) ⇒ Object
Calls eval with either the irb’s current workspace binding or TOPLEVEL_BINDING.
29 30 31 32 |
# 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 |
Instance Method Details
#after_match(input) ⇒ Object
Stuff a mission needs to do after matching successfully, in preparation for Mission.execute.
126 127 128 |
# File 'lib/bond/mission.rb', line 126 def after_match(input) create_input(input[/\S+$/]) end |
#call_action(input) ⇒ Object
Calls the action to generate an array of possible completions.
95 96 97 98 99 100 101 102 |
# 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 = $!.is_a?(NoMethodError) && !@action.respond_to?(:call) && !Rc.respond_to?(@action) ? "Completion action '#{@action}' doesn't exist." : "Failed during completion action '#{name}' with '#{$!.}'." raise FailedMissionError.new(self), end |
#call_search(search, input, list) ⇒ Object
Searches possible completions from the action which match the input.
85 86 87 88 89 90 91 92 |
# File 'lib/bond/mission.rb', line 85 def call_search(search, input, list) Rc.send("#{search}_search", input || '', list) rescue = $!.is_a?(NoMethodError) && !Rc.respond_to?("#{search}_search") ? "Completion search '#{search}' doesn't exist." : "Failed during completion search with '#{$!.}'." raise FailedMissionError.new(self), end |
#condition ⇒ Object
A regexp representing the condition under which a mission matches the input.
111 112 113 |
# 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) ⇒ Object
Method which must return non-nil for a mission to match.
121 122 123 |
# File 'lib/bond/mission.rb', line 121 def do_match(input) @matched = input.match(@on) end |
#execute(input = @input) ⇒ Object
Called when a mission has been chosen to autocomplete.
72 73 74 75 76 77 78 79 80 81 82 |
# 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 ⇒ Object
A message used to explains under what conditions a mission matched the user input. Useful for spying and debugging.
106 107 108 |
# File 'lib/bond/mission.rb', line 106 def "Matches completion with condition #{condition.inspect}." end |
#matches?(input) ⇒ Boolean
Returns a boolean indicating if a mission matches the given Input and should be executed for completion.
65 66 67 68 69 |
# 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 ⇒ Object
The name or generated unique_id for a mission. Mostly for use with Bond.recomplete.
116 117 118 |
# File 'lib/bond/mission.rb', line 116 def name @name ? @name.to_s : unique_id end |