X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FClass.pm;h=9ede6f8a56a618221ad3fe86972e7307ff10b48a;hp=7d4cdcd1f28d005b44b6075c19fe068039caa985;hb=53c495ce524c95ee0fc1ee13f20edeeb382ef89f;hpb=4f9945f5a128e120049ce8a7a30cf469d1568b9b diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index 7d4cdcd..9ede6f8 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -9,6 +9,7 @@ use Mouse::Util qw/get_linear_isa not_supported/; use base qw(Mouse::Meta::Module); +sub method_metaclass(){ 'Mouse::Meta::Method' } # required for get_method() sub _new { my($class, %args) = @_; @@ -22,7 +23,19 @@ sub _new { \@{ $args{package} . '::ISA' }; }; - bless \%args, $class; + #return Mouse::Meta::Class->initialize($class)->new_object(%args) + # if $class ne __PACKAGE__; + + return bless \%args, $class; +} + +sub create_anon_class{ + my $self = shift; + return $self->create(undef, @_); +} + +sub is_anon_class{ + return exists $_[0]->{anon_serial_id}; } sub roles { $_[0]->{roles} } @@ -52,11 +65,12 @@ sub add_attribute { if (@_ == 1 && blessed($_[0])) { my $attr = shift @_; $self->{'attributes'}{$attr->name} = $attr; - } else { + } + else { my $names = shift @_; $names = [$names] if !ref($names); my $metaclass = 'Mouse::Meta::Attribute'; - my %options = @_; + my %options = (@_ == 1 ? %{$_[0]} : @_); if ( my $metaclass_name = delete $options{metaclass} ) { my $new_class = Mouse::Util::resolve_metaclass_alias( @@ -70,10 +84,10 @@ sub add_attribute { for my $name (@$names) { if ($name =~ s/^\+//) { - $metaclass->clone_parent($self, $name, @_); + $metaclass->clone_parent($self, $name, %options); } else { - $metaclass->create($self, $name, @_); + $metaclass->create($self, $name, %options); } } } @@ -101,26 +115,28 @@ sub linearized_isa { @{ get_linear_isa($_[0]->name) } } sub new_object { my $self = shift; - my $args = (@_ == 1) ? $_[0] : { @_ }; + my %args = (@_ == 1 ? %{$_[0]} : @_); my $instance = bless {}, $self->name; + my @triggers_queue; + foreach my $attribute ($self->get_all_attributes) { my $from = $attribute->init_arg; my $key = $attribute->name; - if (defined($from) && exists($args->{$from})) { - $args->{$from} = $attribute->coerce_constraint($args->{$from}) + if (defined($from) && exists($args{$from})) { + $args{$from} = $attribute->coerce_constraint($args{$from}) if $attribute->should_coerce; - $attribute->verify_against_type_constraint($args->{$from}); + $attribute->verify_against_type_constraint($args{$from}); - $instance->{$key} = $args->{$from}; + $instance->{$key} = $args{$from}; weaken($instance->{$key}) if ref($instance->{$key}) && $attribute->is_weak_ref; if ($attribute->has_trigger) { - $attribute->trigger->($instance, $args->{$from}); + push @triggers_queue, [ $attribute->trigger, $args{$from} ]; } } else { @@ -151,6 +167,12 @@ sub new_object { } } } + + foreach my $trigger_and_value(@triggers_queue){ + my($trigger, $value) = @{$trigger_and_value}; + $trigger->($instance, $value); + } + return $instance; } @@ -209,7 +231,8 @@ sub make_immutable { sub make_mutable { not_supported } -sub is_immutable { $_[0]->{is_immutable} } +sub is_immutable { $_[0]->{is_immutable} } +sub is_mutable { !$_[0]->{is_immutable} } sub _install_modifier { my ( $self, $into, $type, $name, $code ) = @_; @@ -237,6 +260,8 @@ sub _install_modifier { $name, $code ); + $self->{methods}{$name}++; # register it to the method map + return; }; } @@ -262,16 +287,12 @@ sub add_after_method_modifier { sub add_override_method_modifier { my ($self, $name, $code) = @_; - my $pkg = $self->name; - my $method = "${pkg}::${name}"; - - # Class::Method::Modifiers won't do this for us, so do it ourselves + my $package = $self->name; - my $body = $pkg->can($name) - or $self->throw_error("You cannot override '$method' because it has no super method"); + my $body = $package->can($name) + or $self->throw_error("You cannot override '$name' because it has no super method"); - no strict 'refs'; - *$method = sub { $code->($pkg, $body, @_) }; + $self->add_method($name => sub { $code->($package, $body, @_) }); } sub does_role { @@ -281,10 +302,11 @@ sub does_role { || $self->throw_error("You must supply a role name to look for"); for my $class ($self->linearized_isa) { - my $meta = Mouse::class_of($class); + my $meta = Mouse::Meta::Module::class_of($class); next unless $meta && $meta->can('roles'); for my $role (@{ $meta->roles }) { + return 1 if $role->does_role($role_name); } } @@ -292,81 +314,6 @@ sub does_role { return 0; } -sub create { - my ($class, $package_name, %options) = @_; - - (ref $options{superclasses} eq 'ARRAY') - || $class->throw_error("You must pass an ARRAY ref of superclasses") - if exists $options{superclasses}; - - (ref $options{attributes} eq 'ARRAY') - || $class->throw_error("You must pass an ARRAY ref of attributes") - if exists $options{attributes}; - - (ref $options{methods} eq 'HASH') - || $class->throw_error("You must pass a HASH ref of methods") - if exists $options{methods}; - - do { - ( defined $package_name && $package_name ) - || $class->throw_error("You must pass a package name"); - - my $code = "package $package_name;"; - $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';" - if exists $options{version}; - $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';" - if exists $options{authority}; - - eval $code; - $class->throw_error("creation of $package_name failed : $@") if $@; - }; - - my %initialize_options = %options; - delete @initialize_options{qw( - package - superclasses - attributes - methods - version - authority - )}; - my $meta = $class->initialize( $package_name => %initialize_options ); - - # FIXME totally lame - $meta->add_method('meta' => sub { - Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]); - }); - - $meta->superclasses(@{$options{superclasses}}) - if exists $options{superclasses}; - # NOTE: - # process attributes first, so that they can - # install accessors, but locally defined methods - # can then overwrite them. It is maybe a little odd, but - # I think this should be the order of things. - if (exists $options{attributes}) { - foreach my $attr (@{$options{attributes}}) { - Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr); - } - } - if (exists $options{methods}) { - foreach my $method_name (keys %{$options{methods}}) { - $meta->add_method($method_name, $options{methods}->{$method_name}); - } - } - return $meta; -} - -{ - my $ANON_CLASS_SERIAL = 0; - my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::'; - sub create_anon_class { - my ( $class, %options ) = @_; - my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL; - return $class->create( $package_name, %options ); - } -} - 1; __END__