Class: Plurimath::Formatter::Numbers::Integer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#interpolate

Constructor Details

#initialize(symbols = {}) ⇒ Integer

Returns a new instance of Integer.



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

def initialize(symbols = {})
  @groups    = Array(symbols[:group_digits] || 3)
  @separator = symbols[:group] || ','
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#groupsObject (readonly)

Returns the value of attribute groups.



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

def groups
  @groups
end

#separatorObject (readonly)

Returns the value of attribute separator.



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

def separator
  @separator
end

Instance Method Details

#apply(number, options = {}) ⇒ Object



14
15
16
# File 'lib/plurimath/formatter/numbers/integer.rb', line 14

def apply(number, options = {})
  format_groups(interpolate(number, number.to_i))
end

#chop_group(string, size) ⇒ Object



29
30
31
# File 'lib/plurimath/formatter/numbers/integer.rb', line 29

def chop_group(string, size)
  string.slice!([string.size - size, 0].max, size)
end

#format_groups(string) ⇒ Object



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

def format_groups(string)
  return string if groups.empty?

  tokens = []

  tokens << chop_group(string, groups.first)
  tokens << chop_group(string, groups.last) until string.empty?

  tokens.compact.reverse.join(separator)
end