add myself to committer list
[gitmo/Moo.git] / lib / Moo.pm
index 7148502..a6c6fc1 100644 (file)
@@ -5,7 +5,7 @@ use Moo::_Utils;
 use B 'perlstring';
 use Sub::Defer ();
 
-our $VERSION = '1.000003'; # 1.0.3
+our $VERSION = '1.000008'; # 1.0.8
 $VERSION = eval $VERSION;
 
 require Moo::sification;
@@ -22,8 +22,10 @@ sub import {
   my $target = caller;
   my $class = shift;
   strictures->import;
-  return if $MAKERS{$target}; # already exported into this package
-  $MAKERS{$target} = {};
+  if ($Moo::Role::INFO{$target} and $Moo::Role::INFO{$target}{is_role}) {
+    die "Cannot import Moo into a role";
+  }
+  $MAKERS{$target} ||= {};
   _install_tracked $target => extends => sub {
     $class->_set_superclasses($target, @_);
     $class->_maybe_reset_handlemoose($target);
@@ -35,12 +37,18 @@ sub import {
     $class->_maybe_reset_handlemoose($target);
   };
   _install_tracked $target => has => sub {
-    my ($name, %spec) = @_;
-    $class->_constructor_maker_for($target)
-          ->register_attribute_specs($name, \%spec);
-    $class->_accessor_maker_for($target)
-          ->generate_method($target, $name, \%spec);
-    $class->_maybe_reset_handlemoose($target);
+    my ($name_proto, %spec) = @_;
+    my $name_isref = ref $name_proto eq 'ARRAY';
+    foreach my $name ($name_isref ? @$name_proto : $name_proto) {
+      # Note that when $name_proto is an arrayref, each attribute
+      # needs a separate \%specs hashref
+      my $spec_ref = $name_isref ? +{%spec} : \%spec;
+      $class->_constructor_maker_for($target)
+            ->register_attribute_specs($name, $spec_ref);
+      $class->_accessor_maker_for($target)
+            ->generate_method($target, $name, $spec_ref);
+      $class->_maybe_reset_handlemoose($target);
+    }
     return;
   };
   foreach my $type (qw(before after around)) {
@@ -50,6 +58,8 @@ sub import {
       return;
     };
   }
+  return if $MAKERS{$target}{is_class}; # already exported into this package
+  $MAKERS{$target}{is_class} = 1;
   {
     no strict 'refs';
     @{"${target}::ISA"} = do {
@@ -69,11 +79,11 @@ sub unimport {
 sub _set_superclasses {
   my $class = shift;
   my $target = shift;
-  for (@_) {
-    _load_module($_);
-    if ($INC{"Role/Tiny.pm"} && $Role::Tiny::INFO{$_}) {
+  foreach my $superclass (@_) {
+    _load_module($superclass);
+    if ($INC{"Role/Tiny.pm"} && $Role::Tiny::INFO{$superclass}) {
       require Carp;
-      Carp::croak("Can't extend role '$_'");
+      Carp::croak("Can't extend role '$superclass'");
     }
   }
   # Can't do *{...} = \@_ or 5.10.0's mro.pm stops seeing @ISA
@@ -86,7 +96,7 @@ sub _set_superclasses {
   no warnings 'once'; # piss off. -- mst
   $Moo::HandleMoose::MOUSE{$target} = [
     grep defined, map Mouse::Util::find_meta($_), @_
-  ] if $INC{"Mouse.pm"};
+  ] if Mouse::Util->can('find_meta');
 }
 
 sub _maybe_reset_handlemoose {
@@ -202,7 +212,7 @@ Moo - Minimalist Object Orientation (with Moose compatiblity)
    isa => sub {
      die "Only SWEET-TREATZ supported!" unless $_[0] eq 'SWEET-TREATZ'
    },
-);
+ );
 
  has pounds => (
    is  => 'rw',
@@ -234,10 +244,10 @@ thirds of L<Moose>.
 
 Unlike L<Mouse> this module does not aim at full compatibility with
 L<Moose>'s surface syntax, preferring instead of provide full interoperability
-via the metaclass inflation capabilites described in L</MOO AND MOOSE>.
+via the metaclass inflation capabilities described in L</MOO AND MOOSE>.
 
 For a full list of the minor differences between L<Moose> and L<Moo>'s surface
-syntax, see L</INCOMPATIBILITIES>.
+syntax, see L</INCOMPATIBILITIES WITH MOOSE>.
 
 =head1 WHY MOO EXISTS
 
@@ -420,7 +430,9 @@ C<lazy> generates a reader like C<ro>, but also sets C<lazy> to 1 and
 C<builder> to C<_build_${attribute_name}> to allow on-demand generated
 attributes.  This feature was my attempt to fix my incompetence when
 originally designing C<lazy_build>, and is also implemented by
-L<MooseX::AttributeShortcuts>.
+L<MooseX::AttributeShortcuts>. There is, however, nothing to stop you
+using C<lazy> and C<builder> yourself with C<rwp> or C<rw> - it's just that
+this isn't generally a good idea so we don't provide a shortcut for it.
 
 C<rwp> generates a reader like C<ro>, but also sets C<writer> to
 C<_set_${attribute_name}> for attributes that are designed to be written
@@ -499,11 +511,11 @@ Takes a hashref
    un => 'one',
  }
 
-=item * trigger
+=item * C<trigger>
 
 Takes a coderef which will get called any time the attribute is set. This
-includes the constructor. Coderef will be invoked against the object with the
-new value as an argument.
+includes the constructor, but not default or built values. Coderef will be
+invoked against the object with the new value as an argument.
 
 If you set this to just C<1>, it generates a trigger which calls the
 C<_trigger_${attr_name}> method on C<$self>. This feature comes from
@@ -547,8 +559,14 @@ Moo will call
 
   $self->$builder;
 
-If you set this to just C<1>, the predicate is automatically named
-C<_build_${attr_name}>.  This feature comes from L<MooseX::AttributeShortcuts>.
+The following features come from L<MooseX::AttributeShortcuts>:
+
+If you set this to just C<1>, the builder is automatically named
+C<_build_${attr_name}>.
+
+If you set this to a coderef or code-convertible object, that variable will be
+installed under C<$class::_build_${attr_name}> and the builder set to the same
+name.
 
 =item * C<clearer>
 
@@ -756,6 +774,9 @@ at the end of your class to get an inlined (i.e. not horribly slow)
 constructor. Moo does it automatically the first time ->new is called
 on your class.
 
+An extension L<MooX::late> exists to ease translating Moose packages
+to Moo by providing a more Moose-like interface.
+
 =head1 SUPPORT
 
 Users' IRC: #moose on irc.perl.org
@@ -790,6 +811,12 @@ Mithaldu - Christian Walde (cpan:MITHALDU) <walde.christian@googlemail.com>
 
 ilmari - Dagfinn Ilmari MannsÃ¥ker (cpan:ILMARI) <ilmari@ilmari.org>
 
+tobyink - Toby Inkster (cpan:TOBYINK) <tobyink@cpan.org>
+
+haarg - Graham Knop (cpan:HAARG) <haarg@cpan.org>
+
+mattp - Matt Phillips (cpan:MATTP) <mattp@cpan.org>
+
 =head1 COPYRIGHT
 
 Copyright (c) 2010-2011 the Moo L</AUTHOR> and L</CONTRIBUTORS>