Class: Liquid::Autoescape::ExemptionList

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/autoescape/exemption_list.rb

Overview

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.

Examples:

Adding exemption functions

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

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)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExemptionList

Create a new exemption list



48
49
50
# File 'lib/liquid/autoescape/exemption_list.rb', line 48

def initialize
  @exemptions = []
end

Class Method Details

.from_module(source) ⇒ Liquid::Autoescape::ExemptionList

Create an exemption list from a module’s instance methods

Parameters:

  • source (Module)

    The module providing exemptions as methods

Returns:



41
42
43
44
45
# File 'lib/liquid/autoescape/exemption_list.rb', line 41

def self.from_module(source)
  exemptions = new
  exemptions.import(source)
  exemptions
end

Instance Method Details

#add(&filter) ⇒ Liquid::Autoescape::ExemptionList

Add a single filter function to use as an exemption

Parameters:

  • filter (Proc)

    A filter function to use as an exemption

Returns:



56
57
58
59
# File 'lib/liquid/autoescape/exemption_list.rb', line 56

def add(&filter)
  @exemptions << Exemption.new(&filter)
  self
end

#applies?(variable) ⇒ Boolean Also known as: apply?

Determine whether any of the exemptions apply to a Liquid variable

Parameters:

Returns:

  • (Boolean)

    Whether any of the exemptions apply to the variable



78
79
80
81
82
83
84
85
# 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

#import(source) ⇒ Liquid::Autoescape::ExemptionList

Add all instance methods of a module as exemptions

Parameters:

  • source (Module)

    The module providing exemptions as methods

Returns:



65
66
67
68
69
70
71
72
# 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?Boolean

Whether the exemption list has exemptions

Returns:

  • (Boolean)


92
93
94
# File 'lib/liquid/autoescape/exemption_list.rb', line 92

def populated?
  !@exemptions.empty?
end

#sizeInteger

The number of exemptions in the list

Returns:

  • (Integer)


99
100
101
# File 'lib/liquid/autoescape/exemption_list.rb', line 99

def size
  @exemptions.size
end