Sha256: 95d8b83c41af013744617ba2b2a52ce1dd09d4b3ba7531b502c621fa43bd7b1f

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 KB

Contents

module ZendeskAPI
  # Shamelessly stolen and modified from https://github.com/archan937/dirty_hashy
  # @private
  module TrackChanges
    def self.included(base)
      base.method_defined?(:regular_writer).tap do |defined|
        base.send :include, InstanceMethods
        unless defined
          base.send :alias_method, :_store, :store
          base.send :alias_method, :store, :regular_writer
          base.send :alias_method, :[]=, :store
          base.send :define_method, :update do |other|
            other.each{|key, value| store key, value}
          end
          base.send :alias_method, :merge!, :update
        end
      end
    end

    # @private
    module InstanceMethods
      def clear_changes
        each do |k, v|
          v.clear_changes if v.respond_to?(:clear_changes)
        end

        changes.clear
      end

      def replace(other)
        clear
        merge! other
      end

      def clear
        keys.each{|key| delete key}
      end

      def [](key)
        super(key)
      end

      def regular_writer(key, value)
        changes[key] = value
        defined?(_store) ? _store(key, value) : super(key, value)
      end

      def delete(key)
        self[key] = nil
        super
      end

      def changes
        (@changes ||= self.class.superclass.new).tap do |changes|
          each do |k, v|
            if v.respond_to?(:changed?) && v.changed?
              changes[k] = v.changes
            end
          end
        end
      end

      def changed?(key = nil)
        if key.nil?
          !changes.empty? || any? {|_, v| v.respond_to?(:changed?) && v.changed?}
        else
          changes.key?(key)
        end
      end
      alias :dirty? :changed?
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
zendesk_api-0.3.0 lib/zendesk_api/track_changes.rb
zendesk_api-0.2.6 lib/zendesk_api/track_changes.rb
zendesk_api-0.2.5 lib/zendesk_api/track_changes.rb
zendesk_api-0.2.4 lib/zendesk_api/track_changes.rb
zendesk_api-0.2.2 lib/zendesk_api/track_changes.rb
zendesk_api-0.2.1 lib/zendesk_api/track_changes.rb
zendesk_api-0.1.11 lib/zendesk_api/track_changes.rb
zendesk_api-0.1.10 lib/zendesk_api/track_changes.rb
zendesk_api-0.1.9 lib/zendesk_api/track_changes.rb
zendesk_api-0.1.8 lib/zendesk_api/track_changes.rb