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



121
122
123
124
125
126
127
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 121

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

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

.string_to_date_range(value) ⇒ Object



141
142
143
144
145
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 141

def string_to_date_range(value)
  extract_range(value) do |end_value|
    Date.parse(end_value)
  end
end

.string_to_datetime_range(value) ⇒ Object



147
148
149
150
151
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 147

def string_to_datetime_range(value)
  extract_range(value) do |end_value|
    Time.parse(end_value)
  end
end

.string_to_numeric_range(value, type) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 129

def string_to_numeric_range(value, type)
  if type == :numrange
    extract_range(value) do |end_value|
      end_value.to_f
    end
  else
    extract_range(value) do |end_value|
      end_value.to_i
    end
  end
end

Instance Method Details

#default_value_with_extended_types(default) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 95

def default_value_with_extended_types(default)
  case default
  when /\A'(.*)'::(?:(num|int[48]|date|ts(tz)?)range)\z/
    $1
  else
    default_value_without_extended_types(default)
  end

end

#extract_value_from_default_with_extended_types(default) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb', line 109

def extract_value_from_default_with_extended_types(default)
  case default
  when /\A'(.*)'::(?:(num|int[48]|date|ts(tz)?)range)\z/
    $1
  else
    extract_value_from_default_without_extended_types(default)
  end

end

#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)


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

def number?
  !self.array && super
end

#string_to_array(value) ⇒ Object



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

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

#type_cast_array(array) ⇒ Object



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

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



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

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})"
    when :numrange,:int4range,:int8range then "#{klass}.string_to_numeric_range(#{var_name},#{type.inspect})"
    when :daterange                      then "#{klass}.string_to_date_range(#{var_name})"
    when :tsrange,:tstzrange             then "#{klass}.string_to_datetime_range(#{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
52
53
54
# 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)
    when :numrange,:int4range,:int8range then klass.string_to_numeric_range(value,type)
    when :daterange                      then klass.string_to_date_range(value)
    when :tsrange,:tstzrange             then klass.string_to_datetime_range(value)
    else
      type_cast_without_extended_types(value)
    end
  end
end