Sha256: 0269ecda679b8048ee47ed09642e7015c7558a5904facc4a7899456591c3761e

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Remont
  class RecordProcessor
    NOOP = proc {}

    # @param [Remont::Schema] schema
    # @param [Proc] before
    # @param [Proc] after
    def initialize(schema, before, after)
      @schema = schema
      @before = before || NOOP
      @after = after || NOOP
    end

    # @param [ActiveRecord::Base] record
    # @return [ActiveRecord::Base] record
    def process!(record)
      before.call(record)
      record.update_columns( # rubocop:disable Rails/SkipsModelValidations
        processed_attributes(record)
      )
      after.call(record)

      record
    end

    private

    attr_reader :schema
    attr_reader :before
    attr_reader :after

    def processed_attributes(record)
      schema
        .attributes
        .select { |attribute| record.send(attribute.name).present? }
        .reduce(default_processed_attributes) do |memo, attribute|
          memo.merge(attribute.name => attribute.process(record.send(attribute.name), record))
        end
    end

    def default_processed_attributes
      return {} unless schema.process_timestamp_attribute

      {
        schema.process_timestamp_attribute => Time.now.getlocal
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
remont-0.1.2 lib/remont/record_processor.rb
remont-0.1.1 lib/remont/record_processor.rb