Class: NumericString::Config

Inherits:
Struct
  • Object
show all
Defined in:
lib/core_ext/numeric_string.rb

Overview

You can control how NumericString behaves by supplying a new NumericString::Config to the config: parameter of these methods. Calling NumericString::Config.build gives you the deafult configuration with whatever overrides you give it. You can also modify an existing config with some overrides. For example:

+begin_src ruby

cfg = NumericString::Config.build(group_size: 4) cfg2 = cfg.with(group_char: '_')

"1234567.89".add_grouping(config: cfg) => "123,4567.89" "1234567.89".add_grouping(config: cfg2) => '123_4567.89'

+end_src

Constant Summary collapse

DEFAULTS =
{
  group_char: ',',
  group_size: 3,
  decimal_char: '.',
  currency_symbol: '$',
  pre_pad_char: '0',
  post_pad_char: '0',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#currency_symbolObject

Returns the value of attribute currency_symbol

Returns:

  • (Object)

    the current value of currency_symbol



24
25
26
# File 'lib/core_ext/numeric_string.rb', line 24

def currency_symbol
  @currency_symbol
end

#decimal_charObject

Returns the value of attribute decimal_char

Returns:

  • (Object)

    the current value of decimal_char



24
25
26
# File 'lib/core_ext/numeric_string.rb', line 24

def decimal_char
  @decimal_char
end

#group_charObject

Returns the value of attribute group_char

Returns:

  • (Object)

    the current value of group_char



24
25
26
# File 'lib/core_ext/numeric_string.rb', line 24

def group_char
  @group_char
end

#group_sizeObject

Returns the value of attribute group_size

Returns:

  • (Object)

    the current value of group_size



24
25
26
# File 'lib/core_ext/numeric_string.rb', line 24

def group_size
  @group_size
end

#post_pad_charObject

Returns the value of attribute post_pad_char

Returns:

  • (Object)

    the current value of post_pad_char



24
25
26
# File 'lib/core_ext/numeric_string.rb', line 24

def post_pad_char
  @post_pad_char
end

#pre_pad_charObject

Returns the value of attribute pre_pad_char

Returns:

  • (Object)

    the current value of pre_pad_char



24
25
26
# File 'lib/core_ext/numeric_string.rb', line 24

def pre_pad_char
  @pre_pad_char
end

Class Method Details

.build(**overrides) ⇒ Object

Build from defaults, overriding selectively



47
48
49
# File 'lib/core_ext/numeric_string.rb', line 47

def self.build(**overrides)
  new(**DEFAULTS.merge(overrides)).freeze
end

.defaultObject



42
43
44
# File 'lib/core_ext/numeric_string.rb', line 42

def self.default
  @default ||= new(**DEFAULTS).freeze
end

Instance Method Details

#with(**overrides) ⇒ Object

Clone-with-changes (very Ruby, very nice)



52
53
54
55
56
57
58
59
60
61
# File 'lib/core_ext/numeric_string.rb', line 52

def with(**overrides)
  self.class.build(
    group_char:      overrides.fetch(:group_char, group_char),
    group_size:      overrides.fetch(:group_size, group_size),
    decimal_char:    overrides.fetch(:decimal_char, decimal_char),
    currency_symbol: overrides.fetch(:currency_symbol, currency_symbol),
    pre_pad_char: overrides.fetch(:pre_pad_char, pre_pad_char),
    post_pad_char: overrides.fetch(:post_pad_char, post_pad_char),
  )
end