File: strainer_factory.rb

package info (click to toggle)
ruby-liquid 5.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,176 kB
  • sloc: ruby: 10,561; makefile: 6
file content (41 lines) | stat: -rw-r--r-- 850 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

module Liquid
  # StrainerFactory is the factory for the filters system.
  module StrainerFactory
    extend self

    def add_global_filter(filter)
      strainer_class_cache.clear
      GlobalCache.add_filter(filter)
    end

    def create(context, filters = [])
      strainer_from_cache(filters).new(context)
    end

    def global_filter_names
      GlobalCache.filter_method_names
    end

    GlobalCache = Class.new(StrainerTemplate)

    private

    def strainer_from_cache(filters)
      if filters.empty?
        GlobalCache
      else
        strainer_class_cache[filters] ||= begin
          klass = Class.new(GlobalCache)
          filters.each { |f| klass.add_filter(f) }
          klass
        end
      end
    end

    def strainer_class_cache
      @strainer_class_cache ||= {}
    end
  end
end