Class: Yamler::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Template

Takes the path to the YAML file you wish to render. An optional Hash of options can be passed in. These options are available via the options accessor. If there is a Hash in the options called :locals then the keys of that Hash are available as local methods.

Examples:

Yamler::Template.new('/path/to/file.yml', {:locals => {:username => 'markbates'}, :foo => :bar})

# in file.yml:
username: <%= username %> # => 'markbates'
foo: <%= options[:foo] %> # => :bar


23
24
25
26
# File 'lib/yamler/template.rb', line 23

def initialize(path, options = {})
  self.path = File.expand_path(path)
  self.options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

:nodoc:

Raises:

  • (NoMethodError)


36
37
38
39
# File 'lib/yamler/template.rb', line 36

def method_missing(sym, *args) # :nodoc:
  raise NoMethodError.new(sym.to_s) if self.options[:locals].nil? || self.options[:locals][sym].nil?
  return self.options[:locals][sym]
end

Instance Attribute Details

#optionsObject

Options that are available to the YAML file.



8
9
10
# File 'lib/yamler/template.rb', line 8

def options
  @options
end

#pathObject

The path of the YAML file to be rendered



6
7
8
# File 'lib/yamler/template.rb', line 6

def path
  @path
end

Instance Method Details

#__FILE__Object

Returns the path of the current YAML file.



61
62
63
# File 'lib/yamler/template.rb', line 61

def __FILE__
  self.path
end

#render(b = binding) ⇒ Object

Runs the YAML file through ERB using either the current templates binding or the specified one. This method returns a string and NOT a YAML object.



31
32
33
34
# File 'lib/yamler/template.rb', line 31

def render(b = binding)
  res = ERB.new(File.read(self.path)).result(b)
  res
end

#require_yaml(path) ⇒ Object

Requires another YAML file from inside the current YAML file. The contents of the required YAML file will be run through ERB with the binding of the requiring YAML file and it’s output will be appended to the calling YAML file. The ‘.yml’ extension is optional. It will be added on if the extension is blank. If the file does not exist, it will look for it in the current directory. If it does not exist there it will raise an error.

Examples:

<%= require_yaml('foo') %> # => <current_yml_files_directory>/foo.yml
<%= require_yaml('foo.yml') %> # => <current_yml_files_directory>/foo.yml
<%= require_yaml('/usr/foo.yml') %> # => /usr/foo.yml


52
53
54
55
56
57
58
# File 'lib/yamler/template.rb', line 52

def require_yaml(path)
  path = File.extname(path) == '' ? "#{path}.yml" : path
  unless File.exists?(path)
    path = File.expand_path(File.join(File.dirname(self.path), path))
  end
  Yamler::Template.new(path).render(binding)
end