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

Returns a new instance of Array.



14
15
16
# File 'lib/active_record/type/spanner/array.rb', line 14

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



18
19
20
21
22
23
24
25
# File 'lib/active_record/type/spanner/array.rb', line 18

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_record/type/spanner/array.rb', line 27

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