lib/surrogate/endower.rb in surrogate-0.5.0 vs lib/surrogate/endower.rb in surrogate-0.5.1
- old
+ new
@@ -3,10 +3,18 @@
# 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?
class Endower
+ def self.add_hook(&block)
+ hooks << block
+ end
+
+ def self.hooks
+ @hooks ||= []
+ end
+
def self.endow(klass, &block)
new(klass, &block).endow
end
attr_accessor :klass, :block
@@ -26,18 +34,24 @@
klass.extend ClassMethods
add_hatchery_to klass
enable_defining_methods klass
record_initialization_for_instances_of klass
remember_invocations_for_instances_of klass
+ invoke_hooks klass
end
def endow_singleton_class
hatchery = add_hatchery_to singleton
enable_defining_methods singleton
singleton.module_eval &block if block
klass.instance_variable_set :@hatchling, Hatchling.new(klass, hatchery)
remember_invocations_for_instances_of singleton
+ invoke_hooks singleton
klass
+ end
+
+ def invoke_hooks(klass)
+ self.class.hooks.each { |hook| hook.call klass }
end
# yeesh :( pretty sure there isn't a better way to do this
def record_initialization_for_instances_of(klass)
def klass.method_added(meth)