Class: Tap::Tasks::Render

Inherits:
Dump
  • Object
show all
Defined in:
lib/tap/tasks/render.rb

Overview

:startdoc::task render an object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, app = Tap::App.instance, env = Tap::Env.instance) ⇒ Render

Returns a new instance of Render.



15
16
17
18
# File 'lib/tap/tasks/render.rb', line 15

def initialize(config={}, app=Tap::App.instance, env=Tap::Env.instance)
  super(config, app)
  @env = env
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



13
14
15
# File 'lib/tap/tasks/render.rb', line 13

def env
  @env
end

Instance Method Details

#dump(obj, io) ⇒ Object

Dumps the object to io using obj.inspect



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tap/tasks/render.rb', line 45

def dump(obj, io)
  template_path = env.class_path(dir, obj, path) do |file|
    File.file?(file)
  end
  
  unless template_path
    raise "no template for: #{obj.class}"
  end
  
  io.puts render(template_path, :locals => {:obj => obj})
end

#render(path, options = {}) ⇒ Object

Renders the specified template as ERB using the options. Options:

locals:: a hash of local variables used in the template

The filename used to identify errors in an erb template to a specific



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tap/tasks/render.rb', line 25

def render(path, options={})
  # render template
  template = File.read(path)

  # assign locals to the render binding
  # this almost surely may be optimized...
  locals = options[:locals]
  binding = empty_binding

  locals.each_pair do |key, value|
    @assignment_value = value
    eval("#{key} = remove_instance_variable(:@assignment_value)", binding)
  end if locals

  erb = ERB.new(template, nil, "<>")
  erb.filename = path
  erb.result(binding)
end