Sha256: 2786c8619f5139ac5d1d5b821b4b8082df9989b2a7f34d34f989fbd5433b4733

Contents?: true

Size: 1.53 KB

Versions: 97

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Deimos
  module ActiveRecordConsume
    # Convert a message with a schema to an ActiveRecord model
    class SchemaModelConverter
      # Create new converter
      # @param decoder [SchemaBackends::Base] Incoming message schema.
      # @param klass [ActiveRecord::Base] Model to map to.
      def initialize(decoder, klass)
        @decoder = decoder
        @klass = klass
      end

      # Convert a message from a decoded hash to a set of ActiveRecord
      # attributes. Attributes that don't exist in the model will be ignored.
      # @param payload [Hash] Decoded message payload.
      # @return [Hash] Model attributes.
      def convert(payload)
        attributes = {}
        @decoder.schema_fields.each do |field|
          column = @klass.columns.find { |c| c.name == field.name }
          next if column.nil?
          next if %w(updated_at created_at).include?(field.name)

          attributes[field.name] = _coerce_field(column, payload[field.name])
        end
        attributes
      end

    private

      # @param column [ActiveRecord::ConnectionAdapters::Column]
      # @param val [Object]
      def _coerce_field(column, val)
        return nil if val.nil?

        if column.type == :datetime
          int_val = begin
                      val.is_a?(Integer) ? val : (val.is_a?(String) && Integer(val))
                    rescue StandardError
                      nil
                    end

          return Time.zone.at(int_val) if int_val
        end

        val
      end
    end
  end
end

Version data entries

97 entries across 97 versions & 2 rubygems

Version Path
deimos-ruby-1.24.2 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.24.1 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.24.0 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.23.3 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.23.2 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.23.1.pre.beta6 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.23.1.pre.beta5 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.23.1.pre.beta4 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.23.1.pre.beta3 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.23.1.pre.beta2 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.23.1.pre.beta1 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.23.0 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.22.5 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.22.4 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.22.3 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.22.2 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.22.1 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.22 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.20.1 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.20.0 lib/deimos/active_record_consume/schema_model_converter.rb