| 1 | package Sash::ResultHistory; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Carp; | ||
| 6 | |||
| 7 | # The Highlander Pattern | ||
| 8 | |||
| 9 | my @_result_history; | ||
| 10 | my @_redo_stack; | ||
| 11 | |||
| 12 | sub add { | ||
| 13 | my $class = shift; | ||
| 14 | my $result = shift; | ||
| 15 | |||
| 16 | croak "result not of type Sash::Table" unless ref $result eq 'Sash::Table'; | ||
| 17 | |||
| 18 | unshift @_result_history, $result; | ||
| 19 | |||
| 20 | return; | ||
| 21 | } | ||
| 22 | |||
| 23 | # For many of the methods below notice we return the current Sash::Table | ||
| 24 | # object as a convenience to the caller. It is good form in that it | ||
| 25 | # indicates the requested operation was successful and also allows the | ||
| 26 | # caller to chain commands together which they might want to do. | ||
| 27 | |||
| 28 | sub undo { | ||
| 29 | my $class = shift; | ||
| 30 | |||
| 31 | unshift @_redo_stack, shift @_result_history; | ||
| 32 | |||
| 33 | return $class->current; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub current { | ||
| 37 | my $class = shift; | ||
| 38 | |||
| 39 | return $_result_history[0]; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub redo { | ||
| 43 | my $class = shift; | ||
| 44 | |||
| 45 | unshift @_result_history, shift @_redo_stack; | ||
| 46 | |||
| 47 | return $class->current; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub revert { | ||
| 51 | my $class = shift; | ||
| 52 | |||
| 53 | @_result_history = $_result_history[ scalar( @_result_history ) - 1 ]; | ||
| 54 | |||
| 55 | return $class->current; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub all { | ||
| 59 | my $class = shift; | ||
| 60 | |||
| 61 | return @_result_history; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub size { | ||
| 65 | my $class = shift; | ||
| 66 | |||
| 67 | return scalar @_result_history; | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; |