Class: Liquid::StrainerTemplate

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

Overview

StrainerTemplate is the computed class for the filters system. New filters are mixed into the strainer class which is then instantiated for each liquid template render run.

The Strainer only allows method calls defined in filters given to it via StrainerFactory.add_global_filter, Context#add_filters or Template.register_filter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ StrainerTemplate

Returns a new instance of StrainerTemplate.



12
13
14
# File 'lib/liquid/strainer_template.rb', line 12

def initialize(context)
  @context = context
end

Class Method Details

.add_filter(filter) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/liquid/strainer_template.rb', line 17

def add_filter(filter)
  return if include?(filter)

  invokable_non_public_methods = (filter.private_instance_methods + filter.protected_instance_methods).select { |m| invokable?(m) }
  if invokable_non_public_methods.any?
    raise MethodOverrideError, "Filter overrides registered public methods as non public: #{invokable_non_public_methods.join(', ')}"
  end

  include(filter)

  filter_methods.merge(filter.public_instance_methods.map(&:to_s))
end

.filter_method_namesObject



39
40
41
# File 'lib/liquid/strainer_template.rb', line 39

def filter_method_names
  filter_methods.map(&:to_s).to_a
end

.inherited(subclass) ⇒ Object



34
35
36
37
# File 'lib/liquid/strainer_template.rb', line 34

def inherited(subclass)
  super
  subclass.instance_variable_set(:@filter_methods, @filter_methods.dup)
end

.invokable?(method) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/liquid/strainer_template.rb', line 30

def invokable?(method)
  filter_methods.include?(method.to_s)
end

Instance Method Details

#invoke(method, *args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/liquid/strainer_template.rb', line 50

def invoke(method, *args)
  if self.class.invokable?(method)
    send(method, *args)
  elsif @context.strict_filters
    raise Liquid::UndefinedFilter, "undefined filter #{method}"
  else
    args.first
  end
rescue ::ArgumentError => e
  raise Liquid::ArgumentError, e.message, e.backtrace
end