Module: JdbcSpec::PostgreSQL::Column

Defined in:
lib/jdbc_adapter/jdbc_postgre.rb

Instance Method Summary collapse

Instance Method Details

#cast_to_boolean(value) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/jdbc_adapter/jdbc_postgre.rb', line 60

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)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jdbc_adapter/jdbc_postgre.rb', line 70

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



49
50
51
52
53
54
55
56
57
58
# File 'lib/jdbc_adapter/jdbc_postgre.rb', line 49

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|^money/i
  return :binary if field_type =~ /^bytea/i
  return :boolean if field_type =~ /^bool/i
  super
end

#type_cast(value) ⇒ Object



42
43
44
45
46
47
# File 'lib/jdbc_adapter/jdbc_postgre.rb', line 42

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