Class: Surrogate::Hatchery
- Inherits:
-
Object
- Object
- Surrogate::Hatchery
- Defined in:
- lib/surrogate/hatchery.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
Instance Method Summary collapse
-
#add_api_methods_for(method_name) ⇒ Object
here we need to find better domain terminology.
- #api_method_names ⇒ Object
- #api_methods ⇒ Object
- #define(method_name, options = {}, block) ⇒ Object
- #defines_methods ⇒ Object
-
#initialize(klass) ⇒ Hatchery
constructor
A new instance of Hatchery.
Constructor Details
#initialize(klass) ⇒ Hatchery
Returns a new instance of Hatchery.
5 6 7 8 |
# File 'lib/surrogate/hatchery.rb', line 5 def initialize(klass) self.klass = klass defines_methods end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
3 4 5 |
# File 'lib/surrogate/hatchery.rb', line 3 def klass @klass end |
Instance Method Details
#add_api_methods_for(method_name) ⇒ Object
here we need to find better domain terminology
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/surrogate/hatchery.rb', line 28 def add_api_methods_for(method_name) klass.send :define_method, method_name do |*args, &block| @surrogate.invoke_method method_name, args, &block end # verbs klass.send :define_method, "will_#{method_name}" do |*args, &block| if args.size == 1 @surrogate.prepare_method method_name, args, &block else @surrogate.prepare_method_queue method_name, args, &block end self end # nouns klass.send :alias_method, "will_have_#{method_name}", "will_#{method_name}" end |
#api_method_names ⇒ Object
23 24 25 |
# File 'lib/surrogate/hatchery.rb', line 23 def api_method_names api_methods.keys - [:initialize] end |
#api_methods ⇒ Object
19 20 21 |
# File 'lib/surrogate/hatchery.rb', line 19 def api_methods @api_methods ||= {} end |
#define(method_name, options = {}, block) ⇒ Object
14 15 16 17 |
# File 'lib/surrogate/hatchery.rb', line 14 def define(method_name, ={}, block) add_api_methods_for method_name api_methods[method_name] = Options.new , block end |
#defines_methods ⇒ Object
10 11 12 |
# File 'lib/surrogate/hatchery.rb', line 10 def defines_methods klass.singleton_class.send :define_method, :define, &method(:define) end |