Snapshot

A lightweight single-depth object state capture. The take_snapshot method reads the object‘s state, which is generally it‘s collection of instance variables, and returns them in a hash. The state can be restored with apply_snapshot.

Usage

  Customer = Struct.new("Customer", :name, :address, :zip)
  joe = Customer.new( "Joe Pitare", "1115 Lila Ln.", 47634 )

  # simple transactions
  joe_snap = joe.take_snapshot
  begin
    do_something_with( joe )
  rescue
    joe.apply_snapshot( joe_snap )
  end

  joe_snap[:name]     => "Joe Pitare"
  joe_snap[:address]  => "1115 Lila Ln."
  joe_snap[:zip]      => 47634

Details

Class Snapshot simply represents a collection of objects from which snapshots were taken via their methods take_snapshot. It provides methods to add an object to a snapshot (Snapshot#add) as well as to restore all objects of the snapshot to their state stored in the snapshot (method Snapshot#restore).

In Wee, this class is used to 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 Wee::Component#backtrack_state method.

Todo

  • Perhaps extend to offer multiple depths.
  • Should key consitancy be enforced? Currently Struct‘s will have symbol keys while other classes will have string keys in the form of "@name".
  • Add other core classes.
  • Convert to Kernel#to_data ?
  • I‘ve been thinking about renaming this to State.

Authors

  • Michael Neumann

Copying

Copyright (c) 2004 Michael Neumann

Ruby License

This module is free software. You may use, modify, and/or redistribute this software under the same terms as Ruby.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.