Class: Surrogate::Hatchling

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

Overview

This contains the unique behaviour for each instance It handles method invocation and records the appropriate information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, hatchery) ⇒ Hatchling

Returns a new instance of Hatchling.



10
11
12
# File 'lib/surrogate/hatchling.rb', line 10

def initialize(instance, hatchery)
  self.instance, self.hatchery = instance, hatchery
end

Instance Attribute Details

#hatcheryObject

Returns the value of attribute hatchery.



8
9
10
# File 'lib/surrogate/hatchling.rb', line 8

def hatchery
  @hatchery
end

#instanceObject

Returns the value of attribute instance.



8
9
10
# File 'lib/surrogate/hatchling.rb', line 8

def instance
  @instance
end

Instance Method Details

#api_methodsObject



14
15
16
# File 'lib/surrogate/hatchling.rb', line 14

def api_methods
  hatchery.api_methods
end

#get_ivar(method_name) ⇒ Object



41
42
43
# File 'lib/surrogate/hatchling.rb', line 41

def get_ivar(method_name)
  instance.instance_variable_get "@#{method_name}"
end

#has_ivar?(method_name) ⇒ Boolean

maybe these four should be extracted into their own class

Returns:

  • (Boolean)


33
34
35
# File 'lib/surrogate/hatchling.rb', line 33

def has_ivar?(method_name)
  instance.instance_variable_defined? "@#{method_name}"
end

#invocations(method_name) ⇒ Object



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

def invocations(method_name)
  invoked_methods[method_name]
end

#invoke_method(method_name, args, &block) ⇒ Object



18
19
20
21
22
# File 'lib/surrogate/hatchling.rb', line 18

def invoke_method(method_name, args, &block)
  invoked_methods[method_name] << args
  return get_default method_name, args unless has_ivar? method_name
  Value.factory(get_ivar method_name).value(self, method_name)
end

#prepare_method(method_name, args, &block) ⇒ Object



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

def prepare_method(method_name, args, &block)
  set_ivar method_name, Value.factory(*args, &block)
end

#set_ivar(method_name, value) ⇒ Object



37
38
39
# File 'lib/surrogate/hatchling.rb', line 37

def set_ivar(method_name, value)
  instance.instance_variable_set "@#{method_name}", value
end

#unset_ivar(method_name) ⇒ Object



45
46
47
# File 'lib/surrogate/hatchling.rb', line 45

def unset_ivar(method_name)
  instance.send :remove_instance_variable, "@#{method_name}"
end