Module: MongoMapper::Plugins::Dirty::InstanceMethods

Defined in:
lib/mongo_mapper/plugins/dirty.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mongo_mapper/plugins/dirty.rb', line 6

def method_missing(method, *args, &block)
  if method.to_s =~ /(_changed\?|_change|_will_change!|_was)$/
    method_suffix = $1
    key = method.to_s.gsub(method_suffix, '')

    if key_names.include?(key)
      case method_suffix
        when '_changed?'
          key_changed?(key)
        when '_change'
          key_change(key)
        when '_will_change!'
          key_will_change!(key)
        when '_was'
          key_was(key)
      end
    else
      super
    end
  else
    super
  end
end

Instance Method Details

#changedObject



34
35
36
# File 'lib/mongo_mapper/plugins/dirty.rb', line 34

def changed
  changed_keys.keys
end

#changed?Boolean

Returns:



30
31
32
# File 'lib/mongo_mapper/plugins/dirty.rb', line 30

def changed?
  !changed_keys.empty?
end

#changesObject



38
39
40
# File 'lib/mongo_mapper/plugins/dirty.rb', line 38

def changes
  changed.inject({}) { |h, key| h[key] = key_change(key); h }
end

#initializeObject



42
43
44
45
# File 'lib/mongo_mapper/plugins/dirty.rb', line 42

def initialize(*)
  # never register initial id assignment as a change
  super.tap { changed_keys.delete('_id') }
end

#initialize_from_databaseObject



47
48
49
# File 'lib/mongo_mapper/plugins/dirty.rb', line 47

def initialize_from_database(*)
  super.tap { changed_keys.clear }
end

#reloadObject



64
65
66
67
68
# File 'lib/mongo_mapper/plugins/dirty.rb', line 64

def reload(*)
  document = super
  changed_keys.clear
  document
end

#saveObject



51
52
53
54
55
56
# File 'lib/mongo_mapper/plugins/dirty.rb', line 51

def save(*)
  if status = super
    changed_keys.clear
  end
  status
end

#save!Object



58
59
60
61
62
# File 'lib/mongo_mapper/plugins/dirty.rb', line 58

def save!(*)
  status = super
  changed_keys.clear
  status
end