Class: Hyraft::Compiler::HyraftCompiler

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

Instance Method Summary collapse

Constructor Details

#initialize(layout_file = nil) ⇒ HyraftCompiler

Returns a new instance of HyraftCompiler.



8
9
10
11
12
# File 'lib/hyraft/compiler/compiler.rb', line 8

def initialize(layout_file = nil)
  @layout_file = layout_file
  @renderer = HyraftRenderer.new
  @cache = {}
end

Instance Method Details

#compile(view_path, data = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hyraft/compiler/compiler.rb', line 14

def compile(view_path, data = {})
  full_path = resolve_view_path(view_path)
  
  if ENV['HYRAFT_ENV'] == 'production'
    cache_key = "#{full_path}:#{File.mtime(full_path)}"
    return @cache[cache_key] if @cache[cache_key]
  end
  
  content = File.read(full_path)
  parsed = HyraftParser.new(content).parse
  
  result = if @layout_file && File.exist?(@layout_file)
    layout_content = File.read(@layout_file)
    @renderer.render_with_layout(parsed, layout_content, data)
  else
    @renderer.render(parsed, data)
  end
  
  @cache[cache_key] = result if ENV['HYRAFT_ENV'] == 'production'
  result
end

#compile_raw(view_path, data = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/hyraft/compiler/compiler.rb', line 36

def compile_raw(view_path, data = {})
  full_path = resolve_view_path(view_path)
  content = File.read(full_path)
  parsed = HyraftParser.new(content).parse
  @renderer.render_raw(parsed, data)
end