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
- @@filters =
:nodoc:
[]
- @@known_filters =
Set.new
- @@known_methods =
Set.new
- @@strainer_class_cache =
Hash.new do |hash, filters| hash[filters] = Class.new(Strainer) do filters.each { |f| include f } end end
Class Method Summary collapse
- .add_known_filter(filter) ⇒ Object
- .create(context, filters = []) ⇒ Object
- .global_filter(filter) ⇒ Object
- .strainer_class_cache ⇒ Object
Instance Method Summary collapse
-
#initialize(context) ⇒ Strainer
constructor
A new instance of Strainer.
- #invokable?(method) ⇒ Boolean
- #invoke(method, *args) ⇒ Object
Constructor Details
#initialize(context) ⇒ Strainer
Returns a new instance of Strainer.
20 21 22 |
# File 'lib/liquid/strainer.rb', line 20 def initialize(context) @context = context end |
Class Method Details
.add_known_filter(filter) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/liquid/strainer.rb', line 30 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, filters = []) ⇒ Object
44 45 46 47 |
# File 'lib/liquid/strainer.rb', line 44 def self.create(context, filters = []) filters = @@filters + filters strainer_class_cache[filters].new(context) end |
.global_filter(filter) ⇒ Object
24 25 26 27 28 |
# File 'lib/liquid/strainer.rb', line 24 def self.global_filter(filter) raise ArgumentError, "Passed filter is not a module" unless filter.is_a?(Module) add_known_filter(filter) @@filters << filter unless @@filters.include?(filter) end |
.strainer_class_cache ⇒ Object
40 41 42 |
# File 'lib/liquid/strainer.rb', line 40 def self.strainer_class_cache @@strainer_class_cache end |
Instance Method Details
#invokable?(method) ⇒ Boolean
59 60 61 |
# File 'lib/liquid/strainer.rb', line 59 def invokable?(method) @@known_methods.include?(method.to_s) && respond_to?(method) end |
#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 invokable?(method) send(method, *args) else args.first end rescue ::ArgumentError => e raise Liquid::ArgumentError.new(e.) end |