Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MooseX::Storage::Format::Moosey;
- use Moose::Role;
- use MooseX::Traits::Util 'new_class_with_traits';
- use DateTime::Format::ISO8601;
- MooseX::Storage::Engine->add_custom_type_handler(
- 'DateTime' => (
- expand => sub { DateTime::Format::ISO8601->parse_datetime(shift) },
- collapse => sub { (shift)->iso8601 }
- )
- );
- MooseX::Storage::Engine->add_custom_type_handler(
- 'MongoDB::OID' => (
- expand => sub { MongoDB::OID->new( value => shift ); },
- collapse => sub { shift->to_string }
- )
- );
- no warnings 'once';
- use utf8 ();
- our $VERSION = '0.01';
- sub thaw {
- my ( $class, $doc, %args ) = @_;
- my $obj = undef;
- if ( $doc->{traits} and $doc->{_trait_namespace} ) {
- delete $doc->{_trait_namespace};
- my @traits = ref $doc->{traits} ? @{$doc->{traits}} : ($doc->{traits});
- # Ignore traits we don't want... should use is_anon or something
- @traits = grep { !/\|/; } @traits;
- my $meta = new_class_with_traits( $class, @traits );
- my $e = $class->_storage_get_engine_class(%args)->new(class => $meta->name);
- $obj = MooseX::Storage::Basic::_storage_construct_instance(
- $meta->name,
- $e->expand_object($doc, %args),
- \%args
- );
- } else {
- $obj = $class->unpack( $doc, %args );
- }
- $obj->_id( $doc->{_id} ) if defined $obj;
- return $obj;
- }
- sub freeze {
- my ( $self, @args ) = @_;
- my $raw = $self->pack( @args );
- # Now, we look at the traits on the class. If it does MooseX::Traits we
- # look at the anon class and pull out any role that doesn't make sense, and
- # add it to __TRAITS__
- if ( $self->meta->does_role('MooseX::Traits') and ( ref $self ) =~ /^MooseX::Traits/ ) {
- # XX Support multiple classes?
- ( $raw->{__CLASS__} ) = $self->meta->superclasses;
- $raw->{traits} = [
- map { '+' . $_->name }
- grep { !$_->is_anon_role }
- $self->meta->calculate_all_roles
- ];
- }
- return $raw;
- }
- no Moose::Role;
- 1;
Add Comment
Please, Sign In to add comment