Module: Bridgetown::Builders::DSL::Generators

Included in:
PluginBuilder
Defined in:
bridgetown-builder/lib/bridgetown-builder/dsl/generators.rb

Instance Method Summary collapse

Instance Method Details

#generator(method_name = nil, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/generators.rb', line 7

def generator(method_name = nil, &block)
  block = method(method_name) if method_name.is_a?(Symbol)
  local_name = name # pull the name method into a local variable
  builder_priority = self.class.instance_variable_get(:@priority)

  anon_generator = Class.new(Bridgetown::Generator) do
    define_method(:_builder_block) { block }
    define_singleton_method(:custom_name) { local_name }

    attr_reader :site

    priority builder_priority || :low

    def inspect
      "#<#{self.class.custom_name} (Generator)>"
    end

    def generate(_site)
      _builder_block.call
    end
  end

  site.generators << anon_generator.new(site.config)
  site.generators.sort!

  functions << { name:, generator: anon_generator }
end