Class: Surrogate::Hatchery

Inherits:
Object
  • Object
show all
Defined in:
lib/surrogate/hatchery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#klassObject

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_namesObject



23
24
25
# File 'lib/surrogate/hatchery.rb', line 23

def api_method_names
  api_methods.keys - [:initialize]
end

#api_methodsObject



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, options={}, block)
  add_api_methods_for method_name
  api_methods[method_name] = Options.new options, block
end

#defines_methodsObject



10
11
12
# File 'lib/surrogate/hatchery.rb', line 10

def defines_methods
  klass.singleton_class.send :define_method, :define, &method(:define)
end