Class: ActiveRecord::ConnectionAdapters::PostgreSQLColumn

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

Overview

:nodoc:

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.



14
15
16
17
18
19
20
# File 'lib/ar_pg_array/schema.rb', line 14

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
  super(name, self.class.extract_value_from_default(default), sql_type, null)
end

Instance Attribute Details

#base_columnObject (readonly)

Returns the value of attribute base_column.



12
13
14
# File 'lib/ar_pg_array/schema.rb', line 12

def base_column
  @base_column
end

Class Method Details

._string_to_array(string) ⇒ Object



73
74
75
76
77
78
# File 'lib/ar_pg_array/schema.rb', line 73

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



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

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



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

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



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

def self.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_pg_array/schema.rb', line 131

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



80
81
82
83
84
85
# File 'lib/ar_pg_array/schema.rb', line 80

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

  yield
end

#defaultObject



68
69
70
71
# File 'lib/ar_pg_array/schema.rb', line 68

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

#klassObject



31
32
33
34
35
36
37
# File 'lib/ar_pg_array/schema.rb', line 31

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

#safe_string_to_array(string) ⇒ Object



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

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



22
23
24
25
26
27
28
# File 'lib/ar_pg_array/schema.rb', line 22

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



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

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



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

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

#string_to_text_array(string) ⇒ Object



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

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

#type_cast(value) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ar_pg_array/schema.rb', line 39

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



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ar_pg_array/schema.rb', line 54

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