Sha256: e32cc33e063dc4d5ccd237331c631444d27fed14d95693c6dd6f937bd73ffd19

Contents?: true

Size: 1.25 KB

Versions: 61

Compression:

Stored size: 1.25 KB

Contents

module SparkApi
  module Models
    module Dirty

      def changed?
        changed.any?
      end

      def changed
        changed_attributes.keys
      end

      def changes
        Hash[changed.map { |attr| [attr, attribute_change(attr)] }]
      end

      def previous_changes
        @previously_changed
      end

      # hash with changed attributes and their original values
      def changed_attributes
        @changed_attributes ||= {}
      end

      # hash with changed attributes and their new values
      def dirty_attributes
        changed.inject({}) { |h, k| h[k] = attributes[k.to_s]; h }
      end

      private

      def reset_dirty
        @previously_changed = changed_attributes
        @changed_attributes.clear
      end

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

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

      def attribute_will_change!(attr)
        attr = attr.to_s
        begin
          value = @attributes[attr]
          value = value.duplicable? ? value.clone : value
        rescue TypeError, NoMethodError; end

        changed_attributes[attr] = value unless changed.include?(attr)
      end

    end
  end
end

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
spark_api-1.6.3 lib/spark_api/models/dirty.rb
spark_api-1.6.2 lib/spark_api/models/dirty.rb
spark_api-1.6.1 lib/spark_api/models/dirty.rb
spark_api-1.5.7 lib/spark_api/models/dirty.rb
spark_api-1.6.0 lib/spark_api/models/dirty.rb
spark_api-1.5.6 lib/spark_api/models/dirty.rb
spark_api-1.5.5 lib/spark_api/models/dirty.rb
spark_api-1.5.4 lib/spark_api/models/dirty.rb
spark_api-1.5.3 lib/spark_api/models/dirty.rb
spark_api-1.5.2 lib/spark_api/models/dirty.rb
spark_api-1.5.1 lib/spark_api/models/dirty.rb
spark_api-1.5.0 lib/spark_api/models/dirty.rb
spark_api-1.4.34 lib/spark_api/models/dirty.rb
spark_api-1.4.32 lib/spark_api/models/dirty.rb
spark_api-1.4.31 lib/spark_api/models/dirty.rb
spark_api-1.4.29 lib/spark_api/models/dirty.rb
spark_api-1.4.28 lib/spark_api/models/dirty.rb
spark_api-1.4.27 lib/spark_api/models/dirty.rb
spark_api-1.4.26 lib/spark_api/models/dirty.rb
spark_api-1.4.25 lib/spark_api/models/dirty.rb