Class: ActiveRecord::Type::Spanner::Array

Inherits:
Type::Value
  • Object
show all
Defined in:
lib/active_record/type/spanner/array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element_type) ⇒ Array

This constructor intentionally does not call super. rubocop:disable Lint/MissingSuper



17
18
19
# File 'lib/active_record/type/spanner/array.rb', line 17

def initialize element_type
  @element_type = element_type
end

Instance Attribute Details

#element_typeObject (readonly)

Returns the value of attribute element_type.



11
12
13
# File 'lib/active_record/type/spanner/array.rb', line 11

def element_type
  @element_type
end

Instance Method Details

#cast(value) ⇒ Object

rubocop:enable Lint/MissingSuper



22
23
24
25
26
27
28
29
# File 'lib/active_record/type/spanner/array.rb', line 22

def cast value
  return super if value.nil?
  return super unless value.respond_to? :map

  value.map do |v|
    @element_type.cast v
  end
end

#serialize(value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_record/type/spanner/array.rb', line 31

def serialize value
  return super if value.nil?
  return super unless value.respond_to? :map

  if @element_type.is_a? ActiveRecord::Type::Decimal
    # Convert a decimal (NUMERIC) array to a String array to prevent it from being encoded as a FLOAT64 array.
    value.map do |v|
      next if v.nil?
      v.to_s
    end
  else
    value.map do |v|
      @element_type.serialize v
    end
  end
end