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



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

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



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

def changed
  changed_keys.keys
end

#changed?Boolean

Returns:



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

def changed?
  !changed_keys.empty?
end

#changesObject



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

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

#initialize(*args) ⇒ Object



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

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

#reload(*args) ⇒ Object



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

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

#save(*args) ⇒ Object



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

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

#save!(*args) ⇒ Object



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

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