Sha256: 0b84e1267d9e0309c3ed024641c424ea1eacf0145521f4d02b63b660908bbdce

Contents?: true

Size: 1.37 KB

Versions: 21

Compression:

Stored size: 1.37 KB

Contents

# @source: https://github.com/ahawkins/virtus-dirty_attribute
module Virtus
  module DirtyAttribute
    class Session
      attr_reader :subject

      def initialize(subject)
        @subject = subject
      end

      def original_attributes
        @_original_attributes ||= get_original_attributes(subject).dup
      end

      def dirty_attributes
        @_dirty_attributes ||= {}
      end

      def dirty!(name, value)
        dirty_attributes[name] = value
      end

      def attribute_clean!(name)
        dirty_attributes.delete name
        original_attributes.delete name
      end

      def dirty?(name = nil)
        name ? dirty_attributes.key?(name) : dirty_attributes.any?
      end

      def clean!
        original_attributes.clear
        dirty_attributes.clear
      end

      private

      # Get the original values from the instance variable directly and not
      # the possibly overridden accessor.
      #
      # This allows for accessors that are created to provide lazy loading
      # of external resources.
      #
      # Whenever something is loaded from the server is should be marked clean.
      def get_original_attributes(subject)
        subject.class.attribute_set.each_with_object({}) do |attribute, attributes|
          name = attribute.name
          attributes[name] = subject.instance_variable_get("@#{name}")
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
tracker_api-1.0.0 lib/virtus/dirty_attribute/session.rb