From: Matt S Trout Date: Thu, 5 Mar 2009 11:01:28 +0000 (-0500) Subject: switch snippets across to object based X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2Fcatbook-code.git;a=commitdiff_plain;h=28d1f68e20c5bc5dd1581c3d80e6cd617bac59c8 switch snippets across to object based --- diff --git a/lib/LolCatalyst/Lite/Snippet.pm b/lib/LolCatalyst/Lite/Snippet.pm new file mode 100644 index 0000000..ed5fd15 --- /dev/null +++ b/lib/LolCatalyst/Lite/Snippet.pm @@ -0,0 +1,17 @@ +package LolCatalyst::Lite::Snippet; + +use Moose; +use namespace::clean -except => 'meta'; + +has 'id' => (is => 'ro', required => 1); +has 'text' => (is => 'ro', required => 1); +has '_translator' => (is => 'ro', required => 1, init_arg => 'translator'); + +sub translate { + my ($self) = @_; + $self->_translator->translate($self->text); +} + +__PACKAGE__->meta->make_immutable; + +1; diff --git a/lib/LolCatalyst/Lite/SnippetStore.pm b/lib/LolCatalyst/Lite/SnippetStore.pm index b5a1735..facc6c6 100644 --- a/lib/LolCatalyst/Lite/SnippetStore.pm +++ b/lib/LolCatalyst/Lite/SnippetStore.pm @@ -1,9 +1,19 @@ package LolCatalyst::Lite::SnippetStore; use Moose; +use aliased 'LolCatalyst::Lite::Snippet'; use namespace::clean -except => 'meta'; has '_snippets' => (is => 'ro', default => sub { [] }); +has '_translator' => ( + is => 'ro', + required => 1, + lazy => 1, + default => sub { + confess "_translator object requested but never supplied" + }, + init_arg => 'translator' +); sub find { my ($self, $id) = @_; @@ -17,9 +27,13 @@ sub all { sub create { my ($self, $new) = @_; - $new->{id} = @{$self->_snippets} + 1; - push(@{$self->_snippets}, $new); - $new; + my $snippet = Snippet->new( + %$new, + id => (@{$self->_snippets} + 1), + translator => $self->_translator + ); + push(@{$self->_snippets}, $snippet); + $snippet; } __PACKAGE__->meta->make_immutable; diff --git a/t/snippet_store/basic.t b/t/snippet_store/basic.t index 5a46a3f..ada3887 100644 --- a/t/snippet_store/basic.t +++ b/t/snippet_store/basic.t @@ -1,11 +1,18 @@ use strict; use warnings; use Test::More qw(no_plan); +use Test::Exception; use_ok "LolCatalyst::Lite::SnippetStore"; my $store = LolCatalyst::Lite::SnippetStore->new; +dies_ok { + $store->create({ text => "earth shattering kaboom" }); +} 'Create without translator object fails'; + +$store = LolCatalyst::Lite::SnippetStore->new(translator => 'DUMMY'); + my $num_snips = 3; ok(