Class: Chuusha::Renderer

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

Instance Method Summary collapse

Constructor Details

#initialize(config, path, output_dir) ⇒ Renderer

Returns a new instance of Renderer.



81
82
83
84
85
86
# File 'lib/chuusha.rb', line 81

def initialize(config, path, output_dir)
  @config = config
  @outfile = File.join(output_dir, File.basename(path))
  @path = path + ".erb"
  @evaluated = nil
end

Instance Method Details

#evaluatedObject



92
93
94
95
96
# File 'lib/chuusha.rb', line 92

def evaluated
  return @evaluated if @evaluated
  eruby = Erubis::Eruby.new(::File.read(@path))
  @evaluated = eruby.result(@config.variables)
end

#renderObject



103
104
105
106
# File 'lib/chuusha.rb', line 103

def render
  write_cached_copy if @config.cache?
  evaluated
end

#respondObject



108
109
110
# File 'lib/chuusha.rb', line 108

def respond
  [200, {"Content-Type" => ::Rack::Mime.mime_type(File.extname(@outfile))}, render]
end

#template_exists?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/chuusha.rb', line 88

def template_exists?
  ::File.exist?(@path)
end

#write_cached_copyObject



98
99
100
101
# File 'lib/chuusha.rb', line 98

def write_cached_copy
  FileUtils.mkdir_p(File.dirname(@outfile))
  ::File.open(@outfile, "w") { |f| f.write evaluated }
end