Class: Twig::Loader::Chain

Inherits:
Base
  • Object
show all
Defined in:
lib/twig/loader/chain.rb

Instance Method Summary collapse

Constructor Details

#initialize(loaders) ⇒ Chain

Returns a new instance of Chain.

Parameters:



7
8
9
10
11
# File 'lib/twig/loader/chain.rb', line 7

def initialize(loaders)
  super()

  @loaders = loaders
end

Instance Method Details

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/twig/loader/chain.rb', line 33

def exists?(name)
  @has_source_cache ||= {}

  return @has_source_cache[name] if @has_source_cache.key?(name)

  loaders.each do |loader|
    if loader.exists?(name)
      return @has_source_cache[name] = true
    end
  end

  @has_source_cache[name] = false
end

#fresh?(name, time) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/twig/loader/chain.rb', line 67

def fresh?(name, time)
  exceptions = []

  loaders.each do |loader|
    unless loader.exists?(name)
      next
    end

    begin
      return loader.fresh?(name, time)
    rescue Twig::Error::Loader => e
      exceptions << "#{loader.class.name}: #{e.message}"
    end
  end

  exceptions = exceptions.any? ? "(#{exceptions.join(', ')})" : ''

  raise Twig::Error::Loader, "Template \"#{name}\" is not defined#{exceptions}."
end

#get_cache_key(name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/twig/loader/chain.rb', line 47

def get_cache_key(name)
  exceptions = []

  loaders.each do |loader|
    unless loader.exists?(name)
      next
    end

    begin
      return loader.get_cache_key(name)
    rescue Twig::Error::Loader => e
      exceptions << "#{loader.class.name}: #{e.message}"
    end
  end

  exceptions = exceptions.any? ? "(#{exceptions.join(', ')})" : ''

  raise Twig::Error::Loader, "Template \"#{name}\" is not defined#{exceptions}."
end

#get_source_context(name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/twig/loader/chain.rb', line 13

def get_source_context(name)
  exceptions = []

  loaders.each do |loader|
    unless loader.exists?(name)
      next
    end

    begin
      return loader.get_source_context(name)
    rescue Twig::Error::Loader => e
      exceptions << "#{loader.class.name}: #{e.message}"
    end
  end

  exceptions = exceptions.any? ? "(#{exceptions.join(', ')})" : ''

  raise Twig::Error::Loader, "Template \"#{name}\" is not defined#{exceptions}."
end