MooseX-Blessed-Reconstruct-0.04/000755 000765 000024 00000000000 11416726341 020232 5ustar00nothingmuchstaff000000 000000 MooseX-Blessed-Reconstruct-0.04/Changes000644 000765 000024 00000000412 11416726307 021524 0ustar00nothingmuchstaff000000 000000 0.04 - Update BUILDALL behavior for recent versions of Moose (doy) 0.03 - Class::MOP no longer returns the metaclass from load_class 0.02 - Remove Class::Accessor from test suite (not having it as a dependency caused failures) 0.01 - Initial release MooseX-Blessed-Reconstruct-0.04/lib/000755 000765 000024 00000000000 11416726341 021000 5ustar00nothingmuchstaff000000 000000 MooseX-Blessed-Reconstruct-0.04/Makefile.PL000644 000765 000024 00000000610 11416726231 022177 0ustar00nothingmuchstaff000000 000000 #!/usr/bin/perl -w use strict; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'MooseX::Blessed::Reconstruct', VERSION_FROM => 'lib/MooseX/Blessed/Reconstruct.pm', INSTALLDIRS => 'site', SIGN => 1, PL_FILES => { }, PREREQ_PM => { 'Test::use::ok' => 0, 'namespace::clean' => 0, 'Moose' => 1.05, 'Class::MOP' => 0.93, 'Data::Visitor' => 0.21, }, ); MooseX-Blessed-Reconstruct-0.04/MANIFEST000644 000765 000024 00000000416 11416726341 021364 0ustar00nothingmuchstaff000000 000000 Changes lib/MooseX/Blessed/Reconstruct.pm Makefile.PL MANIFEST This list of files MANIFEST.SKIP t/basic.t META.yml Module meta-data (added by MakeMaker) SIGNATURE Public-key signature (added by MakeMaker) MooseX-Blessed-Reconstruct-0.04/MANIFEST.SKIP000644 000765 000024 00000001125 11131764000 022113 0ustar00nothingmuchstaff000000 000000 # Avoid version control files. \bRCS\b \bCVS\b \bSCCS\b ,v$ \B\.svn\b \B\.git\b \b_darcs\b # Avoid Makemaker generated and utility files. \bMANIFEST\.bak \bMakefile$ \bblib/ \bMakeMaker-\d \bpm_to_blib\.ts$ \bpm_to_blib$ \bblibdirs\.ts$ # 6.18 through 6.25 generated this # Avoid Module::Build generated and utility files. \bBuild$ \b_build/ # Avoid temp and backup files. ~$ \.old$ \#$ \b\.# \.bak$ # Avoid Devel::Cover files. \bcover_db\b ### DEFAULT MANIFEST.SKIP ENDS HERE #### \.DS_Store$ \.sw.$ (\w+-)*(\w+)-\d\.\d+(?:\.tar\.gz)?$ \.t\.log$ \.prove$ # XS shit \.(?:bs|c|o)$ MooseX-Blessed-Reconstruct-0.04/META.yml000644 000765 000024 00000001146 11416726341 021505 0ustar00nothingmuchstaff000000 000000 --- #YAML:1.0 name: MooseX-Blessed-Reconstruct version: 0.04 abstract: ~ author: [] license: unknown distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: Class::MOP: 0.93 Data::Visitor: 0.21 Moose: 1.05 namespace::clean: 0 Test::use::ok: 0 no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 MooseX-Blessed-Reconstruct-0.04/SIGNATURE000644 000765 000024 00000002203 11416726341 021513 0ustar00nothingmuchstaff000000 000000 This file contains message digests of all files listed in MANIFEST, signed via the Module::Signature module, version 0.62. To verify the content in this distribution, first make sure you have Module::Signature installed, then type: % cpansign -v It will check each file's integrity, as well as the signature's validity. If "==> Signature verified OK! <==" is not displayed, the distribution may already have been compromised, and you should not run its Makefile.PL or Build.PL. -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 SHA1 8e442a83c925d9d54f6e2da526c4a127bd73b877 Changes SHA1 78a468e5be41b2f15d15e6a989c94fba17c9a093 MANIFEST SHA1 190e9058eb9c6446a1a3f3ddf15b082f1ecde152 MANIFEST.SKIP SHA1 e7a4399700f4f8481e357bee58acc36dfd1ed263 META.yml SHA1 8bfb42276336bae728712b029a18bc2d82914f84 Makefile.PL SHA1 de7e757558aa28a86f64829ae945eaf5091c6866 lib/MooseX/Blessed/Reconstruct.pm SHA1 a864e611614c3e25f55dd7174dc6f1f8922881a9 t/basic.t -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.12 (Darwin) iEYEARECAAYFAkw7rOEACgkQVCwRwOvSdBh4XwCfRasi0vD7xrRV/bQQLGZ4pDpd GBcAn1lHe20qX1zvUB96UD1uXy12ERI0 =irHr -----END PGP SIGNATURE----- MooseX-Blessed-Reconstruct-0.04/t/000755 000765 000024 00000000000 11416726341 020475 5ustar00nothingmuchstaff000000 000000 MooseX-Blessed-Reconstruct-0.04/t/basic.t000644 000765 000024 00000004230 11254414654 021743 0ustar00nothingmuchstaff000000 000000 #!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; use Scalar::Util qw(refaddr); use ok 'MooseX::Blessed::Reconstruct'; { # convince Perl these classes are loaded { package Some::Foo; sub foo {}; package Some::Bar; sub bar {} } my $v = MooseX::Blessed::Reconstruct->new; isa_ok( $v, "MooseX::Blessed::Reconstruct" ); my $obj = bless({ oh => "hai", bar => bless({ name => "Hello", }, "Some::Bar"), }, "Some::Foo"); $obj->{bar2} = $obj->{bar}; my $fixed = $v->visit($obj); isa_ok( $fixed, "Some::Foo" ); isa_ok( $fixed->{bar}, "Some::Bar" ); isa_ok( $fixed->{bar2}, "Some::Bar" ); is( refaddr($fixed->{bar}), refaddr($fixed->{bar2}), "refaddr for shared ref" ); is( $fixed->{oh}, "hai", "simple value" ); } { { package A::Nonmoose::Class; # not to be confused with an anonymous class! sub new { bless $_[1], $_[0] } sub name { $_[0]{name} } package A::Moose::Class; use Moose; has string => ( is => 'ro', isa => 'Str', required => 1 ); has _ => ( is => 'ro', isa => 'Str', required => 1, init_arg => 'foo' ); has ignore => ( is => 'ro', isa => 'Str', default => 'ok', init_arg => undef ); has other => ( is => 'ro', isa => 'A::Nonmoose::Class' ); has moose => ( is => 'ro', isa => 'Str', default => 'hi' ); has extra => ( is => 'rw' ); sub BUILD { shift->extra("yatta") } } my $obj = bless( { string => 'test', foo => 'bar', other => bless({ name => "Yuval"}, "A::Nonmoose::Class"), }, 'A::Moose::Class'); isnt $obj->{moose}, 'hi', 'default not yet created'; isnt $obj->{extra}, 'extra', 'BUILD not called'; my $fixed = MooseX::Blessed::Reconstruct->new->visit($obj); isnt( refaddr($fixed), refaddr($obj), "new object" ); isa_ok $fixed, 'A::Moose::Class'; isa_ok $fixed->other, 'A::Nonmoose::Class'; is $fixed->string, 'test', 'simple data'; is $fixed->other->name, 'Yuval', 'nested object data'; is $fixed->_, 'bar', 'init_arg value works'; is $fixed->moose, 'hi', '"default" worked'; is $fixed->ignore, 'ok', 'no init-arg'; is $fixed->extra, 'yatta', 'BUILD called'; isnt $obj->{moose}, 'hi', 'proto was not destroyed'; isnt $obj->{extra}, 'extra', 'proto was not destroyed'; } MooseX-Blessed-Reconstruct-0.04/lib/MooseX/000755 000765 000024 00000000000 11416726341 022212 5ustar00nothingmuchstaff000000 000000 MooseX-Blessed-Reconstruct-0.04/lib/MooseX/Blessed/000755 000765 000024 00000000000 11416726341 023573 5ustar00nothingmuchstaff000000 000000 MooseX-Blessed-Reconstruct-0.04/lib/MooseX/Blessed/Reconstruct.pm000644 000765 000024 00000006257 11416726250 026455 0ustar00nothingmuchstaff000000 000000 #!/usr/bin/perl package MooseX::Blessed::Reconstruct; use Moose; use Carp qw(croak); use Class::MOP 0.66; # well behaved load_class() use Data::Visitor 0.21; # n-arity visit use Scalar::Util qw(reftype); use namespace::clean -except => 'meta'; our $VERSION = "0.04"; extends qw(Data::Visitor); has load_classes => ( isa => "Bool", is => "rw", default => 1, ); sub visit_object { my ( $v, $obj ) = @_; my $class = ref $obj; Class::MOP::load_class($class) if $v->load_classes; my $meta = Class::MOP::get_metaclass_by_name($class); if ( ref $meta ) { return $v->visit_object_with_meta($obj, $meta); } else { return $v->visit_ref($obj); } } sub visit_object_with_meta { my ( $v, $obj, $meta ) = @_; my $instance = $meta->get_meta_instance->create_instance; $v->_register_mapping( $obj => $instance ); my $args = $v->prepare_args( $meta, $obj ); $meta->new_object( %$args, __INSTANCE__ => $instance ); return $instance; } sub prepare_args { my ( $v, $meta, $obj ) = @_; my @args; if ( reftype $obj eq 'HASH' ) { @args = %$obj; } elsif ( reftype $obj eq 'ARRAY' ) { @args = @$obj; } elsif ( reftype $obj eq 'SCALAR' ) { @args = $$obj; } else { croak "unknown ref type $obj"; } my @processed = $v->visit(@args); return $meta->name->BUILDARGS(@processed); } __PACKAGE__->meta->make_immutable; __PACKAGE__ __END__ =pod =head1 NAME MooseX::Blessed::Reconstruct - A L for creating Moose objects from blessed placeholders =head1 SYNOPSIS use MooseX::Blessed::Reconstruct; my $obj = bless( { init_arg_foo => "Blah", arf => "yay", }, "Foo" ); my $proper = MooseX::Blessed::Reconstruct->new->visit($obj); # equivalent to: my $proper = Foo->meta->new_object(%$obj); # but recursive (and works with shared references) =head1 DESCRIPTION The purpose of this module is to "fix up" blessed data into a real Moose object. This is used internally by L but has no implementation details having to do with L itself. =head1 METHODS See L =over 4 =item visit_object $object Calls L on the C of $object. If there's a metaclass, calls C, otherwise C is used to walk the object brutishly. Returns a deep clone of the input structure with all the L objects reconstructed "properly". =item visit_object_with_meta $obj, $meta Uses the metaclass C<$meta> to create a new instance, registers the instance with L's cycle tracking, and then inflates it using L. =item prepare_args $obj Collapses $obj into key value pairs to be used as init args to L. =back =head1 VERSION CONTROL This module is maintained using Darcs. You can get the latest version from L, and use C to commit changes. =head1 AUTHOR Jonathan Rockway Yuval Kogman Enothingmuch@woobling.orgE =head1 COPYRIGHT Copyright (c) 2008 Infinity Interactive, Yuval Kogman. All rights reserved This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut