Array-To-Moose

 view release on metacpan or  search on metacpan

Build.PL  view on Meta::CPAN

    module_name         => 'Array::To::Moose',
    license             => 'perl',
    dist_author         => 'Sam Brain <samb@stanford.edu>',
    dist_version_from   => 'lib/Array/To/Moose.pm',
    requires => {
        'Array::GroupBy'            => 0,
        'Moose'                     => 0,
        'MooseX::StrictConstructor' => 0,
        'Params::Validate::Array'   => 0,
        'Carp'                      => 0,
        'namespace::autoclean'      => 0,
        'Test::More'                => 0,
        'Data::Dumper'              => 0,
        'version'                   => 0,
    },

    configure_requires => {
        'Module::Build' => 0.38
    },

    add_to_cleanup      => [ 'Array-To-Moose-*' ],

META.json  view on Meta::CPAN

      },
      "runtime" : {
         "requires" : {
            "Array::GroupBy" : "0",
            "Carp" : "0",
            "Data::Dumper" : "0",
            "Moose" : "0",
            "MooseX::StrictConstructor" : "0",
            "Params::Validate::Array" : "0",
            "Test::More" : "0",
            "namespace::autoclean" : "0",
            "version" : "0"
         }
      }
   },
   "provides" : {
      "Array::To::Moose" : {
         "file" : "lib/Array/To/Moose.pm",
         "version" : "v0.0.9"
      }
   },

META.yml  view on Meta::CPAN

    file: lib/Array/To/Moose.pm
    version: v0.0.9
requires:
  Array::GroupBy: 0
  Carp: 0
  Data::Dumper: 0
  Moose: 0
  MooseX::StrictConstructor: 0
  Params::Validate::Array: 0
  Test::More: 0
  namespace::autoclean: 0
  version: 0
resources:
  license: http://dev.perl.org/licenses/
version: v0.0.9

README  view on Meta::CPAN

	./Build test
	./Build install


DEPENDENCIES

  Moose
  MooseX::StrictConstructor
  Params::Validate
  Carp
  namespace::autoclean
  Test::More
  Data::Dumper

TODO
  (1) Smarten up type contstraint handling a la Moose::Util::TypeConstraints
      Currently only handles 'Str', 'Int', etc.

COPYRIGHT AND LICENCE

Copyright (C) 2012, Sam Brain <samb@stanford.edu>

t/1.t  view on Meta::CPAN


my $n1 = [ 1, 1, 1 ];
my $n2 = [ 2, 2, 2 ];
my $n3 = [ 3, 3, 3 ];
my $n4 = [ 4, 4, 4 ];

my $data = [ $n1, $n2, $n3, $n4 ];

#----------------------------------------
package Person;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has [ qw( last first gender ) ] => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;

#----------------------------------------
package Person_w_Aliases;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

extends 'Person';

has 'aliases' => (is => 'ro', isa => 'ArrayRef[Str]');

__PACKAGE__->meta->make_immutable;

#----------------------------------------
package Person_w_Visit;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

extends 'Person';

has 'Visits' => (is => 'ro', isa => 'ArrayRef[Visit]');

__PACKAGE__->meta->make_immutable;

#----------------------------------------
package Person_w_Aliases_n_Visit;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

extends 'Person_w_Visit';

has 'aliases' => (is => 'ro', isa => 'ArrayRef[Str]');

__PACKAGE__->meta->make_immutable;

package main;

t/1.t  view on Meta::CPAN

         } qr/Class 'ePrson' not defined/,
           '_check_descriptor() Class undefined';

throws_ok {
  _check_descriptor($data, { class => 'Person', name => 0, gender => 2 });
         } qr/Attribute 'name' not in 'Person' object/,
           '_check_descriptor() wrong attribute name';

# construct two classes with attrib 'class' and 'CLASS'
package Obj_Attr_class;
use namespace::autoclean;
use Moose;

has [ qw( name class ) ] => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;

package Obj_Attr_CLASS;
use namespace::autoclean;
use Moose;

has [ qw( name CLASS ) ] => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;

package main;

throws_ok {
  _check_descriptor($data,{ class => 'Obj_Attr_class', name => 0 })

t/1.t  view on Meta::CPAN

throws_ok {
  _check_descriptor($data,{ CLASS => 'Obj_Attr_CLASS', name => 0 })
          } qr/The 'Obj_Attr_CLASS' object has an attribute called 'CLASS'/,
            "_check_descriptor() object with attribute called 'CLASS'";

# reset the class indicator
set_class_ind();

# construct two classes with attribs 'key' and 'KEY'
package Obj_Attr_key;
use namespace::autoclean;
use Moose;

has [ qw( name key ) ] => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;

package Obj_Attr_KEY;
use namespace::autoclean;
use Moose;

has [ qw( name KEY ) ] => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;

package main;

throws_ok {
  _check_descriptor($data,

t/10.t  view on Meta::CPAN


BEGIN {
  eval "use Test::Exception";
  plan skip_all => "Test::Exception needed" if $@;
}

plan tests => 11;

#----------------------------------------
package Sibling;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has [ qw( name sex ) ] => (is => 'rw', isa => 'Str' );

__PACKAGE__->meta->make_immutable;

#----------------------------------------
package Cousin;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has [ qw( name sex ) ] => (is => 'rw', isa => 'Str' );

__PACKAGE__->meta->make_immutable;

#----------------------------------------
package Person;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has [ qw( name sex ) ] => (is => 'rw', isa =>          'Str' );
has SibH               => (is => 'rw', isa => 'HashRef[Sibling]' );
has SibA               => (is => 'rw', isa => 'ArrayRef[Sibling]');
has SibB               => (is => 'rw', isa =>           'Sibling'); # (B)are

__PACKAGE__->meta->make_immutable;

t/11.t  view on Meta::CPAN


BEGIN {
  eval "use Test::Exception";
  plan skip_all => "Test::Exception needed" if $@;
}

plan tests => 1;

#----------------------------------------
package Person;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has 'name'    => (is => 'rw', isa =>        'Str' );
has 'sibling' => (is => 'rw', isa => 'Maybe[Str]' );

__PACKAGE__->meta->make_immutable;

package main;

t/2.t  view on Meta::CPAN


BEGIN {
  eval "use Test::Exception";
  plan skip_all => "Test::Exception needed" if $@;
}

plan tests => 12;

#----------------------------------------
package Person;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has [ qw( name gender ) ] => (is => 'rw', isa =>          'Str' );
has hobbies               => (is => 'rw', isa => 'ArrayRef[Str]');

__PACKAGE__->meta->make_immutable;

package main;

t/3.t  view on Meta::CPAN


eval 'use VarianReportsMoose qw(print_obj)';

use Data::Dumper;

use Array::To::Moose qw (:ALL :TESTING);

package Patient;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has [ qw(last first) ] => (is => 'ro', isa => 'Str');

__PACKAGE__->meta->make_immutable;

package main;

sub Npat { Patient->new(last => $_[0], first => $_[1] ) }

# patients

t/3a.t  view on Meta::CPAN


use Array::To::Moose qw (:ALL :TESTING);

# change keywords "class" to "_CLASS_" and "key" to "_KEY_"
set_class_ind('_CLASS_');
set_key_ind('_KEY_');

package Patient;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has [ qw(last first) ] => (is => 'ro', isa => 'Str');

__PACKAGE__->meta->make_immutable;

package main;

sub Npat { Patient->new(last => $_[0], first => $_[1] ) }

# patients

t/4d.t  view on Meta::CPAN

use Array::To::Moose qw (:ALL);

eval 'use VarianReportsMoose qw(print_obj)';

use Data::Dumper;

use Carp;

package Person;
use Moose;
use namespace::autoclean;
use MooseX::StrictConstructor;
has last      => (is => 'ro', isa => 'Str');
has first     => (is => 'ro', isa => 'Str');
has hobbies   => (is => 'ro', isa => 'ArrayRef[Str]');

__PACKAGE__->meta->make_immutable;

package main;

sub Npat { Person->new(last => $_[0], first => $_[1], hobbies => $_[2] ) }

t/4d.t  view on Meta::CPAN

                          hobbies => 2,     # Oops! should be '[2]'
                        }
                          );
           }
  qr/'Person'.*Did you forget/,
  "Moose object with 'ArrayRef[Str]' attribute but no '[]' in desc";


# has attribute 'no_type' which has - No Type!
package Person_no_type;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

extends 'Person';

has 'no_type' => ( is => 'rw' );  # no isa => ...

__PACKAGE__->meta->make_immutable;

package main;

t/4d.t  view on Meta::CPAN

                        }
                          );
           }
  qr/'Person_no_type' ref attrib 'no_type' has no type constraint/,
  "Moose ref attribute has no type";


# has attribute 'non_simple' which is an arrayref of type
# non-defined class 'Blah'
package Person_non_simple_att;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

extends 'Person';

has 'non_simple' => ( is => 'rw', isa => 'ArrayRef[Blah]' );

__PACKAGE__->meta->make_immutable;

package main;

t/4d.t  view on Meta::CPAN

                          non_simple => [2],
                        }
                          );
           }
  qr/'Person_non_simple_att' .* not a simple type/,
  "Moose simple attribute has non-simple type";


# make attribute 'non_simple' an arrayref of a defined class 'Person_no_type'
package Person_non_simple_att_1;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

extends 'Person';

has 'non_simple' => ( is => 'rw', isa => 'ArrayRef[Person_no_type]' );

__PACKAGE__->meta->make_immutable;

package main;

t/4d.t  view on Meta::CPAN

                          first      => 1,
                          non_simple => [2],
                        }
                          );
           }
  qr/'Person_non_simple_att_1' .* not a simple type/,
  "Moose simple attribute has non-simple (but defined) type";

# make attribute 'non_arrayref' a HashRef
package Person_w_hashref;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

extends 'Person';

has 'non_simple' => ( is => 'rw', isa => 'HashRef[Blah]' );

__PACKAGE__->meta->make_immutable;

package main;

t/5.t  view on Meta::CPAN

eval 'use VarianReportsMoose qw(print_obj)';

use Data::Dumper;

use Carp;

#------------
package Test;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has 'name'        => (is => 'rw', isa => 'Str');
has 'result'      => (is => 'rw', isa => 'Str');

#------------
package Visit;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has 'date'        => (is => 'rw', isa => 'Str'           );
has 'md'          => (is => 'rw', isa => 'Str'           );
has 'diagnosis'   => (is => 'rw', isa => 'Str'           );
has 'Tests'       => (is => 'rw', isa => 'HashRef[Test]' );

#------------
package Patient;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has 'last'        => (is => 'rw', isa => 'Str'             );
has 'first'       => (is => 'rw', isa => 'Str'             );
has 'Visits'      => (is => 'rw', isa => 'ArrayRef[Visit]' );

Test   ->meta->make_immutable;
Visit  ->meta->make_immutable;
Patient->meta->make_immutable;

package main;

t/5a.t  view on Meta::CPAN

eval 'use VarianReportsMoose qw(print_obj)';

use Data::Dumper;

use Carp;

#------------
package Test;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has 'name'        => (is => 'rw', isa => 'Str');
has 'result'      => (is => 'rw', isa => 'Num');

#------------
package Visit;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has 'date'     => (is => 'rw', isa => 'Str'            );
has 'md'       => (is => 'rw', isa => 'Str'            );
has 'Tests'    => (is => 'rw', isa => 'ArrayRef[Test]' );

#------------
package Patient;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has 'last'        => (is => 'rw', isa => 'Str'            );
has 'first'       => (is => 'rw', isa => 'Str'            );
has 'Visits'      => (is => 'rw', isa => 'ArrayRef[Visit]' );

Test   ->meta->make_immutable;
Visit  ->meta->make_immutable;
Patient->meta->make_immutable;

package main;

t/7.t  view on Meta::CPAN

  eval "use Test::Exception";
  plan skip_all => "Test::Exception needed" if $@;
}

plan tests => 11;

#----------------------------------------
package Patient;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has [ qw(last first gender) ] => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;
package main;

my $data = [ [1, 2, 3, 4] ];

throws_ok { array_to_moose ( data => $data,
                             desc => { class => 'Patient' }
                           )
          } qr/no attributes with column numbers in descriptor:/,
          "no numeric attributes";

#----------------------------------------
# using set_class_ind()

package ObjWclass;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has [ qw(class other) ] => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;

package main;

throws_ok { array_to_moose ( data => $data,
                             desc => { class => 'ObjWclass', other => 0 }
                           )

t/7.t  view on Meta::CPAN


# remember to reset
set_class_ind();

#----------------------------------------
# using set_key_ind()

package ObjWkey;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has [ qw(key other) ] => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;

package main;

# only get error if class has key AND desc has key ...
lives_ok { array_to_moose ( data => $data,
                             desc => { class => 'ObjWkey', other => 0 }

t/7.t  view on Meta::CPAN

throws_ok { array_to_moose ( data => $data,
                             desc => { class => 'Patient', last => sub { } }
                           )
          } qr/attribute 'last' can't be a 'CODE' reference/,
          "non-sub attribute is CODE ref";

#----------------------------------------
package Other;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has [ qw(other) ] => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;
package main;

package Typeless;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has [ qw(this that SubObj) ] => (is => 'rw');  # no types!

__PACKAGE__->meta->make_immutable;
package main;

throws_ok { array_to_moose ( data => $data,
                             desc => { class => 'Typeless',
                                       that => 0,
                                       SubObj => {
                                         other => 1,
                                       }
                                     }
                                      )
                      } qr/Moose class 'Typeless', attrib 'that' has no type/,
                      "typeless sub-object attribute";

package Typeless1;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;

has [ qw(this that) ] => (is => 'rw', isa => 'Str');
has         'SubObj'  => (is => 'rw');  # no types!

__PACKAGE__->meta->make_immutable;
package main;

throws_ok { array_to_moose ( data => $data,
                             desc => { class => 'Typeless1',
                                       that => 0,

t/8c.t  view on Meta::CPAN


plan tests => 3;

eval 'use VarianReportsMoose qw(print_obj)';

use Data::Dumper;

use Carp;

package Employer;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has 'year'    => (is => 'rw', isa => 'Str');
has 'name'    => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;

package Person;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has 'name'        => (is => 'rw', isa => 'Str'              );
has 'Employers'   => (is => 'rw', isa => 'HashRef[Employer]');

__PACKAGE__->meta->make_immutable;

package main;

t/8d.t  view on Meta::CPAN


plan tests => 3;

eval 'use VarianReportsMoose qw(print_obj)';

use Data::Dumper;

use Carp;

package Salary;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has 'year'    => (is => 'rw', isa => 'Str');
has 'amount'  => (is => 'rw', isa => 'Int');

__PACKAGE__->meta->make_immutable;

package Person;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;

has 'name'     => (is => 'rw', isa => 'Str'   );
has 'Salary'   => (is => 'rw', isa => 'Salary'); # a single object

__PACKAGE__->meta->make_immutable;

package main;

t/9.t  view on Meta::CPAN


plan tests => 2;

eval 'use VarianReportsMoose qw(print_obj)';

use Data::Dumper;

use Carp;

package Point;
use namespace::autoclean;
use Moose;
use MooseX::StrictConstructor;
has 'x' => (is => 'ro', isa => 'Int');
has 'y' => (is => 'ro', isa => 'Int');

__PACKAGE__->meta->make_immutable;

package main;

sub Npt { Point->new(x => $_[0], y => $_[1]) }



( run in 0.753 second using v1.01-cache-2.11-cpan-a5abf4f5562 )