Module: ArJdbc::PostgreSQL::Column

Defined in:
lib/arjdbc/postgresql/adapter.rb

Instance Method Summary collapse

Instance Method Details

#cast_to_boolean(value) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/arjdbc/postgresql/adapter.rb', line 40

def cast_to_boolean(value)
  return nil if value.nil?
  if value == true || value == false
    value
  else
    %w(true t 1).include?(value.to_s.downcase)
  end
end

#default_value(value) ⇒ Object

Post process default value from JDBC into a Rails-friendly format (columns-internal)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/arjdbc/postgresql/adapter.rb', line 50

def default_value(value)
  # Boolean types
  return "t" if value =~ /true/i
  return "f" if value =~ /false/i

  # Char/String/Bytea type values
  return $1 if value =~ /^'(.*)'::(bpchar|text|character varying|bytea)$/

  # Numeric values
  return value if value =~ /^-?[0-9]+(\.[0-9]*)?/

  # Fixed dates / timestamp
  return $1 if value =~ /^'(.+)'::(date|timestamp)/

  # Anything else is blank, some user type, or some function
  # and we can't know the value of that, so return nil.
  return nil
end

#simplified_type(field_type) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/arjdbc/postgresql/adapter.rb', line 29

def simplified_type(field_type)
  return :integer if field_type =~ /^serial/i
  return :string if field_type =~ /\[\]$/i || field_type =~ /^interval/i
  return :string if field_type =~ /^(?:point|lseg|box|"?path"?|polygon|circle)/i
  return :datetime if field_type =~ /^timestamp/i
  return :float if field_type =~ /^(?:real|double precision)$/i
  return :binary if field_type =~ /^bytea/i
  return :boolean if field_type =~ /^bool/i
  super
end

#type_cast(value) ⇒ Object



22
23
24
25
26
27
# File 'lib/arjdbc/postgresql/adapter.rb', line 22

def type_cast(value)
  case type
  when :boolean then cast_to_boolean(value)
  else super
  end
end