blob: 4e7d472d97547f24bf1aa3b1477498e857b6f09b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module OID # :nodoc:
class Array < Type::Value
attr_reader :subtype
delegate :type, to: :subtype
def initialize(subtype)
@subtype = subtype
end
def type_cast_from_database(value)
if value.is_a?(::String)
type_cast_array(parse_pg_array(value), :type_cast_from_database)
else
super
end
end
def type_cast_from_user(value)
type_cast_array(value, :type_cast_from_user)
end
# Loads pg_array_parser if available. String parsing can be
# performed quicker by a native extension, which will not create
# a large amount of Ruby objects that will need to be garbage
# collected. pg_array_parser has a C and Java extension
begin
require 'pg_array_parser'
include PgArrayParser
rescue LoadError
require 'active_record/connection_adapters/postgresql/array_parser'
include PostgreSQL::ArrayParser
end
private
def type_cast_array(value, method)
if value.is_a?(::Array)
value.map { |item| type_cast_array(item, method) }
else
@subtype.public_send(method, value)
end
end
end
end
end
end
end
|