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.
-
#ext ⇒ Object
readonly
Returns the value of attribute ext.
-
#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 19 |
# File 'lib/nm/source.rb', line 10 def initialize(root, opts = nil) opts ||= {} @root = Pathname.new(root.to_s) @ext = opts[:ext] ? ".#{opts[:ext]}" : nil @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 |
#ext ⇒ Object (readonly)
Returns the value of attribute ext.
8 9 10 |
# File 'lib/nm/source.rb', line 8 def ext @ext 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
25 26 27 28 29 |
# File 'lib/nm/source.rb', line 25 def data(file_path) @cache[file_path] ||= begin File.send(File.respond_to?(:binread) ? :binread : :read, file_path) end end |
#inspect ⇒ Object
21 22 23 |
# File 'lib/nm/source.rb', line 21 def inspect "#<#{self.class}:#{'0x0%x' % (object_id << 1)} @root=#{@root.inspect}>" end |
#render(template_name, locals = nil) ⇒ Object Also known as: partial
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/nm/source.rb', line 31 def render(template_name, locals = nil) if (filename = source_file_path(template_name)).nil? template_desc = "a template file named #{template_name.inspect}" if !@ext.nil? template_desc += " that ends in #{@ext.inspect}" end raise ArgumentError, "#{template_desc} does not exist" end @template_class.new(self, filename, locals || {}).__data__ end |