Module: Multiplay::WasChanged

Defined in:
lib/multiplay/was_changed.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
# File 'lib/multiplay/was_changed.rb', line 4

def self.included(base)
  base.send(:before_save, :store_changes)
  base.attribute_method_suffix '_was_changed?', '_before_save_was', '_before_save_change'
end

Instance Method Details

#attribute_before_save_change(attr) ⇒ Object



33
34
35
# File 'lib/multiplay/was_changed.rb', line 33

def attribute_before_save_change attr
  [@was_changed_attributes[attr], __send__(attr)] if attribute_was_changed?(attr)
end

#attribute_before_save_was(attr) ⇒ Object



29
30
31
# File 'lib/multiplay/was_changed.rb', line 29

def attribute_before_save_was attr
  attribute_was_changed?(attr) ? @was_changed_attributes[attr] : __send__(attr)
end

#attribute_was_changed?(attr) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/multiplay/was_changed.rb', line 25

def attribute_was_changed? attr
  @was_changed_attributes.keys.include? attr
end

#saved_changesObject



21
22
23
# File 'lib/multiplay/was_changed.rb', line 21

def saved_changes
  was_saved.inject({}) { |h, attr| h[attr] = attribute_before_save_change(attr); h }
end

#store_changesObject



9
10
11
# File 'lib/multiplay/was_changed.rb', line 9

def store_changes
  @was_changed_attributes = @changed_attributes.clone
end

#was_changed?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/multiplay/was_changed.rb', line 13

def was_changed?
  !@was_changed_attributes.empty?
end

#was_savedObject



17
18
19
# File 'lib/multiplay/was_changed.rb', line 17

def was_saved
  @was_changed_attributes.keys
end