Module: Bridgetown::Builders::DSL::Helpers

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

Instance Method Summary collapse

Instance Method Details

#helper(helper_name, method_name = nil, &block) ⇒ Object



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/helpers.rb', line 11

def helper(helper_name, method_name = nil, &block)
  m = Module.new

  builder_self = self
  method_name ||= helper_name unless block
  unless method_name
    method_name = :"__helper_#{helper_name}"
    builder_self.define_singleton_method(method_name) do |*args, **kwargs, &block2|
      block.(*args, **kwargs, &block2)
    end
  end
  m.define_method helper_name do |*args, **kwargs, &block2|
    prev_var = builder_self.instance_variable_get(:@helpers)
    builder_self.instance_variable_set(:@helpers, self)
    builder_self.send(method_name, *args, **kwargs, &block2).tap do
      builder_self.instance_variable_set(:@helpers, prev_var)
    end
  end

  Bridgetown::RubyTemplateView::Helpers.include(m)

  functions << { name:, filter: m }
end

#helpersObject



7
8
9
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/helpers.rb', line 7

def helpers
  @helpers # could be nil
end