[Moose-commits] r7821 - in Mouse/trunk: lib lib/Mouse/Meta t
tokuhirom at code2.0beta.co.uk
tokuhirom at code2.0beta.co.uk
Wed Mar 4 23:49:51 GMT 2009
Author: tokuhirom
Date: 2009-03-04 15:49:51 -0800 (Wed, 04 Mar 2009)
New Revision: 7821
Added:
Mouse/trunk/t/046-meta-add_attribute.t
Modified:
Mouse/trunk/lib/Mouse.pm
Mouse/trunk/lib/Mouse/Meta/Class.pm
Log:
added YourClass->meta->add_attribute(foo => (is => 'ro', isa => 'Str')); support.
Modified: Mouse/trunk/lib/Mouse/Meta/Class.pm
===================================================================
--- Mouse/trunk/lib/Mouse/Meta/Class.pm 2009-03-04 22:38:48 UTC (rev 7820)
+++ Mouse/trunk/lib/Mouse/Meta/Class.pm 2009-03-04 23:49:51 UTC (rev 7821)
@@ -98,9 +98,35 @@
sub add_attribute {
my $self = shift;
- my $attr = shift;
- $self->{'attributes'}{$attr->name} = $attr;
+ if (@_ == 1 && blessed($_[0])) {
+ my $attr = shift @_;
+ $self->{'attributes'}{$attr->name} = $attr;
+ } else {
+ my $names = shift @_;
+ $names = [$names] if !ref($names);
+ my $metaclass = 'Mouse::Meta::Attribute';
+ my %options = @_;
+
+ if ( my $metaclass_name = delete $options{metaclass} ) {
+ my $new_class = Mouse::Util::resolve_metaclass_alias(
+ 'Attribute',
+ $metaclass_name
+ );
+ if ( $metaclass ne $new_class ) {
+ $metaclass = $new_class;
+ }
+ }
+
+ for my $name (@$names) {
+ if ($name =~ s/^\+//) {
+ $metaclass->clone_parent($self, $name, @_);
+ }
+ else {
+ $metaclass->create($self, $name, @_);
+ }
+ }
+ }
}
sub compute_all_applicable_attributes {
@@ -351,7 +377,7 @@
Gets (or sets) the list of superclasses of the owner class.
-=head2 add_attribute Mouse::Meta::Attribute
+=head2 add_attribute (Mouse::Meta::Attribute| name => spec)
Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
class.
Modified: Mouse/trunk/lib/Mouse.pm
===================================================================
--- Mouse/trunk/lib/Mouse.pm 2009-03-04 22:38:48 UTC (rev 7820)
+++ Mouse/trunk/lib/Mouse.pm 2009-03-04 23:49:51 UTC (rev 7821)
@@ -29,30 +29,7 @@
sub has {
my $meta = Mouse::Meta::Class->initialize(caller);
-
- my $names = shift;
- $names = [$names] if !ref($names);
- my $metaclass = 'Mouse::Meta::Attribute';
- my %options = @_;
-
- if ( my $metaclass_name = delete $options{metaclass} ) {
- my $new_class = Mouse::Util::resolve_metaclass_alias(
- 'Attribute',
- $metaclass_name
- );
- if ( $metaclass ne $new_class ) {
- $metaclass = $new_class;
- }
- }
-
- for my $name (@$names) {
- if ($name =~ s/^\+//) {
- $metaclass->clone_parent($meta, $name, @_);
- }
- else {
- $metaclass->create($meta, $name, @_);
- }
- }
+ $meta->add_attribute(@_);
}
sub before {
Added: Mouse/trunk/t/046-meta-add_attribute.t
===================================================================
--- Mouse/trunk/t/046-meta-add_attribute.t (rev 0)
+++ Mouse/trunk/t/046-meta-add_attribute.t 2009-03-04 23:49:51 UTC (rev 7821)
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use Test::More tests => 1;
+
+{
+ package Foo;
+ use Mouse;
+}
+
+Foo->meta->add_attribute(
+ 'foo' => (
+ is => 'ro',
+ isa => 'Str',
+ default => 'bar',
+ )
+);
+is(Foo->new->foo, 'bar');
More information about the Moose-commits
mailing list