Module: TemplateMethod::Macro

Defined in:
lib/template_method/macro.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.macro_methodsObject



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

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
# File 'lib/template_method/macro.rb', line 13

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

  inherit = true
  concrete_implementation_exists = method_defined?(method_name, inherit)
  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!



26
27
28
29
30
# File 'lib/template_method/macro.rb', line 26

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