Class | Snapshot |
In: |
lib/facets/more/snapshot.rb
|
Parent: | Object |
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.
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
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.