Class: Memento::State

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/memento/state.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/memento/state.rb', line 41

def fetch?
  action.fetch?
end

#record_dataObject



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_dataObject



45
46
47
# File 'lib/memento/state.rb', line 45

def set_record_data
  self.record_data = action.fetch
end

#undoObject



28
29
30
# File 'lib/memento/state.rb', line 28

def undo
  Memento::Result.new(action.undo, self)
end