X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FStorage.pm;h=4dfc86d61dab62399d4535fff8a6f365e27d30fc;hb=8af2c2b01fe7c63f2748d1b1deca3198c8974b7f;hp=05b243327596fee2b66cc3cdb52a55a54aed8442;hpb=25697231931e82932eb5e4fbd302c9faa82c066a;p=gitmo%2FMooseX-Storage.git diff --git a/lib/MooseX/Storage.pm b/lib/MooseX/Storage.pm index 05b2433..4dfc86d 100644 --- a/lib/MooseX/Storage.pm +++ b/lib/MooseX/Storage.pm @@ -1,13 +1,9 @@ - package MooseX::Storage; use Moose qw(confess); use MooseX::Storage::Meta::Attribute::DoNotSerialize; use String::RewritePrefix (); -our $VERSION = '0.21'; -our $AUTHORITY = 'cpan:STEVAN'; - sub import { my $pkg = caller(); @@ -21,16 +17,28 @@ sub import { my %HORRIBLE_GC_AVOIDANCE_HACK; -sub __expand_role { - my ($base, $value) = @_; +sub _rewrite_role_name { + my ($self, $base, $string) = @_; + + my $role_name = scalar String::RewritePrefix->rewrite( + { + '' => "MooseX::Storage::$base\::", + '=' => '', + }, + $string, + ); +} + +sub _expand_role { + my ($self, $base, $value) = @_; return unless defined $value; if (ref $value) { - my ($class, $param, $no) = @$value; - confess "too many args in arrayref role declaration" if defined $no; + confess "too many args in arrayref role declaration" if @$value > 2; + my ($class, $param) = @$value; - $class = __expand_role($base => $class); + $class = $self->_rewrite_role_name($base => $class); Class::MOP::load_class($class); my $role = $class->meta->generate_role(parameters => $param); @@ -38,16 +46,20 @@ sub __expand_role { $HORRIBLE_GC_AVOIDANCE_HACK{ $role->name } = $role; return $role->name; } else { - my $role = scalar String::RewritePrefix->rewrite( - { - '' => "MooseX::Storage::$base\::", - '=' => '', - }, - $value, - ); - - Class::MOP::load_class($role); - return $role; + my $class = $self->_rewrite_role_name($base, $value); + Class::MOP::load_class($class); + + my $role = $class; + + if ($class->meta->isa( + 'MooseX::Role::Parameterized::Meta::Role::Parameterizable' + )) { + $role = $class->meta->generate_role(parameters => undef); + $HORRIBLE_GC_AVOIDANCE_HACK{ $role->name } = $role; + return $role->name; + } + + return $class; } } @@ -56,13 +68,13 @@ sub _injected_storage_role_generator { $params{base} = '=MooseX::Storage::Basic' unless defined $params{base}; - my @roles = __expand_role(Base => $params{base}); + my @roles = __PACKAGE__->_expand_role(Base => $params{base}); # NOTE: # you don't have to have a format # role, this just means you dont # get anything other than pack/unpack - push @roles, __expand_role(Format => $params{format}); + push @roles, __PACKAGE__->_expand_role(Format => $params{format}); # NOTE: # many IO roles don't make sense unless @@ -75,13 +87,13 @@ sub _injected_storage_role_generator { # us. This allows the StorableFile to work #(exists $params{'format'}) # || confess "You must specify a format role in order to use an IO role"; - push @roles, __expand_role(IO => $params{io}); + push @roles, __PACKAGE__->_expand_role(IO => $params{io}); # Note: # These traits alter the behaviour of the engine, the user can # specify these per role-usage for my $trait ( @{ $params{'traits'} ||= [] } ) { - push @roles, __expand_role(Traits => $trait); + push @roles, __PACKAGE__->_expand_role(Traits => $trait); } return @roles; @@ -103,8 +115,6 @@ MooseX::Storage - A serialization framework for Moose classes use Moose; use MooseX::Storage; - our $VERSION = '0.01'; - with Storage('format' => 'JSON', 'io' => 'File'); has 'x' => (is => 'rw', isa => 'Int'); @@ -168,10 +178,10 @@ The first (base) level is C and C. In this level the class is serialized into a Perl HASH reference, it is tagged with the class name and each instance attribute is stored. Very simple. -This level is not optional, it is the bare minumum that +This level is not optional, it is the bare minimum that MooseX::Storage provides and all other levels build on top of this. -See L for the fundamental implementation and +See L for the fundamental implementation and options to C and C =item B @@ -184,13 +194,15 @@ specific serialization format and Perl land. This level is optional, if you don't want/need it, you don't have to have it. You can just use C/C instead. +=for stopwords io + =item B The third (io) level is C and C. In this level we are reading and writing data to file/network/database/etc. This level is also optional, in most cases it does require a C role -to also be used, the expection being the C role. +to also be used, the exception being the C role. =back @@ -208,7 +220,7 @@ The following traits are currently bundled with C: =item OnlyWhenBuilt -Only attributes that have been built (ie, where the predicate returns +Only attributes that have been built (i.e., where the predicate returns 'true') will be serialized. This avoids any potentially expensive computations. See L for details. @@ -228,9 +240,11 @@ MooseX::Storage enabled objects are supported. With Array and Hash references the first level down is inspected and any objects found are serialized/deserialized for you. We do not do -this recusively by default, however this feature may become an +this recursively by default, however this feature may become an option eventually. +=for stopwords subtypes + The specific serialize/deserialize routine is determined by the Moose type constraint a specific attribute has. In most cases subtypes of the supported types are handled correctly, and there is a facility @@ -256,7 +270,7 @@ over anyway. =head1 CAVEAT -This is B a persistence framework, changes to your object after +This is B a persistence framework; changes to your object after you load or store it will not be reflected in the stored class. =head1 EXPORTS @@ -265,7 +279,7 @@ you load or store it will not be reflected in the stored class. =item B -This module will export the C method will can be used to +This module will export the C method and can be used to load a specific set of MooseX::Storage roles to implement a specific combination of features. It is meant to make things easier, but it is by no means the only way. You can still compose your roles by @@ -280,6 +294,8 @@ that is not under the default namespace prefix, start with an equal sign: Storage(format => '=My::Private::JSONFormat'); +=for stopwords parameterized + To use a parameterized role (for which, see L) you can pass an arrayref of the role name (in short or long form, as above) and its parameters: @@ -304,6 +320,8 @@ parameters: =back +=for stopwords TODO + =head1 TODO This module needs docs and probably a Cookbook of some kind as well. @@ -337,3 +355,5 @@ This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut + +