Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Moose from ArrayRef[HashRef] to ArrayRef[Object]

by saintex (Scribe)
on Feb 23, 2011 at 16:10 UTC ( [id://889818]=note: print w/replies, xml ) Need Help??


in reply to Re: Moose from ArrayRef[HashRef] to ArrayRef[Object]
in thread Moose from ArrayRef[HashRef] to ArrayRef[Object]

yes...
but... how?

I have checked the samples on Moose manual and I am a little confused.
  • Comment on Re^2: Moose from ArrayRef[HashRef] to ArrayRef[Object]

Replies are listed 'Best First'.
Re^3: Moose from ArrayRef[HashRef] to ArrayRef[Object]
by ikegami (Patriarch) on Feb 23, 2011 at 18:44 UTC
    Using BUILDARGS to avoid effects at a distance:
    { package Page; use strict; use warnings; use Moose; has 'url' => ( is => 'ro', isa => 'Str', required => 1, ); no Moose; __PACKAGE__->meta->make_immutable(); } { package Container; use strict; use warnings; use Moose; my $pages_constraint = Moose::Util::TypeConstraints::create_paramet +erized_type_constraint('ArrayRef[Page]'); my $aoh_constraint = Moose::Util::TypeConstraints::create_paramet +erized_type_constraint('ArrayRef[HashRef]'); around BUILDARGS => sub { my $orig = shift; my $class = shift; my %args = ( @_ == 1 ? %{ $_[0] } : @_ ); if (exists($args{pages})) { my $pages = $args{pages}; if (!$pages_constraint->check($pages)) { # Need to coerce? if ($aoh_constraint->check($pages)) { # Can we coerce? $args{pages} = [ map Page->new($_), @$pages ]; } } } return $class->$orig(%args); }; has 'pages' => ( is => 'rw', isa => 'ArrayRef[Page]', default => sub { [ ] }, ); no Moose; __PACKAGE__->meta->make_immutable(); } Container->new( pages => [ { url => "a" }, { url => "b" } ] );

    This is basically the same approach you tried, but correctly uses the more appropriate BUILDARGS instead of BUILD.

    Coded so it can take both ArrayRef[HashRef] and ArrayRef[Page].

      thank you very much for your solutions:
      tomorrow I will try both of them.
      I tried the two samples.
      The second one is the one I like it (because conceptually you don't have to create a type not related to class in the base-class).

      Just a little precisation:
      Here:
      $args{pages} = [ map Page->new($_), @$pages ];

      must be:
      $args{pages} = [ map Page->new(URL=>$$_{Page}{URL})), @$pages ];


      And I have a question:

      I have also checked Moose manual:

      What "around" is?

      around BUILDARGS => sub {

      Thank you for your answers.
Re^3: Moose from ArrayRef[HashRef] to ArrayRef[Object]
by ikegami (Patriarch) on Feb 23, 2011 at 18:23 UTC
    { package Page; use strict; use warnings; use Moose; has 'url' => ( is => 'ro', isa => 'Str', required => 1, ); use Moose::Util::TypeConstraints; subtype 'Pages' => as 'ArrayRef[Page]'; coerce 'Pages' => from 'ArrayRef[HashRef]' => via { [ map __PACKAGE__->new($_), @{ $_[0] } ] }; no Moose::Util::TypeConstraints; no Moose; __PACKAGE__->meta->make_immutable(); } { package Container; use strict; use warnings; use Moose; has 'pages' => ( is => 'rw', isa => 'Pages', coerce => 1, default => sub { [ ] }, ); no Moose; __PACKAGE__->meta->make_immutable(); } Container->new( pages => [ { url => "a" }, { url => "b" } ] );

    I don't really like the global-ness of the coercion. I'll be posting an alternative that uses BUILDARGS shortly.

Re^3: Moose from ArrayRef[HashRef] to ArrayRef[Object]
by saintex (Scribe) on Feb 23, 2011 at 17:06 UTC
    This is an horrible solution, because of duplicate attributes:

    YAML file:
    tmpPages: - Page: URL: http://www.perlmonks.org/ - Page: URL: http://www.perl.org/
    Objects container:
    package PagesGenerator; our $VERSION=0.01; use Moose; with 'MooseX::SimpleConfig'; use Page; has 'tmpPages'=>( is=>'rw', isa=>'ArrayRef[HashRef]', default => sub { [ ] }, ); has 'pages'=>( is=>'rw', isa=>'ArrayRef[Page]', default => sub { [ ] }, ); sub BUILD{ my $self=shift; @{$self->pages}= map {new Page(URL=>$$_{Page}{URL});} @{$self->tmp +Pages}; for ( @{ $self->pages } ) { print $_->URL; print "\n"; } }


    Any other idea?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://889818]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found