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

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

Overview

:nodoc:

Defined Under Namespace

Classes: Data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Array.


13
14
15
16
17
18
19
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 13

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


10
11
12
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 10

def delimiter
  @delimiter
end

#subtypeObject (readonly)

Returns the value of attribute subtype


10
11
12
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 10

def subtype
  @subtype
end

Instance Method Details

#==(other) ⇒ Object


48
49
50
51
52
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 48

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

#cast(value) ⇒ Object


32
33
34
35
36
37
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 32

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

#changed_in_place?(raw_old_value, new_value) ⇒ Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 63

def changed_in_place?(raw_old_value, new_value)
  deserialize(raw_old_value) != new_value
end

#deserialize(value) ⇒ Object


21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 21

def deserialize(value)
  case value
  when ::String
    type_cast_array(@pg_decoder.decode(value), :deserialize)
  when Data
    type_cast_array(value.values, :deserialize)
  else
    super
  end
end

#map(value, &block) ⇒ Object


59
60
61
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 59

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

#serialize(value) ⇒ Object


39
40
41
42
43
44
45
46
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 39

def serialize(value)
  if value.is_a?(::Array)
    casted_values = type_cast_array(value, :serialize)
    Data.new(@pg_encoder, casted_values)
  else
    super
  end
end

#type_cast_for_schema(value) ⇒ Object


54
55
56
57
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 54

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