class Liquid::Autoescape::ExemptionList

A list of exemptions that may apply to template variables

Exemption lists manage one or more exemptions, and determine whether any managed exemptions applies to a template variable. Exemptions can be added as individual filter functions, or can be imported in bulk from a module.

@example Adding exemption functions

exemptions = ExemptionList.new
ExemptionList.add { |variable| variable.name == "one" }
ExemptionList.add { |variable| variable.name == "two" }

@example Importing exemptions from a module

module MyExemptions

  def exemption_one(variable)
    variable.name == "one"
  end

  def exemption_two(variable)
    variable.name == "two"
  end

end

exemptions = ExemptionList.new
exemptions.import(MyExemptions)

Public Class Methods

from_module(source) click to toggle source

Create an exemption list from a module's instance methods

@param [Module] source The module providing exemptions as methods @return [Liquid::Autoescape::ExemptionList]

# File lib/liquid/autoescape/exemption_list.rb, line 41
def self.from_module(source)
  exemptions = new
  exemptions.import(source)
  exemptions
end
new() click to toggle source

Create a new exemption list

# File lib/liquid/autoescape/exemption_list.rb, line 48
def initialize
  @exemptions = []
end

Public Instance Methods

add(&filter) click to toggle source

Add a single filter function to use as an exemption

@param [Proc] filter A filter function to use as an exemption @return [Liquid::Autoescape::ExemptionList] The updated exemption list

# File lib/liquid/autoescape/exemption_list.rb, line 56
def add(&filter)
  @exemptions << Exemption.new(&filter)
  self
end
applies?(variable) click to toggle source

Determine whether any of the exemptions apply to a Liquid variable

@param [Liquid::Autoescape::TemplateVariable] variable A Liquid variable used in a template @return [Boolean] Whether any of the exemptions apply to the variable

# File lib/liquid/autoescape/exemption_list.rb, line 78
def applies?(variable)
  @exemptions.each do |exemption|
    if exemption.applies?(variable)
      return true
    end
  end
  false
end
Also aliased as: apply?
apply?(variable)
Alias for: applies?
import(source) click to toggle source

Add all instance methods of a module as exemptions

@param [Module] source The module providing exemptions as methods @return [Liquid::Autoescape::ExemptionList] The updated exemption list

# File lib/liquid/autoescape/exemption_list.rb, line 65
def import(source)
  container = Module.new { extend source }
  exemptions = source.instance_methods(false)
  exemptions.each do |exemption|
    @exemptions << Exemption.new(&container.method(exemption))
  end
  self
end
populated?() click to toggle source

Whether the exemption list has exemptions

@return [Boolean]

# File lib/liquid/autoescape/exemption_list.rb, line 92
def populated?
  !@exemptions.empty?
end
size() click to toggle source

The number of exemptions in the list

@return [Integer]

# File lib/liquid/autoescape/exemption_list.rb, line 99
def size
  @exemptions.size
end