Module: TemplateMethod::Macro

Defined in:
lib/template_method/macro.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.macro_methodsObject



40
41
42
43
44
45
# File 'lib/template_method/macro.rb', line 40

def self.macro_methods
  [
    'template_method',
    'template_method!'
  ]
end

Instance Method Details

#include_template_method_moduleObject



7
8
9
10
11
# File 'lib/template_method/macro.rb', line 7

def include_template_method_module
  mod = Module.new
  include mod
  mod
end

#template_method_macro(method_name, &implementation) ⇒ Object Also known as: template_method



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/template_method/macro.rb', line 13

def template_method_macro(method_name, &implementation)
  implementation ||= proc { |*| nil }

  ancestors_without_prepended_modules = ancestors.drop_while do |ancestor|
    ancestor != self
  end

  concrete_implementation_exists = ancestors_without_prepended_modules.any? do |ancestor|
    ancestor.method_defined?(method_name, false) ||
      ancestor.private_method_defined?(method_name, false)
  end

  if concrete_implementation_exists
    return
  end

  template_method_module.define_method(method_name, &implementation)
end

#template_method_moduleObject



3
4
5
# File 'lib/template_method/macro.rb', line 3

def template_method_module
  @template_method_module ||= include_template_method_module
end

#template_method_variant_macro(method_name) ⇒ Object Also known as: template_method!



33
34
35
36
37
# File 'lib/template_method/macro.rb', line 33

def template_method_variant_macro(method_name)
  template_method_macro(method_name) do |*|
    raise TemplateMethod::Error, "Implementation is required (Method name: #{method_name})"
  end
end