Class: ActiveRecord::ConnectionAdapters::PostgreSQLColumn

Inherits:
Object
  • Object
show all
Includes:
PgArrayParser
Defined in:
lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



9
10
11
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 9

def array
  @array
end

Class Method Details

.string_to_cidr_address(string) ⇒ Object



87
88
89
90
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 87

def string_to_cidr_address(string)
  return string unless String === string
  return IPAddr.new(string)
end

Instance Method Details

#initialize_with_extended_types(name, default, sql_type = nil, null = true) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 10

def initialize_with_extended_types(name, default, sql_type = nil, null = true)
  if sql_type =~ /\[\]$/
    @array = true
    initialize_without_extended_types(name, default, sql_type[0..sql_type.length - 3], null)
    @sql_type = sql_type
  else
    initialize_without_extended_types(name,default, sql_type, null)
  end
end

#klass_with_extended_typesObject



20
21
22
23
24
25
26
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 20

def klass_with_extended_types
  case type
  when :inet, :cidr   then IPAddr
  else
    klass_without_extended_types
  end
end

#string_to_array(value) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 46

def string_to_array(value)
  if Array === value
    value
  else
    string_array = parse_pg_array value
    if type == :string
      string_array
    else
      type_cast_array(string_array)
    end
  end
end

#type_cast_array(array) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 59

def type_cast_array(array)
  casted_array = []
  array.each do |value|
    if Array === value
      casted_array.push type_cast_array(value)
    else
      casted_array.push type_cast value
    end
  end
  casted_array
end

#type_cast_code_with_extended_types(var_name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 71

def type_cast_code_with_extended_types(var_name)
  klass = self.class.name

  if self.array
    "#{klass}.new('#{self.name}', #{self.default.nil? ? 'nil' : "'#{self.default}'"}, '#{self.sql_type}').string_to_array(#{var_name})"
  else
    case type
    when :inet, :cidr   then "#{klass}.string_to_cidr_address(#{var_name})"
    else
      type_cast_code_without_extended_types(var_name)
    end
  end
end

#type_cast_with_extended_types(value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 28

def type_cast_with_extended_types(value)
  return nil if value.nil?
  return coder.load(value) if encoded?

  klass = self.class
  if self.array && String === value && value.start_with?('{') && value.end_with?('}')
    string_to_array value
  elsif self.array && Array === value
    value
  else
    case type
    when :inet, :cidr   then klass.string_to_cidr_address(value)
    else 
      type_cast_without_extended_types(value)
    end
  end
end