Class: Saga::Formatter

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, template_path: nil) ⇒ Formatter

Returns a new instance of Formatter.



10
11
12
13
# File 'lib/saga/formatter.rb', line 10

def initialize(document, template_path: nil)
  @document = document
  @template_path ||= template_path || File.join(self.class.template_path, 'default')
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



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

def template_path
  @template_path
end

Class Method Details

.format(document, **kwargs) ⇒ Object



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

def self.format(document, **kwargs)
  formatter = new(document, **kwargs)
  formatter.format
end

.saga_format(document) ⇒ Object



38
39
40
# File 'lib/saga/formatter.rb', line 38

def self.saga_format(document)
  format(document, template_path: File.join(template_path, 'saga'))
end

.template_pathObject



34
35
36
# File 'lib/saga/formatter.rb', line 34

def self.template_path
  TEMPLATE_PATH
end

Instance Method Details

#formatObject



19
20
21
22
23
24
25
26
27
# File 'lib/saga/formatter.rb', line 19

def format
  @document.extend(ERB::Util) unless @document.is_a?(ERB::Util)

  if File.exist?(helpers_file)
    @document.instance_eval(File.read(helpers_file))
  end

  template.result(@document._binding)
end

#templateObject



15
16
17
# File 'lib/saga/formatter.rb', line 15

def template
  @template ||= build_template
end