Module: DirtyDan

Defined in:
lib/dirtydan.rb

Overview

This module provides methods for automation of object dirtyness tracking.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object

:nodoc:



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dirtydan.rb', line 3

def DirtyDan.included(mod) #:nodoc:
  class << mod
    instance_eval do
      define_method( :attr_writer ) do |*syms|
        syms.each do |sym|
          class_eval <<-THECODE
            def #{sym}= (val)
              mark_dirty() if @#{sym} != val
              @#{sym} = val
            end
          THECODE
        end
      end
    end
    instance_eval do
      define_method( :attr_accessor ) do |*syms|
        attr_writer *syms
        attr_reader *syms
      end
    end
  end
end

Instance Method Details

#clean_dirtyObject

Clear the dirty mark



37
38
39
# File 'lib/dirtydan.rb', line 37

def clean_dirty
  @dirty = false
end

#hide_dirtyObject

Hide the dirty tracking variable for dumps, etc. Has the side effect of clearing the mark.



42
43
44
# File 'lib/dirtydan.rb', line 42

def hide_dirty
  remove_instance_variable(:@dirty)
end

#is_dirty?Boolean

Discover if an object is marked dirty

Returns:

  • (Boolean)


32
33
34
# File 'lib/dirtydan.rb', line 32

def is_dirty?
  !!@dirty
end

#mark_dirtyObject

Mark an object as dirty



27
28
29
# File 'lib/dirtydan.rb', line 27

def mark_dirty
  @dirty = true
end