Class: Gamercard::Templater

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_data) ⇒ Templater

Pass an optional template path in the option hash to use a custom path.



10
11
12
13
14
15
# File 'lib/templater.rb', line 10

def initialize(template_data)
  template_path   = template_data.delete('template_path')
  @templates_path = template_path || File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
  @template       = load_template(template_data)
  self
end

Instance Attribute Details

#templateObject

Returns the value of attribute template.



7
8
9
# File 'lib/templater.rb', line 7

def template
  @template
end

#templates_pathObject

Returns the value of attribute templates_path.



7
8
9
# File 'lib/templater.rb', line 7

def templates_path
  @templates_path
end

Instance Method Details

#load_template(template_data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/templater.rb', line 17

def load_template(template_data)
  name = template_data['name']
  raise 'Your template settings need to include a name entry' if name.nil?
  begin
    require File.join(templates_path, "#{name}")
  rescue LoadError
    raise ::Gamercard::Template::OptionError, "the #{name}.rb template file could not be found in #{templates_path}"
  else
    # find and return template object
    klass_name = name.capitalize
    if Template.const_defined?(klass_name)
      template_klass = Template.const_get(klass_name)
      template_klass.new(template_data)
    else
      raise Template::OptionError, "The #{klass_name} template class was not found"
    end
  end
end