Class: Liquid::Strainer

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

Overview

Strainer is the parent 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 Strainer.global_filter, Context#add_filters or Template.register_filter

Constant Summary collapse

@@filters =

:nodoc:

{}
@@known_filters =
Set.new
@@known_methods =
Set.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Strainer

Returns a new instance of Strainer.



15
16
17
# File 'lib/liquid/strainer.rb', line 15

def initialize(context)
  @context = context
end

Class Method Details

.add_known_filter(filter) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/liquid/strainer.rb', line 25

def self.add_known_filter(filter)
  unless @@known_filters.include?(filter)
    @@method_blacklist ||= Set.new(Strainer.instance_methods.map(&:to_s))
    new_methods = filter.instance_methods.map(&:to_s)
    new_methods.reject!{ |m| @@method_blacklist.include?(m) }
    @@known_methods.merge(new_methods)
    @@known_filters.add(filter)
  end
end

.create(context) ⇒ Object



35
36
37
38
39
# File 'lib/liquid/strainer.rb', line 35

def self.create(context)
  strainer = Strainer.new(context)
  @@filters.each { |k,m| strainer.extend(m) }
  strainer
end

.global_filter(filter) ⇒ Object

Raises:



19
20
21
22
23
# File 'lib/liquid/strainer.rb', line 19

def self.global_filter(filter)
  raise ArgumentError, "Passed filter is not a module" unless filter.is_a?(Module)
  add_known_filter(filter)
  @@filters[filter.name] = filter
end

Instance Method Details

#invokable?(method) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/liquid/strainer.rb', line 49

def invokable?(method)
  @@known_methods.include?(method.to_s) && respond_to?(method)
end

#invoke(method, *args) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/liquid/strainer.rb', line 41

def invoke(method, *args)
  if invokable?(method)
    send(method, *args)
  else
    args.first
  end
end