Sha256: 2786c8619f5139ac5d1d5b821b4b8082df9989b2a7f34d34f989fbd5433b4733

Contents?: true

Size: 1.53 KB

Versions: 99

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

99 entries across 99 versions & 2 rubygems

Version Path
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
deimos-ruby-1.19.7 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.6 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.5 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.4 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.3 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.2 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.1 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.1.pre.beta1 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.0 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.beta2 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.19.beta1 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.18.2 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.18.1 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.18.0 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.17.1 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.17.0 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.16.5 lib/deimos/active_record_consume/schema_model_converter.rb
deimos-ruby-1.16.4 lib/deimos/active_record_consume/schema_model_converter.rb