module DatastaxRails::Types::DirtyCollection

An extension to normal arrays and hashes that allow for tracking of dirty values. This is used by ActiveModel's change tracking framework.

Public Class Methods

ignore_modifications() { || ... } click to toggle source

FIXME: How do we make this thread-safe?

# File lib/datastax_rails/types/dirty_collection.rb, line 44
def self.ignore_modifications
  original = dsr_ignore_modifications
  self.dsr_ignore_modifications = true
  result = yield
  self.dsr_ignore_modifications = original
  result
end
new(record, name, collection) click to toggle source
Calls superclass method
# File lib/datastax_rails/types/dirty_collection.rb, line 29
def initialize(record, name, collection)
  @record   = record
  @name     = name.to_s

  super(collection)
  organize_collection
end

Public Instance Methods

delete(obj) click to toggle source
Calls superclass method
# File lib/datastax_rails/types/dirty_collection.rb, line 37
def delete(obj)
  modifying do
    super
  end
end

Private Instance Methods

modifying() { || ... } click to toggle source
# File lib/datastax_rails/types/dirty_collection.rb, line 54
def modifying
  # So there's a problem with overriding the map! method on Array.
  # When we do the update to record.attributes, HashWithIndifferentAccess
  # calls .map! on our Array.  This causes infinite recursion which
  # I find is generally not a desired behavior.  We use a variable
  # to tell if we've already hijacked the call.
  if dsr_ignore_modifications
    yield
  else
    DirtyCollection.ignore_modifications do
      original = dup unless record.changed_attributes.key?(name)

      result = yield

      organize_collection

      if !record.changed_attributes.key?(name) && original != self
        record.changed_attributes[name] = original
      end

      record.attributes[name] = self

      result
    end
  end
end
organize_collection() click to toggle source

A hook to allow implementing classes to muck with the collection before we check it for equality.

# File lib/datastax_rails/types/dirty_collection.rb, line 83
def organize_collection
  # No-op
end