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
# File 'lib/plurimath/formatter/numbers/number_renderer.rb', line 11

def initialize(source, options)
  @source = source
  @options = options
  @base_notation = BaseNotation.from_options(@options)
  @integer_format = Integer.new(@options)
  @fraction_format = Fraction.new(@options)
  @significant_format = Significant.new(@options)
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



20
21
22
23
24
25
26
27
# File 'lib/plurimath/formatter/numbers/number_renderer.rb', line 20

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

#format_parts(parts, precision:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/plurimath/formatter/numbers/number_renderer.rb', line 29

def format_parts(parts, precision:)
  decimal_precision = decimal_precision_for(precision)
  unless decimal_precision.nil?
    parts = parts.with_digits(
      fraction_digits: parts.fraction_digits[0...decimal_precision.to_i].to_s,
    )
  end
  parts = renderable_parts(parts, precision: precision)

  parts = significant_format.apply_parts(parts) if significant_format.active?

  FormattedNumber.new(
    sign: parts.sign,
    integer_part: integer_format.format_groups(parts.integer_digits),
    fraction_part: parts.fractional? ? fraction_format.format_groups(parts.fraction_digits) : "",
    decimal_separator: fraction_format.decimal,
    base_notation: base_notation,
    number_sign: options.number_sign,
  )
end