Sha256: 7ccb0dda100b85bb63d3724b841e7afe312e265645202f775b6383383e89d48d

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

module ActiveRecord
  module Type
    module Bigquery
      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

4 entries across 4 versions & 1 rubygems

Version Path
activerecord-bigquery-adapter-1.0.3 lib/active_record/type/bigquery/array.rb
activerecord-bigquery-adapter-1.0.2 lib/active_record/type/bigquery/array.rb
activerecord-bigquery-adapter-1.0.1 lib/active_record/type/bigquery/array.rb
activerecord-bigquery-adapter-1.0.0 lib/active_record/type/bigquery/array.rb