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.



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

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.



12
13
14
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 12

def delimiter
  @delimiter
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



12
13
14
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 12

def subtype
  @subtype
end

Instance Method Details

#==(other) ⇒ Object



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

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

#cast(value) ⇒ Object



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

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)


65
66
67
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 65

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

#deserialize(value) ⇒ Object



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

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



61
62
63
# File 'lib/active_record/connection_adapters/postgresql/oid/array.rb', line 61

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

#serialize(value) ⇒ Object



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

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



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

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