diff options
author | dakkar <dakkar@thenautilus.net> | 2010-12-02 20:49:22 +0000 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2010-12-02 20:49:22 +0000 |
commit | b66368ad64ceb4d30c79d079895199cf51301af4 (patch) | |
tree | ed810f1af1e58711bfebf8450bc6cd1a262a180d /lib/GridFiller/Status.pm | |
parent | scaler, and __DATA__ (diff) | |
download | qr-builder-b66368ad64ceb4d30c79d079895199cf51301af4.tar.gz qr-builder-b66368ad64ceb4d30c79d079895199cf51301af4.tar.bz2 qr-builder-b66368ad64ceb4d30c79d079895199cf51301af4.zip |
mutable objects, all status in attributes
Diffstat (limited to 'lib/GridFiller/Status.pm')
-rw-r--r-- | lib/GridFiller/Status.pm | 63 |
1 files changed, 39 insertions, 24 deletions
diff --git a/lib/GridFiller/Status.pm b/lib/GridFiller/Status.pm index b14f870..092f9a9 100644 --- a/lib/GridFiller/Status.pm +++ b/lib/GridFiller/Status.pm @@ -3,13 +3,32 @@ use Moose; use namespace::autoclean; use List::Util qw(shuffle); use List::MoreUtils qw(uniq); -use GridFiller::Types qw(WordListT GridStatusT); +use GridFiller::Types qw(WordListT GridStatusT GridT WordListT); +use MooseX::Types::Moose qw(Str); use GridFiller::Constants ':all'; use Carp; use feature 'switch'; with 'MooseX::Log::Log4perl'; +has words => ( + isa => WordListT, + required => 1, + is => 'ro', +); + +has grid => ( + isa => GridT, + required => 1, + is => 'ro', +); + +has mode => ( + isa => Str, + is => 'rw', + default => 'given', +); + has words_to_use => ( isa => WordListT, traits => ['Array'], @@ -17,32 +36,21 @@ has words_to_use => ( has_next_word => 'count', get_next_word => 'shift', }, - is => 'rw', - required => 1, + lazy_build => 1, + clearer => '_reset_wts', ); has grid_status => ( isa => GridStatusT, - is => 'rw', - required => 1, + is => 'ro', + lazy_build => 1, + clearer => '_reset_gs', ); -around BUILDARGS => sub { - my ($orig, $class, $args, @rest) = @_; - - if (exists $args->{words} && exists $args->{grid}) { - $args->{words_to_use} = _munge_words_to_use(delete $args->{words},$args); - $args->{grid_status} = _munge_grid_status(delete $args->{grid}); - } - - return $class->$orig($args,@rest); -}; - -sub _munge_words_to_use { - my ($words,$args) = @_; - # clone initial word list - - my $mode = delete $args->{mode} || 'random'; +sub _build_words_to_use { + my ($self) = @_; + my $words = $self->words; + my $mode = $self->mode; given ($mode) { when ('random') { return [ shuffle uniq @$words ] } @@ -53,8 +61,8 @@ sub _munge_words_to_use { } } -sub _munge_grid_status { - my ($grid) = @_; +sub _build_grid_status { + my ($self) = @_; return [ map { [ @@ -62,7 +70,7 @@ sub _munge_grid_status { $_ eq '*' ? $BLACK : $WHITE } @$_ ] - } @$grid + } @{$self->grid} ]; } @@ -129,4 +137,11 @@ sub to_string { } } +sub reset { + my ($self) = @_; + + $self->_reset_gs; + $self->_reset_wts; +} + 1; |