Class: Nm::Source

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

Direct Known Subclasses

DefaultSource

Defined Under Namespace

Classes: NullCache

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'lib/nm/source.rb', line 8

def cache
  @cache
end

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/nm/source.rb', line 8

def root
  @root
end

#template_classObject (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

#inspectObject



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