lib/surrogate/hatchery.rb in surrogate-0.2.0 vs lib/surrogate/hatchery.rb in surrogate-0.3.0

- old
+ new

@@ -1,48 +1,62 @@ class Surrogate + + # This manages the definitions that were given to the class + # The hatchlings are added to the instances, and they look here + # to find out about how their methods are implemented. class Hatchery attr_accessor :klass def initialize(klass) self.klass = klass - defines_methods + klass_can_define_api_methods end - def defines_methods - klass.singleton_class.send :define_method, :define, &method(:define) - end - def define(method_name, options={}, block) - add_api_methods_for method_name + add_api_method_for method_name + add_verb_helpers_for method_name + add_noun_helpers_for method_name api_methods[method_name] = Options.new options, block + klass end def api_methods @api_methods ||= {} end def api_method_names api_methods.keys - [:initialize] end - # here we need to find better domain terminology - def add_api_methods_for(method_name) + private + + def klass_can_define_api_methods + klass.singleton_class.send :define_method, :define, &method(:define) + end + + def add_api_method_for(method_name) klass.send :define_method, method_name do |*args, &block| - @surrogate.invoke_method method_name, args, &block + @hatchling.invoke_method method_name, args, &block end + end - # verbs - klass.send :define_method, "will_#{method_name}" do |*args, &block| + def add_verb_helpers_for(method_name) + add_helpers_for method_name, "will_#{method_name}" + end + + def add_noun_helpers_for(method_name) + add_helpers_for method_name, "will_have_#{method_name}" + end + + def add_helpers_for(method_name, helper_name) + klass.send :define_method, helper_name do |*args, &block| if args.size == 1 - @surrogate.prepare_method method_name, args, &block + @hatchling.prepare_method method_name, args, &block else - @surrogate.prepare_method_queue method_name, args, &block + @hatchling.prepare_method_queue method_name, args, &block end self end - - # nouns - klass.send :alias_method, "will_have_#{method_name}", "will_#{method_name}" end end end