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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#interpolate

Constructor Details

#initialize(symbols = {}) ⇒ Fraction

Returns a new instance of Fraction.



9
10
11
12
13
14
15
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 9

def initialize(symbols = {})
  @precision = 3
  @decimal = symbols[:decimal] || '.'
  @separator = symbols[:fraction_group].to_s
  @group = symbols[:fraction_group_digits]
  @digit_count = symbols[:digit_count] || nil
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, options = {}, int = "") ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 17

def apply(fraction, options = {}, int = "")
  precision = options[:precision] || @precision
  return "" unless precision > 0

  number = interpolate(format(options), fraction, :left)
  number = digit_count_format(int, fraction, number) if @digit_count
  formatted_number = change_format(number) if number
  formatted_number ? decimal + formatted_number : ""
end

#format(options) ⇒ Object



27
28
29
30
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 27

def format(options)
  precision = options[:precision] || @precision
  precision ? '0' * precision : @format
end

#format_groups(string) ⇒ Object



32
33
34
# File 'lib/plurimath/formatter/numbers/fraction.rb', line 32

def format_groups(string)
  change_format(string)
end