Sha256: 73021157330ea1ac2300a589be4218ff7b9637d9e11dd13fe03fb2c717646e80

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

# Represents a completion mission specified by :object in Bond.complete. Unlike other missions, this
# one needs to both match the mission condition and have the current object being completed have
# an ancestor specified by :object.
class Bond::Missions::ObjectMission < Bond::Mission
  #:stopdoc:
  attr_reader :object_condition

  def initialize(options={})
    @object_condition = options.delete(:object)
    @object_condition = /^#{Regexp.escape(@object_condition.to_s)}$/ unless @object_condition.is_a?(Regexp)
    options[:on] ||= /(\S+|[^.]+)\.([^.\s]*)$/
    @eval_binding = options[:eval_binding]
    super
  end

  def unique_id
    "#{@object_condition.inspect}+#{@condition.inspect}"
  end

  def handle_valid_match(input)
    if (match = super)
      begin
        eval_object(match)
      rescue Exception
        return false
      end
      if (match = @evaled_object.class.ancestors.any? {|e| e.to_s =~ @object_condition })
        @completion_prefix = @matched[1] + "."
        @input = @matched[2]
        @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
    end
    match
  end

  def eval_object(match)
    @matched = match
    @evaled_object = self.class.current_eval(match[1], @eval_binding)
  end

  def default_action(obj)
    obj.methods.map {|e| e.to_s} - OPERATORS
  end
  #:startdoc:
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
cldwalker-bond-0.1.4 lib/bond/missions/object_mission.rb
bond-0.1.4 lib/bond/missions/object_mission.rb