Class: Surrogate::Hatchling
- Inherits:
-
Object
- Object
- Surrogate::Hatchling
- 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
-
#hatchery ⇒ Object
Returns the value of attribute hatchery.
-
#instance ⇒ Object
Returns the value of attribute instance.
Instance Method Summary collapse
- #api_methods ⇒ Object
- #get_ivar(method_name) ⇒ Object
-
#has_ivar?(method_name) ⇒ Boolean
maybe these four should be extracted into their own class.
-
#initialize(instance, hatchery) ⇒ Hatchling
constructor
A new instance of Hatchling.
- #invocations(method_name) ⇒ Object
- #invoke_method(method_name, args, &block) ⇒ Object
- #prepare_method(method_name, args, &block) ⇒ Object
- #set_ivar(method_name, value) ⇒ Object
- #unset_ivar(method_name) ⇒ Object
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
#hatchery ⇒ Object
Returns the value of attribute hatchery.
8 9 10 |
# File 'lib/surrogate/hatchling.rb', line 8 def hatchery @hatchery end |
#instance ⇒ Object
Returns the value of attribute instance.
8 9 10 |
# File 'lib/surrogate/hatchling.rb', line 8 def instance @instance end |
Instance Method Details
#api_methods ⇒ Object
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
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 |