lib/surrogate/hatchling.rb in surrogate-0.3.0 vs lib/surrogate/hatchling.rb in surrogate-0.3.1
- old
+ new
@@ -1,8 +1,9 @@
class Surrogate
UnknownMethod = Class.new StandardError
+
# This contains the unique behaviour for each instance
# It handles method invocation and records the appropriate information
class Hatchling
attr_accessor :instance, :hatchery
@@ -15,35 +16,38 @@
end
def invoke_method(method_name, args, &block)
invoked_methods[method_name] << args
return get_default method_name, args unless has_ivar? method_name
- ivar = get_ivar method_name
-
- # This may soon need classes for each type which know how to invoke themselves
- case ivar
- when MethodQueue
- play_from_queue ivar, method_name
- when Exception
- raise ivar
- else
- ivar
- end
+ Value.factory(get_ivar method_name).value(self, method_name)
end
def prepare_method(method_name, args, &block)
- set_ivar method_name, *args
+ set_ivar method_name, Value.factory(*args, &block)
end
- def prepare_method_queue(method_name, args, &block)
- set_ivar method_name, MethodQueue.new(args)
- end
-
def invocations(method_name)
invoked_methods[method_name]
end
+ # maybe these four should be extracted into their own class
+ def has_ivar?(method_name)
+ instance.instance_variable_defined? "@#{method_name}"
+ end
+
+ def set_ivar(method_name, value)
+ instance.instance_variable_set "@#{method_name}", value
+ end
+
+ def get_ivar(method_name)
+ instance.instance_variable_get "@#{method_name}"
+ end
+
+ def unset_ivar(method_name)
+ instance.send :remove_instance_variable, "@#{method_name}"
+ end
+
private
def invoked_methods
@invoked_methods ||= Hash.new do |hash, method_name|
must_know method_name
@@ -55,35 +59,13 @@
api_methods[method_name].default instance, args do
raise UnpreparedMethodError, "#{method_name} has been invoked without being told how to behave"
end
end
- def play_from_queue(queue, method_name)
- result = queue.dequeue
- unset_ivar method_name if queue.empty?
- result
- end
-
def must_know(method_name)
return if api_methods.has_key? method_name
known_methods = api_methods.keys.map(&:to_s).map(&:inspect).join ', '
raise UnknownMethod, "doesn't know \"#{method_name}\", only knows #{known_methods}"
- end
-
- def has_ivar?(method_name)
- instance.instance_variable_defined? "@#{method_name}"
- end
-
- def set_ivar(method_name, value)
- instance.instance_variable_set "@#{method_name}", value
- end
-
- def get_ivar(method_name)
- instance.instance_variable_get "@#{method_name}"
- end
-
- def unset_ivar(method_name)
- instance.send :remove_instance_variable, "@#{method_name}"
end
end
end