Sha256: 8dcb2551c5405adbfe9730c8ba24cf49e77dc414c9ba063b7529d8488ac19a28

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 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(&testing)
        previous_changes.each_pair(&track_commited_change)
      end

      def testing
      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.0 lib/sniffed/sniffer.rb