Class: NumericString::Config
- Inherits:
-
Struct
- Object
- Struct
- NumericString::Config
- 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
-
#currency_symbol ⇒ Object
Returns the value of attribute currency_symbol.
-
#decimal_char ⇒ Object
Returns the value of attribute decimal_char.
-
#group_char ⇒ Object
Returns the value of attribute group_char.
-
#group_size ⇒ Object
Returns the value of attribute group_size.
-
#post_pad_char ⇒ Object
Returns the value of attribute post_pad_char.
-
#pre_pad_char ⇒ Object
Returns the value of attribute pre_pad_char.
Class Method Summary collapse
-
.build(**overrides) ⇒ Object
Build from defaults, overriding selectively.
- .default ⇒ Object
Instance Method Summary collapse
-
#with(**overrides) ⇒ Object
Clone-with-changes (very Ruby, very nice).
Instance Attribute Details
#currency_symbol ⇒ Object
Returns the value of attribute currency_symbol
24 25 26 |
# File 'lib/core_ext/numeric_string.rb', line 24 def currency_symbol @currency_symbol end |
#decimal_char ⇒ Object
Returns the value of attribute decimal_char
24 25 26 |
# File 'lib/core_ext/numeric_string.rb', line 24 def decimal_char @decimal_char end |
#group_char ⇒ Object
Returns the value of attribute group_char
24 25 26 |
# File 'lib/core_ext/numeric_string.rb', line 24 def group_char @group_char end |
#group_size ⇒ Object
Returns the value of attribute group_size
24 25 26 |
# File 'lib/core_ext/numeric_string.rb', line 24 def group_size @group_size end |
#post_pad_char ⇒ Object
Returns the value of attribute 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_char ⇒ Object
Returns the value of attribute 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 |
.default ⇒ Object
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 |