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

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

Instance Method Summary collapse

Instance Method Details

#helper(helper_name, method_name = nil, helpers_scope: false, &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
34
35
36
37
38
39
40
41
# File 'lib/bridgetown-builder/dsl/helpers.rb', line 11

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

  if block && helpers_scope
    Deprecator.deprecation_message(
      "The `helpers_scope' functionality is deprecated. Use the `helpers' builder " \
      "method to access the helpers scope in your plugin."
    )
    m.define_method helper_name, &block
  else
    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
  end

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

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

#helpersObject



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

def helpers
  @helpers # could be nil
end