Class: ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array

Inherits:
Type::Value
  • Object
show all
Includes:
ActiveModel::Type::Helpers::Mutable
Defined in:
activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ActiveModel::Type::Helpers::Mutable

#changed_in_place?

Constructor Details

#initialize(subtype, delimiter = ',') ⇒ Array

Returns a new instance of Array.


11
12
13
14
15
16
17
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 11

def initialize(subtype, delimiter = ',')
  @subtype = subtype
  @delimiter = delimiter

  @pg_encoder = PG::TextEncoder::Array.new name: "#{type}[]", delimiter: delimiter
  @pg_decoder = PG::TextDecoder::Array.new name: "#{type}[]", delimiter: delimiter
end

Instance Attribute Details

#delimiterObject (readonly)

Returns the value of attribute delimiter


8
9
10
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 8

def delimiter
  @delimiter
end

#subtypeObject (readonly)

Returns the value of attribute subtype


8
9
10
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 8

def subtype
  @subtype
end

Instance Method Details

#==(other) ⇒ Object


46
47
48
49
50
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 46

def ==(other)
  other.is_a?(Array) &&
    subtype == other.subtype &&
    delimiter == other.delimiter
end

#cast(value) ⇒ Object


27
28
29
30
31
32
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 27

def cast(value)
  if value.is_a?(::String)
    value = @pg_decoder.decode(value)
  end
  type_cast_array(value, :cast)
end

#deserialize(value) ⇒ Object


19
20
21
22
23
24
25
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 19

def deserialize(value)
  if value.is_a?(::String)
    type_cast_array(@pg_decoder.decode(value), :deserialize)
  else
    super
  end
end

#map(value, &block) ⇒ Object


57
58
59
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 57

def map(value, &block)
  value.map(&block)
end

#serialize(value) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 34

def serialize(value)
  if value.is_a?(::Array)
    result = @pg_encoder.encode(type_cast_array(value, :serialize))
    if encoding = determine_encoding_of_strings(value)
      result.force_encoding(encoding)
    end
    result
  else
    super
  end
end

#type_cast_for_schema(value) ⇒ Object


52
53
54
55
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 52

def type_cast_for_schema(value)
  return super unless value.is_a?(::Array)
  "[" + value.map { |v| subtype.type_cast_for_schema(v) }.join(", ") + "]"
end