Skip to content

Commit

Permalink
added default search config, default missions also use default searching
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed Jul 21, 2009
1 parent 1f9b63d commit b5f729b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 37 deletions.
1 change: 1 addition & 0 deletions lib/bond.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def reset
# Defaults to Bond::Readline. Note that a plugin doesn't imply use with irb. Irb is joined to the hip with Readline.
# [:default_mission] A proc to be used as the default completion proc when no completions match or one fails. When in irb with completion
# enabled, uses irb completion. Otherwise defaults to a proc with an empty completion list.
# [:default_search] A symbol representing the default search to be used in completions. See Bond.complete's :search option for valid values.
# [:eval_binding] Specifies a binding to be used with Bond::Missions::ObjectMission. When in irb, defaults to irb's main binding. Otherwise
# defaults to TOPLEVEL_BINDING.
# [:debug] Boolean to print unexpected errors when autocompletion fails. Default is false.
Expand Down
1 change: 1 addition & 0 deletions lib/bond/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(options={}) #:nodoc:
extend(options[:readline_plugin])
@default_mission_action = options[:default_mission] if options[:default_mission]
@eval_binding = options[:eval_binding] if options[:eval_binding]
Mission.default_search = options[:default_search] if options[:default_search]
setup
@missions = []
end
Expand Down
7 changes: 4 additions & 3 deletions lib/bond/completion.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# An enhanced irb/completion with some handy argument completions

Bond.debrief(:default_search=>:underscore) unless Bond.config[:default_search]
Bond.debrief(:default_mission=>:default) unless Bond.config[:default_mission]
Bond.complete(:method=>/system|`/, :action=>:shell_commands)
Bond.complete(:method=>'require', :action=>:method_require, :search=>false)

# irb/completion reproduced
Bond.complete(:object=>"Object", :search=>:underscore)
Bond.complete(:on=>/([^.\s]+)\.([^.]*)$/, :object=>"Object", :search=>:underscore)
Bond.complete(:object=>"Object")
Bond.complete(:on=>/([^.\s]+)\.([^.]*)$/, :object=>"Object")
Bond.complete(:on=>/(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/, :action=>:constants, :search=>false)
Bond.complete(:on=>/::([A-Z][^:\.\(]*)$/, :search=>false) {|e|
Object.constants.grep(/^#{Regexp.escape(e.matched[1])}/).collect{|f| "::" + f}
Expand Down
68 changes: 37 additions & 31 deletions lib/bond/mission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,37 @@ class Missions; end
class Mission
include Search

# Handles creation of proper Mission class depending on the options passed.
def self.create(options)
if options[:method]
Missions::MethodMission.new(options)
elsif options[:object]
Missions::ObjectMission.new(options)
else
new(options)
class<<self
# default search used across missions
attr_accessor :default_search
# Handles creation of proper Mission class depending on the options passed.
def create(options)
if options[:method]
Missions::MethodMission.new(options)
elsif options[:object]
Missions::ObjectMission.new(options)
else
new(options)
end
end
#:stopdoc:
def action_object
@action_object ||= Object.new.extend(Actions)
end
end

def self.action_object
@action_object ||= Object.new.extend(Actions)
end
def current_eval(string, eval_binding=nil)
eval_binding ||= default_eval_binding
eval(string, eval_binding)
end

def self.current_eval(string, eval_binding=nil)
eval_binding ||= default_eval_binding
eval(string, eval_binding)
end
def default_eval_binding
Object.const_defined?(:IRB) ? IRB.CurrentContext.workspace.binding : ::TOPLEVEL_BINDING
end

def self.default_eval_binding
Object.const_defined?(:IRB) ? IRB.CurrentContext.workspace.binding : ::TOPLEVEL_BINDING
def default_search
@default_search ||= :default
end
#:startdoc:
end

attr_reader :action, :condition
Expand All @@ -49,8 +58,8 @@ def initialize(options)
self.class.action_object.method(options[:action]) : options[:action]
raise InvalidMissionActionError if @action && !@action.respond_to?(:call)
@condition = options[:on]
@search = options.has_key?(:search) ? options[:search] : method(:default_search)
@search = method("#{options[:search]}_search") if respond_to?("#{options[:search]}_search")
@search = options.has_key?(:search) ? options[:search] : Mission.default_search
@search = method("#{@search}_search") unless @search.is_a?(Proc) || @search == false
end

# Returns a boolean indicating if a mission matches the given input.
Expand All @@ -64,18 +73,15 @@ def matches?(input)
end

# Called when a mission has been chosen to autocomplete.
def execute(*args)
if args.empty?
list = (@action.call(@input) || []).map {|e| e.to_s }
list = @search ? @search.call(@input || '', list) : list
if @list_prefix
@list_prefix = @list_prefix.split(Regexp.union(*Readline::DefaultBreakCharacters.split('')))[-1]
list = list.map {|e| @list_prefix + e }
end
list
else
@action.call(*args)
def execute(input=@input)
completions = @action.call(input)
completions = (completions || []).map {|e| e.to_s }
completions = @search.call(input || '', completions) if @search
if @completion_prefix
@completion_prefix = @completion_prefix.split(Regexp.union(*Readline::DefaultBreakCharacters.split('')))[-1]
completions = completions.map {|e| @completion_prefix + e }
end
completions
rescue
error_message = "Mission action failed to execute properly. Check your mission action with pattern #{@condition.inspect}.\n" +
"Failed with error: #{$!.message}"
Expand Down
2 changes: 1 addition & 1 deletion lib/bond/missions/object_mission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def handle_valid_match(input)
return false
end
if (match = @evaled_object.class.ancestors.any? {|e| e.to_s =~ @object_condition })
@list_prefix = @matched[1] + "."
@completion_prefix = @matched[1] + "."
@input = @matched[2]
@input.instance_variable_set("@object", @evaled_object)
@input.instance_eval("def self.object; @object ; end")
Expand Down
13 changes: 11 additions & 2 deletions test/bond_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ class BondTest < Test::Unit::TestCase
end

test "sets default mission" do
default_mission = lambda {}
default_mission = lambda { %w{1 2 3}}
Bond.reset
Bond.debrief :default_mission=>default_mission, :readline_plugin=>valid_readline_plugin
Bond.agent.default_mission.action.should == default_mission
tabtab('1').should == ['1']
end

test "sets default search" do
Bond.reset
Bond.debrief :default_search=>:underscore
complete(:method=>'blah') { %w{all_quiet on_the western_front}}
tabtab('blah a-q').should == ["all_quiet"]
Bond.reset
end
end

Expand Down

0 comments on commit b5f729b

Please sign in to comment.