Class: Memorable::DefaultYAMLEngine

Inherits:
TemplateEngine show all
Defined in:
lib/memorable/template_engines/default.rb

Instance Attribute Summary

Attributes inherited from TemplateEngine

#load_path, #templates

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TemplateEngine

assemble, #assemble, #initialize

Constructor Details

This class inherits a constructor from Memorable::TemplateEngine

Class Method Details

.add(pattern, base_dir) ⇒ Object



18
19
20
21
# File 'lib/memorable/template_engines/default.rb', line 18

def self.add(pattern, base_dir)
  files = Dir[File.join(base_dir, pattern)]
  instance.load_path.concat(files)
end

.load!Object

Class Methods


load yaml templates



9
10
11
12
13
14
15
16
# File 'lib/memorable/template_engines/default.rb', line 9

def self.load!
  pattern = self.pattern_from I18n.available_locales

  add("memorable/templates/#{pattern}.yml", Memorable.config.default_templates_directory)
  add("app/views/memorable/#{pattern}.yml", Rails.root) if defined?(Rails)

  instance.store_templates
end

.pattern_from(args) ⇒ Object



23
24
25
26
# File 'lib/memorable/template_engines/default.rb', line 23

def self.pattern_from(args)
  array = Array(args || [])
  array.blank? ? '*' : "{#{array.join ','}}"
end

Instance Method Details

#parse(controller, action, sub_key) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/memorable/template_engines/default.rb', line 39

def parse(controller, action, sub_key)
  sub_key ||= 'base'
  raw_templates = catch(:template_found) do
    parse_entry(controller, action, sub_key)
    parse_entry(controller, 'other', sub_key)
    parse_entry('defaults', action, sub_key)
    parse_entry('defaults', 'other', sub_key)
    nil
  end
  raise TemplateNotFound, "Template: #{controller} #{action} #{sub_key} not found" unless raw_templates
  raw_templates
end

#render(template, locals) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/memorable/template_engines/default.rb', line 52

def render(template, locals)
  if locals
    I18n.interpolate(template, locals)
  else
    template
  end
end

#store_templatesObject

Instance Methods




31
32
33
34
35
36
37
# File 'lib/memorable/template_engines/default.rb', line 31

def store_templates
  load_path.each do |filename|
    locale = File.basename(filename, ".yml")
    data = load_yml filename
    templates[locale.to_sym] = data
  end
end