Class: Veewee::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/veewee/templates.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Templates

Returns a new instance of Templates.



6
7
8
9
# File 'lib/veewee/templates.rb', line 6

def initialize(env)
  @env = env
  return self
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



4
5
6
# File 'lib/veewee/templates.rb', line 4

def env
  @env
end

Instance Method Details

#[](name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/veewee/templates.rb', line 11

def [](name)
  result = nil
  valid_paths(env.template_path).each do |template_dir|
    template = Veewee::Template.new(name, File.join(template_dir, name), @env)
    if template.exists?
      result = template
      return result
    end
  end
  return nil
end

#each(&block) ⇒ Object

Fetch all Templates



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/veewee/templates.rb', line 24

def each(&block)
  templates = Hash.new

  valid_paths(env.template_path).each do |template_dir|

    env.logger.debug("[Template] Searching #{template_dir} for templates")

    subdirs = Dir.glob("#{template_dir}/*")
    subdirs.each do |sub|
      if File.directory?("#{sub}")
        name = sub.sub(/#{template_dir}\//, '')
        template = Veewee::Template.new(name, sub, @env)
        if template.exists?
          env.logger.debug("[Template] template '#{name}' found")
          templates[name] = template
        end
      end
    end
  end

  if templates.length == 0
    env.logger.debug("[Template] no templates found")
  end

  Hash[templates.sort].each(&block)
end