Sha256: 0f5b1f14ab83527b1bb706b47e197f585c6c76a12f93aa0ce7b140f06ccbb2b8

Contents?: true

Size: 1.2 KB

Versions: 17

Compression:

Stored size: 1.2 KB

Contents

# Copyright 2021 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

module ActiveRecord
  module Type
    module Spanner
      class Array < Type::Value
        attr_reader :element_type
        delegate :type, :user_input_in_time_zone, :limit, :precision, :scale, to: :element_type

        def initialize element_type
          @element_type = element_type
        end

        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

        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
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
activerecord-spanner-adapter-1.6.3 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.6.2 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.6.1 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.6.0 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.5.1 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.5.0 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.4.4 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.4.3 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.4.2 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.4.1 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.4.0 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.3.1 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.2.2 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.2.1 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.2.0 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.1.0 lib/active_record/type/spanner/array.rb
activerecord-spanner-adapter-1.0.1 lib/active_record/type/spanner/array.rb