Skip to content

Commit

Permalink
moved object mission to it's own class
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed Jul 14, 2009
1 parent b31f8c7 commit 87452a5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
1 change: 1 addition & 0 deletions lib/bond.rb
Expand Up @@ -5,6 +5,7 @@
require 'bond/agent'
require 'bond/mission'
require 'bond/missions/method_mission'
require 'bond/missions/object_mission'

module Bond
extend self
Expand Down
36 changes: 10 additions & 26 deletions lib/bond/mission.rb
Expand Up @@ -6,6 +6,8 @@ class Mission
def self.create(options)
if options[:method]
Missions::MethodMission.new(options)
elsif options[:object]
Missions::ObjectMission.new(options)
else
new(options)
end
Expand All @@ -15,43 +17,29 @@ def self.create(options)
OPERATORS = ["%", "&", "*", "**", "+", "-", "/", "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>", "[]", "[]=", "^"]

def initialize(options)
raise InvalidMissionError unless (options[:action] || options[:object]) &&
(options[:on] || options[:default] || options[:object])
raise InvalidMissionError unless (options[:action] || respond_to?(:default_action)) &&
(options[:on] || options[:default])
raise InvalidMissionError if options[:on] && !options[:on].is_a?(Regexp)
@action = options[:action]
@condition = options[:on]
@default = options[:default] || false
@eval_binding = options[:eval_binding]
@search = (options[:search] == false) ? false : (respond_to?("#{options[:search]}_search") ? method("#{options[:search]}_search") :
method(:default_search))
if (@object = options[:object])
@object = /^#{Regexp.quote(@object.to_s)}$/ unless @object.is_a?(Regexp)
@condition = /^((\.?[^.]+)+)\.([^.]*)$/
end
end

def set_input(input, match)
@input = input[/\S+$/]
end

def matches?(input)
def handle_valid_match(input)
if (match = input.match(@condition))
set_input(input, match)
if @object
@evaled_object = begin eval("#{match[1]}", @eval_binding); rescue Exception; nil end
old_match = match
if @evaled_object && (match = @evaled_object.class.ancestors.any? {|e| e.to_s =~ @object })
@list_prefix = old_match[1] + "."
@input = old_match[3]
@input.instance_variable_set("@object", @evaled_object)
@input.instance_eval("def self.object; @object ; end")
@action ||= lambda {|e| default_object_action(e.object) }
else
match = false
end
end
end
if match
match
end

def matches?(input)
if (match = handle_valid_match(input))
@input.instance_variable_set("@matched", match)
@input.instance_eval("def self.matched; @matched ; end")
end
Expand All @@ -72,10 +60,6 @@ def execute(*args)
raise FailedExecutionError, error_message
end

def default_object_action(obj)
obj.methods - OPERATORS
end

def default_search(input, list)
list.grep(/^#{input}/)
end
Expand Down
29 changes: 29 additions & 0 deletions lib/bond/missions/object_mission.rb
@@ -0,0 +1,29 @@
class Bond::Missions::ObjectMission < Bond::Mission
def initialize(options={})
@object = options.delete(:object)
@object = /^#{Regexp.quote(@object.to_s)}$/ unless @object.is_a?(Regexp)
options[:on] = /^((\.?[^.]+)+)\.([^.]*)$/
@eval_binding = options[:eval_binding]
super
end

def handle_valid_match(input)
match = super
@evaled_object = begin eval("#{match[1]}", @eval_binding); rescue Exception; nil end
old_match = match
if @evaled_object && (match = @evaled_object.class.ancestors.any? {|e| e.to_s =~ @object })
@list_prefix = old_match[1] + "."
@input = old_match[3]
@input.instance_variable_set("@object", @evaled_object)
@input.instance_eval("def self.object; @object ; end")
@action ||= lambda {|e| default_action(e.object) }
else
match = false
end
match
end

def default_action(obj)
obj.methods - OPERATORS
end
end

0 comments on commit 87452a5

Please sign in to comment.