Class: Liquid::Strainer
- Inherits:
-
Object
- Object
- Liquid::Strainer
- 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
- @@global_strainer =
:nodoc:
Class.new(Strainer) do @filter_methods = Set.new end
- @@strainer_class_cache =
Hash.new do |hash, filters| hash[filters] = Class.new(@@global_strainer) do @filter_methods = @@global_strainer.filter_methods.dup filters.each { |f| add_filter(f) } end end
Class Method Summary collapse
- .add_filter(filter) ⇒ Object
- .create(context, filters = []) ⇒ Object
- .filter_methods ⇒ Object
- .global_filter(filter) ⇒ Object
- .invokable?(method) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(context) ⇒ Strainer
constructor
A new instance of Strainer.
- #invoke(method, *args) ⇒ Object
Constructor Details
#initialize(context) ⇒ Strainer
Returns a new instance of Strainer.
21 22 23 |
# File 'lib/liquid/strainer.rb', line 21 def initialize(context) @context = context end |
Class Method Details
.add_filter(filter) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/liquid/strainer.rb', line 29 def self.add_filter(filter) raise ArgumentError, "Expected module but got: #{f.class}" unless filter.is_a?(Module) unless self.class.include?(filter) self.send(:include, filter) @filter_methods.merge(filter.public_instance_methods.map(&:to_s)) end end |
.create(context, filters = []) ⇒ Object
45 46 47 |
# File 'lib/liquid/strainer.rb', line 45 def self.create(context, filters = []) @@strainer_class_cache[filters].new(context) end |
.filter_methods ⇒ Object
25 26 27 |
# File 'lib/liquid/strainer.rb', line 25 def self.filter_methods @filter_methods end |
.global_filter(filter) ⇒ Object
37 38 39 |
# File 'lib/liquid/strainer.rb', line 37 def self.global_filter(filter) @@global_strainer.add_filter(filter) end |
.invokable?(method) ⇒ Boolean
41 42 43 |
# File 'lib/liquid/strainer.rb', line 41 def self.invokable?(method) @filter_methods.include?(method.to_s) end |
Instance Method Details
#invoke(method, *args) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/liquid/strainer.rb', line 49 def invoke(method, *args) if self.class.invokable?(method) send(method, *args) else args.first end rescue ::ArgumentError => e raise Liquid::ArgumentError.new(e.) end |