Sha256: 0ba8be19c49aecb284fc98ac87c0fb95fee53dde6bbf6303e01df44d1886d8aa

Contents?: true

Size: 990 Bytes

Versions: 6

Compression:

Stored size: 990 Bytes

Contents

require 'set'

module Intercom
  module Traits
    module DirtyTracking

      def reset_changed_fields!
        @changed_fields = Set.new
      end

      def mark_fields_as_changed!(field_names)
        @changed_fields ||= Set.new
        field_names.each do |attr|
          @changed_fields.add(attr.to_s)
        end
      end

      def mark_field_as_changed!(field_name)
        @changed_fields ||= Set.new
        @changed_fields.add(field_name.to_s)
      end

      def field_changed?(field_name)
        @changed_fields ||= Set.new
        field = instance_variable_get("@#{field_name}")
        if field.respond_to?(:field_changed?)
          field.to_hash.any? do |attribute, _|
            field.field_changed?(attribute)
          end
        else
          @changed_fields.include?(field_name.to_s)
        end
      end

      def instance_variables_excluding_dirty_tracking_field
        instance_variables.reject{|var| var == :@changed_fields}
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
intercom-4.2.1 lib/intercom/traits/dirty_tracking.rb
intercom-4.2.0 lib/intercom/traits/dirty_tracking.rb
intercom-4.1.3 lib/intercom/traits/dirty_tracking.rb
intercom-4.1.2 lib/intercom/traits/dirty_tracking.rb
intercom-4.1.1 lib/intercom/traits/dirty_tracking.rb
intercom-4.1.0 lib/intercom/traits/dirty_tracking.rb