Class: Nm::Source
- Inherits:
-
Object
- Object
- Nm::Source
- Defined in:
- lib/nm/source.rb
Direct Known Subclasses
Defined Under Namespace
Classes: NullCache
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#template_class ⇒ Object
readonly
Returns the value of attribute template_class.
Instance Method Summary collapse
- #data(file_path) ⇒ Object
-
#initialize(root, opts = nil) ⇒ Source
constructor
A new instance of Source.
- #inspect ⇒ Object
- #render(template_name, locals = nil) ⇒ Object (also: #partial)
Constructor Details
#initialize(root, opts = nil) ⇒ Source
Returns a new instance of Source.
10 11 12 13 14 15 16 17 18 |
# File 'lib/nm/source.rb', line 10 def initialize(root, opts = nil) opts ||= {} @root = Pathname.new(root.to_s) @cache = opts[:cache] ? Hash.new : NullCache.new @template_class = Class.new(Template) do (opts[:locals] || {}).each{ |key, value| define_method(key){ value } } end end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
8 9 10 |
# File 'lib/nm/source.rb', line 8 def cache @cache end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
8 9 10 |
# File 'lib/nm/source.rb', line 8 def root @root end |
#template_class ⇒ Object (readonly)
Returns the value of attribute template_class.
8 9 10 |
# File 'lib/nm/source.rb', line 8 def template_class @template_class end |
Instance Method Details
#data(file_path) ⇒ Object
24 25 26 27 28 |
# File 'lib/nm/source.rb', line 24 def data(file_path) @cache[file_path] ||= begin File.send(File.respond_to?(:binread) ? :binread : :read, file_path) end end |
#inspect ⇒ Object
20 21 22 |
# File 'lib/nm/source.rb', line 20 def inspect "#<#{self.class}:#{'0x0%x' % (object_id << 1)} @root=#{@root.inspect}>" end |
#render(template_name, locals = nil) ⇒ Object Also known as: partial
30 31 32 |
# File 'lib/nm/source.rb', line 30 def render(template_name, locals = nil) @template_class.new(self, source_file_path(template_name), locals || {}).__data__ end |