Guest User

Untitled

a guest
Jul 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package MooseX::Storage::Format::Moosey;
  2.  
  3. use Moose::Role;
  4.  
  5. use MooseX::Traits::Util 'new_class_with_traits';
  6. use DateTime::Format::ISO8601;
  7.  
  8. MooseX::Storage::Engine->add_custom_type_handler(
  9. 'DateTime' => (
  10. expand => sub { DateTime::Format::ISO8601->parse_datetime(shift) },
  11. collapse => sub { (shift)->iso8601 }
  12. )
  13. );
  14.  
  15. MooseX::Storage::Engine->add_custom_type_handler(
  16. 'MongoDB::OID' => (
  17. expand => sub { MongoDB::OID->new( value => shift ); },
  18. collapse => sub { shift->to_string }
  19. )
  20. );
  21.  
  22. no warnings 'once';
  23. use utf8 ();
  24.  
  25. our $VERSION = '0.01';
  26.  
  27. sub thaw {
  28. my ( $class, $doc, %args ) = @_;
  29.  
  30. my $obj = undef;
  31. if ( $doc->{traits} and $doc->{_trait_namespace} ) {
  32. delete $doc->{_trait_namespace};
  33. my @traits = ref $doc->{traits} ? @{$doc->{traits}} : ($doc->{traits});
  34. # Ignore traits we don't want... should use is_anon or something
  35. @traits = grep { !/\|/; } @traits;
  36.  
  37. my $meta = new_class_with_traits( $class, @traits );
  38. my $e = $class->_storage_get_engine_class(%args)->new(class => $meta->name);
  39. $obj = MooseX::Storage::Basic::_storage_construct_instance(
  40. $meta->name,
  41. $e->expand_object($doc, %args),
  42. \%args
  43. );
  44. } else {
  45. $obj = $class->unpack( $doc, %args );
  46. }
  47.  
  48. $obj->_id( $doc->{_id} ) if defined $obj;
  49. return $obj;
  50. }
  51.  
  52. sub freeze {
  53. my ( $self, @args ) = @_;
  54.  
  55. my $raw = $self->pack( @args );
  56. # Now, we look at the traits on the class. If it does MooseX::Traits we
  57. # look at the anon class and pull out any role that doesn't make sense, and
  58. # add it to __TRAITS__
  59. if ( $self->meta->does_role('MooseX::Traits') and ( ref $self ) =~ /^MooseX::Traits/ ) {
  60. # XX Support multiple classes?
  61. ( $raw->{__CLASS__} ) = $self->meta->superclasses;
  62. $raw->{traits} = [
  63. map { '+' . $_->name }
  64. grep { !$_->is_anon_role }
  65. $self->meta->calculate_all_roles
  66. ];
  67. }
  68.  
  69. return $raw;
  70. }
  71.  
  72. no Moose::Role;
  73. 1;
Add Comment
Please, Sign In to add comment