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

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

Constant Summary collapse

DEFAULT_PRECISION =
3
DEFAULT_STRINGS =
{ empty: "", zero: "0", dot: ".", f: "F" }.freeze

Constants inherited from Base

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

Instance Attribute Summary collapse

Attributes inherited from Base

#base, #symbols

Instance Method Summary collapse

Constructor Details

#initialize(symbols = {}) ⇒ Fraction

Returns a new instance of Fraction.



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

def initialize(symbols = {})
  super
  @group       = symbols[:fraction_group_digits]
  @decimal     = symbols.fetch(:decimal, DEFAULT_STRINGS[:dot])
  @int_group   = symbols[:group]
  @separator   = symbols[:fraction_group].to_s
  @precision   = symbols.fetch(:precision, DEFAULT_PRECISION)
  @digit_count = symbols[:digit_count].to_i
end

Instance Attribute Details

#decimalObject (readonly)

Returns the value of attribute decimal.



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

def decimal
  @decimal
end

#groupObject (readonly)

Returns the value of attribute group.



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

def group
  @group
end

#precisionObject (readonly)

Returns the value of attribute precision.



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

def precision
  @precision
end

#separatorObject (readonly)

Returns the value of attribute separator.



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

def separator
  @separator
end

Instance Method Details

#apply(fraction, result, integer_formatter) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 22

def apply(fraction, result, integer_formatter)
  precision = symbols[:precision] || @precision
  @result = result
  @integer_formatter = integer_formatter
  return DEFAULT_STRINGS[:empty] unless precision.positive?

  fraction = change_base(fraction) if !base_default? && fraction.match?(/[1-9]/)

  number = if @digit_count.positive?
             digit_count_format(fraction)
           else
             format(fraction, precision)
           end
  formatted_number = format_groups(number) if number && !number.empty?
  formatted_number ? decimal + formatted_number : DEFAULT_STRINGS[:empty]
end

#format(number, precision) ⇒ Object



39
40
41
42
43
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 39

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

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

#format_groups(string, length = group) ⇒ Object



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

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

  change_format(string, length)
end