Class: Yamler::Template
- Inherits:
-
Object
- Object
- Yamler::Template
- Defined in:
- lib/yamler/template.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Options that are available to the YAML file.
-
#path ⇒ Object
The path of the YAML file to be rendered.
Instance Method Summary collapse
-
#__FILE__ ⇒ Object
Returns the path of the current YAML file.
-
#initialize(path, options = {}) ⇒ Template
constructor
Takes the path to the YAML file you wish to render.
-
#method_missing(sym, *args) ⇒ Object
:nodoc:.
-
#render(b = binding) ⇒ Object
Runs the YAML file through ERB using either the current templates
binding
or the specified one. -
#require_yaml(path) ⇒ Object
Requires another YAML file from inside the current YAML file.
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, = {}) self.path = File.(path) self. = end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
:nodoc:
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.[:locals].nil? || self.[:locals][sym].nil? return self.[:locals][sym] end |
Instance Attribute Details
#options ⇒ Object
Options that are available to the YAML file.
8 9 10 |
# File 'lib/yamler/template.rb', line 8 def @options end |
#path ⇒ Object
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.(File.join(File.dirname(self.path), path)) end Yamler::Template.new(path).render(binding) end |