Class: ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Money

Inherits:
Type
  • Object
show all
Defined in:
lib/arjdbc/postgresql/base/oid.rb

Instance Method Summary collapse

Methods inherited from Type

#infinity, #simplified_type

Instance Method Details

#typeObject



79
# File 'lib/arjdbc/postgresql/base/oid.rb', line 79

def type; :decimal end

#type_cast(value) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/arjdbc/postgresql/base/oid.rb', line 81

def type_cast(value)
  return if value.nil?
  return value unless ::String === value

  # Because money output is formatted according to the locale, there are two
  # cases to consider (note the decimal separators):
  #  (1) $12,345,678.12
  #  (2) $12.345.678,12
  # Negative values are represented as follows:
  #  (3) -$2.55
  #  (4) ($2.55)

  value.sub!(/^\((.+)\)$/, '-\1') # (4)
  case value
  when /^-?\D+[\d,]+\.\d{2}$/  # (1)
    value.gsub!(/[^-\d.]/, '')
  when /^-?\D+[\d.]+,\d{2}$/  # (2)
    value.gsub!(/[^-\d,]/, '').sub!(/,/, '.')
  end

  ConnectionAdapters::Column.value_to_decimal value
end