Module: Locomotive::Plugin::Liquid::LiquidClassMethods

Defined in:
lib/locomotive/plugin/liquid.rb

Overview

This module adds class methods to the plugin class in order to generate the prefixed liquid filter module and generate and register the prefixed liquid tag classes.

Instance Method Summary collapse

Instance Method Details

#prefixed_liquid_filter_module(prefix) ⇒ Object

Gets the module to include as a filter in liquid. It prefixes the filter methods with the given string followed by an underscore.

Parameters:

  • prefix (String)

    the prefix to use

Returns:

  • the module to use as a filter module



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/locomotive/plugin/liquid.rb', line 36

def prefixed_liquid_filter_module(prefix)
  # Create the module to be returned
  @prefixed_liquid_filter_module = Module.new do
    include ::Locomotive::Plugin::Liquid::PrefixedFilterModule
  end

  # Add the prefixed methods to the module
  raw_filter_modules = [self.liquid_filters].flatten.compact
  raw_filter_modules.each do |mod|
    mod.public_instance_methods.each do |meth|
      @prefixed_liquid_filter_module.module_eval do
        define_method(:"#{prefix}_#{meth}") do |*args|
          self._passthrough_filter_call(prefix, meth, *args)
        end
      end
    end
  end

  # Add a method which returns the modules to include for this prefix
  @prefixed_liquid_filter_module.module_eval do
    protected

    define_method(:"_modules_for_#{prefix}") do
      raw_filter_modules
    end
  end

  @prefixed_liquid_filter_module
end

#prefixed_liquid_tags(prefix) ⇒ Object

Returns a hash of tag names and tag classes to be registered in the liquid environment. The tag names are prefixed by the given prefix followed by an underscore, and the tag classes are modified so that they check the liquid context to determine whether they are enabled and should render normally. This check is done by determining whether the tag class is in the :enabled_plugin_tags register in the liquid context object (see setup_liquid_context).

Parameters:

  • prefix (String)

    the prefix to use

Returns:

  • a hash of tag names to tag classes



76
77
78
79
80
81
# File 'lib/locomotive/plugin/liquid.rb', line 76

def prefixed_liquid_tags(prefix)
  self.liquid_tags.inject({}) do |hash, (tag_name, tag_class)|
    hash["#{prefix}_#{tag_name}"] = tag_subclass(prefix, tag_class)
    hash
  end
end

#register_tags(prefix) ⇒ Object

Registers the tags given by prefixed_liquid_tags in the liquid template system.

Parameters:

  • prefix (String)

    the prefix to give to prefixed_liquid_tags



87
88
89
90
91
# File 'lib/locomotive/plugin/liquid.rb', line 87

def register_tags(prefix)
  prefixed_liquid_tags(prefix).each do |tag_name, tag_class|
    ::Liquid::Template.register_tag(tag_name, tag_class)
  end
end