Class: Plurimath::Formatter::Numbers::NumberRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/formatter/numbers/number_renderer.rb

Overview

Orchestrates Source and Parts through numeric transforms and final renderers; low-level digit rules stay in composed helpers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ NumberRenderer

Returns a new instance of NumberRenderer.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/plurimath/formatter/numbers/number_renderer.rb', line 11

def initialize(source, options)
  @source = source
  @options = options
  @base_notation = BaseNotation.new(@options)
  @integer_format = Integer.new(@options)
  @fraction_format = Fraction.new(@options)
  @significant_format = Significant.new(@options)
  @parts_renderer = PartsRenderer.new(
    integer_formatter: @integer_format,
    fraction_formatter: @fraction_format,
  )
  @sign_renderer = SignRenderer.new(@options.number_sign)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/plurimath/formatter/numbers/number_renderer.rb', line 9

def options
  @options
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/plurimath/formatter/numbers/number_renderer.rb', line 9

def source
  @source
end

Instance Method Details

#format(precision: nil) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/plurimath/formatter/numbers/number_renderer.rb', line 25

def format(precision: nil)
  render_precision = precision || options.precision || source_precision
  parts = source.to_parts(
    base: options.base,
    precision: render_precision,
  )
  format_parts(parts, precision: render_precision)
end

#format_parts(parts, precision:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/plurimath/formatter/numbers/number_renderer.rb', line 34

def format_parts(parts, precision:)
  parts = parts.with_digits(
    fraction_digits: parts.fraction_digits[0...precision.to_i].to_s,
  )
  parts = renderable_parts(parts, precision: precision)

  parts = significant_format.apply_parts(parts) if significant_format.active?
  result = parts_renderer.render(parts)

  sign_renderer.apply(parts, base_notation.apply(result))
end