Class: ParamsReady::Value::FormattedDecimalCoder

Inherits:
DecimalCoder show all
Defined in:
lib/params_ready/value/custom.rb

Constant Summary collapse

EU =
/^(\d{1,3}( \d{3})+|\d+),\d{1,2}$/.freeze
US =
/^(\d{1,3}(,\d{3})+).\d{1,2}$/.freeze

Class Method Summary collapse

Methods inherited from DecimalCoder

format

Methods inherited from Coder

instance

Methods included from Coercion

#strict_default?, #try_coerce

Methods inherited from AbstractCoder

value_class_name

Methods included from Extensions::ClassReaderWriter

#class_reader_writer

Class Method Details

.coerce(value, context) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/params_ready/value/custom.rb', line 18

def self.coerce(value, context)
  value = if value.is_a? String
    stripped = value.strip
    if EU.match? stripped
      stripped.gsub(/[ ,]/, ' ' => '', ',' => '.')
    elsif US.match? stripped
      stripped.delete(',')
    else
      stripped
    end
  else
    value
  end
  super
end