Class: Wee::State

Inherits:
Object show all
Defined in:
lib/wee/state.rb

Overview

This class is for backtracking the state of components (or decorations/presenters). Components that want an undo-facility to be implemented (triggered for example by a browsers back-button), have to overwrite the Component#state method. Class Wee::State simply represents a collection of objects from which snapshots were taken via methods take_snapshot.

Defined Under Namespace

Classes: Snapshot

Instance Method Summary collapse

Constructor Details

#initializeState

Returns a new instance of State.



36
37
38
# File 'lib/wee/state.rb', line 36

def initialize
  @snapshots = Hash.new
end

Instance Method Details

#add(object) ⇒ Object Also known as: <<



40
41
42
# File 'lib/wee/state.rb', line 40

def add(object)
  (@snapshots[object.object_id] ||= Snapshot.new(object)).take
end

#add_ivar(object, ivar, value = object.instance_variable_get(ivar)) ⇒ Object



44
45
46
# File 'lib/wee/state.rb', line 44

def add_ivar(object, ivar, value=object.instance_variable_get(ivar))
  (@snapshots[object.object_id] ||= Snapshot.new(object)).add_ivar(ivar, value)
end

#restoreObject



50
51
52
# File 'lib/wee/state.rb', line 50

def restore
  @snapshots.each_value {|snapshot| snapshot.restore}
end