Class: Hobber::RenderableObject

Inherits:
Object
  • Object
show all
Defined in:
lib/hobber/renderable_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, a_renderer = nil, a_tmpl_var_extractor = nil) ⇒ RenderableObject

Returns a new instance of RenderableObject.



10
11
12
13
14
15
16
# File 'lib/hobber/renderable_object.rb', line 10

def initialize(path, a_renderer=nil, a_tmpl_var_extractor=nil)
  @path         = path
  @tmpl_content = block_given? ? yield(self) : File.open(@path, 'r:UTF-8') { |f| f.read }

  @renderer           = a_renderer           || Renderer::Tilt.new
  @tmpl_var_extractor = a_tmpl_var_extractor || TmplVarExtractor::Yaml.new(@tmpl_content, @path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/hobber/renderable_object.rb', line 8

def path
  @path
end

#rendererObject (readonly)

Returns the value of attribute renderer.



8
9
10
# File 'lib/hobber/renderable_object.rb', line 8

def renderer
  @renderer
end

#tmpl_contentObject (readonly)

Returns the value of attribute tmpl_content.



8
9
10
# File 'lib/hobber/renderable_object.rb', line 8

def tmpl_content
  @tmpl_content
end

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/hobber/renderable_object.rb', line 26

def [](key)
  tmpl_vars[key]
end

#fetch(*a) ⇒ Object



30
31
32
# File 'lib/hobber/renderable_object.rb', line 30

def fetch(*a)
  tmpl_vars.fetch(*a)
end

#render(vars = {}, ctx = Object.new, &block) ⇒ Object



18
19
20
# File 'lib/hobber/renderable_object.rb', line 18

def render(vars={}, ctx=Object.new, &block)
  @renderer.render(path, tmpl_content, ctx, tmpl_vars.merge(vars), &block)
end

#tmpl_varsObject



38
39
40
# File 'lib/hobber/renderable_object.rb', line 38

def tmpl_vars
  @tmpl_var_extractor.tmpl_vars
end

#to_aObject



22
23
24
# File 'lib/hobber/renderable_object.rb', line 22

def to_a
  [self]
end