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, SnapshotIVars

Instance Method Summary collapse

Constructor Details

#initializeState

Returns a new instance of State.



15
16
17
18
# File 'lib/wee/state.rb', line 15

def initialize
  @objects = Hash.new
  @objects_ivars = Hash.new 
end

Instance Method Details

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



20
21
22
# File 'lib/wee/state.rb', line 20

def add(object)
  @objects[object.object_id] ||= Snapshot.new(object, object.take_snapshot)
end

#add_ivar(object, ivar, value) ⇒ Object



24
25
26
# File 'lib/wee/state.rb', line 24

def add_ivar(object, ivar, value)
  (@objects_ivars[object.object_id] ||= SnapshotIVars.new(object, {})).ivars[ivar] = value
end

#restoreObject



30
31
32
33
34
35
# File 'lib/wee/state.rb', line 30

def restore
  @objects.each_value {|s| s.object.restore_snapshot(s.snapshot) }
  @objects_ivars.each_value {|s|
    s.ivars.each {|k,v| s.object.instance_variable_set(k, v) }
  }
end