class GhostDog::Responder

Attributes

matcher[R]
options[R]
responding_block[R]

Public Class Methods

from(matcher, options, block) click to toggle source
# File lib/ghost_dog/responder.rb, line 35
def self.from(matcher, options, block)
  raise ArgumentError, "Must specify a block to create a ghost_method" unless block

  if matcher.nil?
    unless options.empty?
      raise ArgumentError, "You cannot specify creation options if you're \
          using the shorthand...specify those options in the DSL instead"
    end

    using_dsl(&block)
  else
    using_dsl do
      options.each {|k, v| send(k, v) }
      match_with(matcher)
      respond_with(&block)
    end
  end
end
new(matcher, options, responding_block) click to toggle source
# File lib/ghost_dog/responder.rb, line 8
def initialize(matcher, options, responding_block)
  @matcher = matcher
  @options = options
  @responding_block = responding_block
end

Private Class Methods

using_dsl(&block) click to toggle source
# File lib/ghost_dog/responder.rb, line 60
def self.using_dsl(&block)
  Responder::DSL.new.tap do |dsl|
    dsl.instance_eval(&block)
  end.to_responder
end

Public Instance Methods

call(instance, klass, method, passed_args, passed_block) click to toggle source
# File lib/ghost_dog/responder.rb, line 19
def call(instance, klass, method, passed_args, passed_block)
  match_result = Array[matches(instance, method)].flatten(1)

  if create_method?
    klass.class_exec(responding_block) do |respond_with|
      define_method(method) do |*args, &block|
        instance_exec(*(match_result + args), &respond_with)
      end
    end

    instance.send(method, *passed_args, &passed_block)
  else
    instance.instance_exec(*(match_result + passed_args), &responding_block)
  end
end
matches(receiver, method) click to toggle source
# File lib/ghost_dog/responder.rb, line 14
def matches(receiver, method)
  matcher.matches(receiver, method)
end
Also aliased as: matches?
matches?(receiver, method)
Alias for: matches

Private Instance Methods

create_method?() click to toggle source
# File lib/ghost_dog/responder.rb, line 56
def create_method?
  options[:create_method]
end