Class: Saga::Formatter

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

Constant Summary collapse

TEMPLATE_PATH =
File.expand_path('../../../templates', __FILE__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ Formatter

Returns a new instance of Formatter.



7
8
9
10
11
# File 'lib/saga/formatter.rb', line 7

def initialize(document, options={})
  @document = document
  @options  = options
  @options[:template] ||= 'default'
end

Class Method Details

.format(document, options = {}) ⇒ Object



26
27
28
29
# File 'lib/saga/formatter.rb', line 26

def self.format(document, options={})
  formatter = new(document, options)
  formatter.format
end

.template_pathObject



31
32
33
# File 'lib/saga/formatter.rb', line 31

def self.template_path
  TEMPLATE_PATH
end

Instance Method Details

#formatObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/saga/formatter.rb', line 13

def format
  template_path = File.join(self.class.template_path, @options[:template])
  
  helpers_file = File.join(template_path, 'helpers.rb')
  load helpers_file
  @document.extend(Helpers)
  binding = @document.send(:binding)
  
  template_file = File.join(template_path, 'document.erb')
  template = Erubis::Eruby.new(File.read(template_file))
  template.result(binding)
end