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:

Defined Under Namespace

Classes: Data

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#immutable_value

Constructor Details

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

Returns a new instance of Array.



15
16
17
18
19
20
21
# File 'activerecord/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 'activerecord/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 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 12

def subtype
  @subtype
end

Instance Method Details

#==(other) ⇒ Object



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

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

#cast(value) ⇒ Object



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

def cast(value)
  if value.is_a?(::String)
    value = begin
      @pg_decoder.decode(value)
    rescue TypeError
      # malformed array string is treated as [], will raise in PG 2.0 gem
      # this keeps a consistent implementation
      []
    end
  end
  type_cast_array(value, :cast)
end

#changed_in_place?(raw_old_value, new_value) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 71

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 'activerecord/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

#force_equality?(value) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 75

def force_equality?(value)
  value.is_a?(::Array)
end

#map(value, &block) ⇒ Object



67
68
69
# File 'activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb', line 67

def map(value, &block)
  value.map { |v| subtype.map(v, &block) }
end

#serialize(value) ⇒ Object



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

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



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

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