Sha256: 8f22f404e6aba4b77e1e485db858d8505a810d89dfd7aa7b58dc0b817fe515cd

Contents?: true

Size: 1.7 KB

Versions: 20

Compression:

Stored size: 1.7 KB

Contents

module JsonApiClient
  module Helpers
    module Dirty

      def changed?
        changed_attributes.present?
      end

      def changed
        changed_attributes.keys
      end

      def changed_attributes
        @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
      end

      def clear_changes_information
        @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
      end

      def set_all_attributes_dirty
        attributes.each do |k, v|
          set_attribute_was(k, v)
        end
      end

      def attribute_will_change!(attr)
        return if attribute_changed?(attr)
        set_attribute_was(attr, attributes[attr])
      end

      def set_attribute_was(attr, value)
        begin
          value = value.duplicable? ? value.clone : value
          changed_attributes[attr] = value
        rescue TypeError, NoMethodError
        end
      end

      def attribute_was(attr) # :nodoc:
        attribute_changed?(attr) ? changed_attributes[attr] : attributes[attr]
      end

      def attribute_changed?(attr)
        changed.include?(attr.to_s)
      end

      def attribute_change(attr)
        [changed_attributes[attr], attributes[attr]] if attribute_changed?(attr)
      end

      protected

      def method_missing(method, *args, &block)
        if method.to_s =~ /^(.*)_changed\?$/
          has_attribute?($1) ? attribute_changed?($1) : nil
        elsif method.to_s =~ /^(.*)_was$/
          has_attribute?($1) ? attribute_was($1) : nil
        else
          super
        end
      end

      def set_attribute(name, value)
        attribute_will_change!(name) if value != attributes[name] || !attributes.has_key?(name)
        super
      end

    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
json_api_client-1.8.0 lib/json_api_client/helpers/dirty.rb
json_api_client-1.7.0 lib/json_api_client/helpers/dirty.rb
json_api_client-1.6.4 lib/json_api_client/helpers/dirty.rb
json_api_client-1.6.3 lib/json_api_client/helpers/dirty.rb
json_api_client-1.6.2 lib/json_api_client/helpers/dirty.rb
json_api_client-1.6.1 lib/json_api_client/helpers/dirty.rb
json_api_client-1.6.0 lib/json_api_client/helpers/dirty.rb
json_api_client-1.5.3 lib/json_api_client/helpers/dirty.rb
json_api_client-1.5.2 lib/json_api_client/helpers/dirty.rb
json_api_client-1.5.1 lib/json_api_client/helpers/dirty.rb
json_api_client-1.5.0 lib/json_api_client/helpers/dirty.rb
json_api_client-1.4.0 lib/json_api_client/helpers/dirty.rb
json_api_client-1.3.0 lib/json_api_client/helpers/dirty.rb
json_api_client-1.2.0 lib/json_api_client/helpers/dirty.rb
json_api_client-1.1.1 lib/json_api_client/helpers/dirty.rb
json_api_client-1.1.0 lib/json_api_client/helpers/dirty.rb
json_api_client-1.0.2 lib/json_api_client/helpers/dirty.rb
json_api_client-1.0.1 lib/json_api_client/helpers/dirty.rb
json_api_client-1.0.0 lib/json_api_client/helpers/dirty.rb
json_api_client-1.0.0.beta7 lib/json_api_client/helpers/dirty.rb