Class: Plurimath::Formatter::Numbers::Fraction

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

Overview

Transforms fraction digits on Parts before localized rendering.

Constant Summary collapse

DEFAULT_PRECISION =
FormatOptions::DEFAULT_FRACTION_PRECISION
DEFAULT_STRINGS =
{ empty: "", zero: "0", dot: "." }.freeze

Constants inherited from Base

Base::DEFAULT_BASE, Base::DIGIT_VALUE, Base::HEX_ALPHANUMERIC, Base::HEX_DIGITS

Instance Attribute Summary collapse

Attributes inherited from Base

#base, #options

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Fraction

Returns a new instance of Fraction.



13
14
15
16
17
18
19
20
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 13

def initialize(options)
  super
  @group = self.options.fraction_group_digits
  @decimal = self.options.decimal
  @separator = self.options.fraction_group
  @precision = self.options.precision || DEFAULT_PRECISION
  @digit_count = self.options.digit_count
end

Instance Attribute Details

#decimalObject (readonly)

Returns the value of attribute decimal.



8
9
10
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 8

def decimal
  @decimal
end

#groupObject (readonly)

Returns the value of attribute group.



8
9
10
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 8

def group
  @group
end

#precisionObject (readonly)

Returns the value of attribute precision.



8
9
10
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 8

def precision
  @precision
end

#separatorObject (readonly)

Returns the value of attribute separator.



8
9
10
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 8

def separator
  @separator
end

Instance Method Details

#apply_parts(parts, precision: self.precision) ⇒ Object

Keep fraction preparation on structured parts; localized rendering and grouping happen later at the PartsRenderer boundary.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 24

def apply_parts(parts, precision: self.precision)
  precision = precision.to_i
  return parts.with_digits(fraction_digits: DEFAULT_STRINGS[:empty]) unless precision.positive?

  @integer_digits = parts.integer_digits

  fraction = parts.fraction_digits
  fraction = change_base(fraction, precision) if !base_default? && fraction.match?(/[1-9]/)
  number = if @digit_count.positive?
             digit_count_format(fraction, precision)
           else
             format(fraction, precision)
           end

  parts.with_digits(
    integer_digits: integer_digits,
    fraction_digits: number.to_s,
  )
end

#format(number, precision) ⇒ Object



44
45
46
47
48
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 44

def format(number, precision)
  return number if precision <= number.length

  number + (DEFAULT_STRINGS[:zero] * (precision - number.length))
end

#format_groups(string, length = group) ⇒ Object



50
51
52
53
54
55
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 50

def format_groups(string, length = group)
  string = capitalize_hex_digits(string)
  length = string.length if group.to_i.zero?

  change_format(string, length)
end