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

#initialize(*args) ⇒ Object



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

def initialize(*args)
  super
  changed_keys.clear if args.first.blank? || !new?
end

#reload(*args) ⇒ Object



60
61
62
63
64
# File 'lib/mongo_mapper/plugins/dirty.rb', line 60

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

#save(*args) ⇒ Object



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

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

#save!(*args) ⇒ Object



54
55
56
57
58
# File 'lib/mongo_mapper/plugins/dirty.rb', line 54

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