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.



13
14
15
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 13

def array
  @array
end

Class Method Details

.string_to_cidr_address(string) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 93

def string_to_cidr_address(string)
  return string unless String === string

  if string.present?
    IPAddr.new(string)
  end
end

Instance Method Details

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



15
16
17
18
19
20
21
22
23
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 15

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



26
27
28
29
30
31
32
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 26

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

#number?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 73

def number?
  !self.array && super
end

#string_to_array(value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 54

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

#type_cast_array(array) ⇒ Object



67
68
69
70
71
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 67

def type_cast_array(array)
  array.map do |value|
    Array === value ? type_cast_array(value) : type_cast(value)
  end
end

#type_cast_code_with_extended_types(var_name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 77

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 35

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