Class: ActiveRecord::ConnectionAdapters::PostgreSQLColumn

Inherits:
Object
  • Object
show all
Extended by:
PgArrayParser
Includes:
PgArrayParser
Defined in:
lib/ar_jdbc_pg_array/schema.rb

Constant Summary collapse

BASE_TYPE_COLUMNS =
Hash.new { |h, base_type|
  base_column = new(nil, nil, base_type.to_s, true)
  h[base_type] = h[base_column.type] = base_column
}

Constants included from PgArrayParser

PgArrayParser::CURLY_BRACKETS, PgArrayParser::ESCAPE_HASH, PgArrayParser::NIL, PgArrayParser::NULL, PgArrayParser::SQUARE_BRACKETS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PgArrayParser

_parse_pgarray, _parse_safe_pgarray, _prepare_pg_string_array, _remap_array, parse_numeric_pgarray, parse_pgarray, parse_safe_pgarray, prepare_pg_float_array, prepare_pg_integer_array, prepare_pg_safe_array, prepare_pg_text_array

Constructor Details

#initialize(name, default, sql_type = nil, null = true) ⇒ PostgreSQLColumn

Returns a new instance of PostgreSQLColumn.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ar_jdbc_pg_array/schema.rb', line 15

def initialize(name, default, sql_type = nil, null = true)
  if sql_type =~ /^(.+)\[\]$/
    @base_sql_type = $1
    @base_column = BASE_TYPE_COLUMNS[@base_sql_type]
  end

  if Hash === name
    super
  else
    super(nil, name, default, sql_type, null)
  end
end

Instance Attribute Details

#base_columnObject (readonly)

Returns the value of attribute base_column.



13
14
15
# File 'lib/ar_jdbc_pg_array/schema.rb', line 13

def base_column
  @base_column
end

Class Method Details

._string_to_array(string) ⇒ Object



79
80
81
82
83
84
# File 'lib/ar_jdbc_pg_array/schema.rb', line 79

def self._string_to_array(string)
  return string unless string.is_a? String
  return nil if string.empty?

  yield
end

.safe_string_to_array(string, sql_type) ⇒ Object



99
100
101
102
103
104
# File 'lib/ar_jdbc_pg_array/schema.rb', line 99

def self.safe_string_to_array(string, sql_type)
  _string_to_array(string) do
    base_column = BASE_TYPE_COLUMNS[sql_type]
    parse_safe_pgarray(string){|v| base_column.type_cast(v)}
  end
end

.string_to_array(string, sql_type) ⇒ Object



112
113
114
115
116
117
# File 'lib/ar_jdbc_pg_array/schema.rb', line 112

def self.string_to_array(string, sql_type)
  _string_to_array(string) do
    base_column = BASE_TYPE_COLUMNS[sql_type]
    parse_pgarray(string){|v| base_column.type_cast(v)}
  end
end

.string_to_num_array(string) ⇒ Object



125
126
127
128
129
# File 'lib/ar_jdbc_pg_array/schema.rb', line 125

def self.string_to_num_array(string)
  _string_to_array(string) do
    parse_numeric_pgarray(string)
  end
end

.string_to_text_array(string) ⇒ Object



137
138
139
140
141
# File 'lib/ar_jdbc_pg_array/schema.rb', line 137

def self.string_to_text_array(string)
  _string_to_array(string) do
    parse_pgarray(string){|v| v}
  end
end

Instance Method Details

#_string_to_array(string) ⇒ Object



86
87
88
89
90
91
# File 'lib/ar_jdbc_pg_array/schema.rb', line 86

def _string_to_array(string)
  return string unless string.is_a? String
  return nil if string.empty?

  yield
end

#defaultObject



74
75
76
77
# File 'lib/ar_jdbc_pg_array/schema.rb', line 74

def default
  res = super
  Array === res ? res.dup : res
end

#klassObject



37
38
39
40
41
42
43
# File 'lib/ar_jdbc_pg_array/schema.rb', line 37

def klass
  if type.to_s =~ /_array$/
    Array
  else
    super
  end
end

#safe_string_to_array(string) ⇒ Object



93
94
95
96
97
# File 'lib/ar_jdbc_pg_array/schema.rb', line 93

def safe_string_to_array(string)
  _string_to_array(string) do
    parse_safe_pgarray(string){|v| @base_column.type_cast(v)}
  end
end

#simplified_type_with_postgresql_arrays(field_type) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/ar_jdbc_pg_array/schema.rb', line 28

def simplified_type_with_postgresql_arrays(field_type)
  if field_type =~ /^(.+)\[\]$/
    :"#{simplified_type_without_postgresql_arrays($1)}_array"
  else
    simplified_type_without_postgresql_arrays(field_type)
  end
end

#string_to_array(string) ⇒ Object



106
107
108
109
110
# File 'lib/ar_jdbc_pg_array/schema.rb', line 106

def string_to_array(string)
  _string_to_array(string) do
    parse_pgarray(string){|v| @base_column.type_cast(v)}
  end
end

#string_to_num_array(string) ⇒ Object



119
120
121
122
123
# File 'lib/ar_jdbc_pg_array/schema.rb', line 119

def string_to_num_array(string)
  _string_to_array(string) do
    parse_numeric_pgarray(string)
  end
end

#string_to_text_array(string) ⇒ Object



131
132
133
134
135
# File 'lib/ar_jdbc_pg_array/schema.rb', line 131

def string_to_text_array(string)
  _string_to_array(string) do
    parse_pgarray(string){|v| v}
  end
end

#type_cast(value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ar_jdbc_pg_array/schema.rb', line 45

def type_cast(value)
  return nil if value.nil?
  case type
    when :integer_array, :float_array
      string_to_num_array(value)
    when :decimal_array, :date_array, :boolean_array
      safe_string_to_array(value)
    when :timestamp_array, :time_array, :datetime_array, :binary_array
      string_to_array(value)
    when :text_array, :string_array
      string_to_text_array(value)
    else super
  end
end

#type_cast_code(var_name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ar_jdbc_pg_array/schema.rb', line 60

def type_cast_code(var_name)
  case type
    when :integer_array, :float_array
      "#{self.class.name}.string_to_num_array(#{var_name})"
    when :decimal_array, :date_array, :boolean_array
      "#{self.class.name}.safe_string_to_array(#{var_name}, #{@base_sql_type.inspect})"
    when :timestamp_array, :time_array, :datetime_array, :binary_array
      "#{self.class.name}.string_to_array(#{var_name}, #{@base_sql_type.inspect})"
    when :text_array, :string_array
      "#{self.class.name}.string_to_text_array(#{var_name})"
    else super
  end
end