Module: Surrogate::ClassMethods

Defined in:
lib/surrogate/endower.rb

Overview

use a module so that the method is inherited (important for substitutability)

Instance Method Summary collapse

Instance Method Details

#cloneObject

Should this be dup? (dup seems to copy singleton methods) and may be able to use #initialize_copy to reset ivars Can we just remove this feature an instead provide a reset feature which could be hooked into in before/after blocks (e.g. github.com/rspec/rspec-core/blob/622505d616d950ed53d12c6e82dbb953ba6241b4/lib/rspec/core/mocking/with_rspec.rb)



94
95
96
97
98
99
100
101
102
# File 'lib/surrogate/endower.rb', line 94

def clone
  hatchling, hatchery = @hatchling, @hatchery
  Class.new self do
    Surrogate.endow self do
      hatchling.api_methods.each { |name, options| define name, options.to_hash, &options.default_proc }
    end
    hatchery.api_methods.each { |name, options| define name, options.to_hash, &options.default_proc }
  end
end

#new(*args) ⇒ Object

Custom new, because user can define initialize, and we need to record it Can we move this into the redefinition of initialize and have it explicitly record itself?



106
107
108
109
110
111
# File 'lib/surrogate/endower.rb', line 106

def new(*args)
  instance = allocate
  instance.instance_variable_set :@hatchling, Hatchling.new(instance, @hatchery)
  instance.send :initialize, *args
  instance
end