Module: Peachy::Mimic

Defined in:
lib/peachy/mimic.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Delegates the method_missing call to the underlying object that is being mimicked.



25
26
27
# File 'lib/peachy/mimic.rb', line 25

def method_missing method_name, *args, &block
  subject.send(method_name, *args, &block)
end

Class Method Details

.make_a_mimic_of(to_mimic, subject) ⇒ Object

Use this method to make a mimic of an object. The first argument passed is the object to be mimicked and the second is the subject of the mimic action. All previous methods defined on the subject will be buried.



6
7
8
9
10
11
12
13
# File 'lib/peachy/mimic.rb', line 6

def self.make_a_mimic_of to_mimic, subject
  subject.instance_eval do
    (class << self; self; end).class_eval do
      define_method(:subject) { to_mimic }
      include Mimic
    end
  end
end

Instance Method Details

#subjectObject

This method has been defined as a template. If you just include Mimic as a module on your class you will get a NothingToMimic error when calling #mimic.

Use Mimic#make_a_mimic_of to turn an object into a mimic.

Raises:



19
20
21
# File 'lib/peachy/mimic.rb', line 19

def subject
  raise NothingToMimic.new
end