Sha256: e9144bc1d68f913b4351c5ce43347e90b79cfe79c0485db613cd8422a6ec01bf

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Sniffed
  module Sniffer
    extend ActiveSupport::Concern

    module ClassMethods
      def sniffed
        include Sniffed::Sniffer::SniffedInstanceMethods

        after_initialize :reset_commited_changes
        after_save :track_commited_changes
      end
    end

    module SniffedInstanceMethods
      def reset_commited_changes
        @unfiltered_commited_changes = {}
      end

      def commited_changes
        @unfiltered_commited_changes.reject(&unchanged_attributes)
      end

      private

      def track_commited_changes
        previous_changes.each_pair(&track_commited_change)
      end

      def track_commited_change
        lambda do |(attribute, (previous_value, current_value))|
          if @unfiltered_commited_changes.key?(attribute)
            @unfiltered_commited_changes[attribute][1] = current_value
          else
            @unfiltered_commited_changes[attribute] = [previous_value, current_value]
          end
        end
      end

      def unchanged_attributes
        lambda do |_k, (previous_value, current_value)|
          previous_value == current_value
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sniffed-0.1.1 lib/sniffed/sniffer.rb