Class: Memento::State
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Memento::State
- Defined in:
- lib/memento/state.rb
Class Method Summary collapse
-
.store(action_type, record) ⇒ Object
before_create :set_record_data.
Instance Method Summary collapse
- #fetch? ⇒ Boolean
- #record_data ⇒ Object
- #record_data=(data) ⇒ Object
- #set_record_data ⇒ Object
- #undo ⇒ Object
Class Method Details
.store(action_type, record) ⇒ Object
before_create :set_record_data
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/memento/state.rb', line 17 def self.store(action_type, record) new do |state| state.action_type = action_type.to_s state.record = record if state.fetch? state.set_record_data state.save end end end |
Instance Method Details
#fetch? ⇒ Boolean
41 42 43 |
# File 'lib/memento/state.rb', line 41 def fetch? action.fetch? end |
#record_data ⇒ Object
32 33 34 |
# File 'lib/memento/state.rb', line 32 def record_data @record_data ||= read_attribute(:record_data) ? Memento.serializer.load(read_attribute(:record_data)) : {} end |
#record_data=(data) ⇒ Object
36 37 38 39 |
# File 'lib/memento/state.rb', line 36 def record_data=(data) @record_data = nil write_attribute(:record_data, data.is_a?(String) ? data : Memento.serializer.dump(data)) end |
#set_record_data ⇒ Object
45 46 47 |
# File 'lib/memento/state.rb', line 45 def set_record_data self.record_data = action.fetch end |
#undo ⇒ Object
28 29 30 |
# File 'lib/memento/state.rb', line 28 def undo Memento::Result.new(action.undo, self) end |