Class: Sangi::Exporter::TextExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/sangi/exporter/text_exporter.rb

Constant Summary collapse

SEPARATOR =
"=" * 60

Instance Method Summary collapse

Constructor Details

#initialize(config, renderer) ⇒ TextExporter

Returns a new instance of TextExporter.



6
7
8
9
# File 'lib/sangi/exporter/text_exporter.rb', line 6

def initialize(config, renderer)
  @config = config
  @renderer = renderer
end

Instance Method Details

#build_text(steps) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sangi/exporter/text_exporter.rb', line 17

def build_text(steps)
  first = steps.first
  state = first&.numeric_state || {}
  lines = [
    "sangi calculation log",
    "version: #{VERSION}",
    "expr: #{state[:source]}",
    "normalized: #{state[:normalized]}",
    "mode: #{@config.mode}",
    "zero: #{@config.zero_mode}",
    "sign: #{@config.sign_mode}",
    "explain: #{@config.explain_mode}",
    ""
  ]
  steps.each do |step|
    lines << SEPARATOR
    lines << "step #{step.index}/#{steps.size}"
    lines << @renderer.render(step, total_steps: steps.size).chomp
  end
  lines << SEPARATOR unless steps.empty?
  lines.join("\n") + "\n"
end

#export(path, steps) ⇒ Object



11
12
13
14
15
# File 'lib/sangi/exporter/text_exporter.rb', line 11

def export(path, steps)
  File.write(path, build_text(steps), mode: "w:UTF-8")
rescue SystemCallError
  raise ExportError, "ログファイルを書き込めません: #{path}"
end