Module: Undoable

Defined in
lib/kyanite/general/undoable.rb

Undoable Module

Save and restore objects. Great for try-and-error-algorithms.

Kyanite definitions

Undoable

Kyanite tests and examples
Usage

require 'kyanite/general/undoable'

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constant Summary

@@undoable_history =
Hash.new

Class Method Details

+ (Object) clear

Discards all saved states of all objects.



39
40
41
# File 'lib/kyanite/general/undoable.rb', line 39

def self.clear
  @@undoable_history.clear
end

Instance Method Details

- (Object) load_and_delete

Loads a saved state of an object, the saved state will be discarded.



32
33
34
# File 'lib/kyanite/general/undoable.rb', line 32

def load_and_delete
  @@undoable_history.delete(self.object_id)
end

- (Object) load_and_keep

Loads a saved state of an object, the saved state remains in memory.



25
26
27
# File 'lib/kyanite/general/undoable.rb', line 25

def load_and_keep
  @@undoable_history[self.object_id]
end

- (Object) save(method = :dup)

Saves an object.

Parameters:

  • method (Symbol) (defaults to: :dup)

    method to use. Default is :dup. For more complex objects use :deep_copy instead.



17
18
19
20
# File 'lib/kyanite/general/undoable.rb', line 17

def save(method=:dup)
  return if self == @@undoable_history[self.object_id] # nichts zu tun
  @@undoable_history[self.object_id] = self.send(method)
end