Class: Surrogate::Endower

Inherits:
Object
  • Object
show all
Defined in:
lib/surrogate/endower.rb

Overview

Adds surrogate behaviour to your class / singleton class / instances

please refactor me! …may not be possible :( Can we move all method definitions into this class?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options, &block) ⇒ Endower

Returns a new instance of Endower.



30
31
32
# File 'lib/surrogate/endower.rb', line 30

def initialize(klass, options, &block)
  self.klass, self.options, self.block = klass, options, block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



28
29
30
# File 'lib/surrogate/endower.rb', line 28

def block
  @block
end

#klassObject

Returns the value of attribute klass.



28
29
30
# File 'lib/surrogate/endower.rb', line 28

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



28
29
30
# File 'lib/surrogate/endower.rb', line 28

def options
  @options
end

Class Method Details

.add_hook(&block) ⇒ Object



16
17
18
# File 'lib/surrogate/endower.rb', line 16

def self.add_hook(&block)
  hooks << block
end

.endow(klass, options, &block) ⇒ Object



24
25
26
# File 'lib/surrogate/endower.rb', line 24

def self.endow(klass, options, &block)
  new(klass, options, &block).endow
end

.hooksObject



20
21
22
# File 'lib/surrogate/endower.rb', line 20

def self.hooks
  @hooks ||= []
end

.uninitialized_instance_for(surrogate_class) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/surrogate/endower.rb', line 8

def self.uninitialized_instance_for(surrogate_class)
  instance = surrogate_class.allocate
  hatchery = surrogate_class.instance_variable_get :@hatchery
  surrogate_class.last_instance = instance
  instance.instance_variable_set :@hatchling, Hatchling.new(instance, hatchery)
  instance
end

Instance Method Details

#endowObject



34
35
36
37
# File 'lib/surrogate/endower.rb', line 34

def endow
  endow_klass
  endow_singleton_class
end