$meta->alias_method('extends' => subname 'Moose::extends' => sub { $meta->superclasses(@_) });
# handle attributes
- $meta->alias_method('has' => subname 'Moose::has' => sub { $meta->add_attribute(@_) });
+ $meta->alias_method('has' => subname 'Moose::has' => sub {
+ my ($name, %options) = @_;
+ if (exists $options{is}) {
+ $options{type_constraint} = $options{is};
+ }
+ elsif (exists $options{isa}) {
+ $options{type_constraint} = Moose::Util::TypeConstraints::subtype(
+ Object => Moose::Util::TypeConstraints::where { $_->isa($options{isa}) }
+ );
+ }
+ $meta->add_attribute($name, %options)
+ });
# handle method modifers
$meta->alias_method('before' => subname 'Moose::before' => sub {
Moose::Meta::Attribute->meta->add_before_method_modifier('new' => sub {
my (undef, undef, %options) = @_;
(reftype($options{type_constraint}) && reftype($options{type_constraint}) eq 'CODE')
- || confess "Type cosntraint parameter must be a code-ref";
+ || confess "Type cosntraint parameter must be a code-ref, not " . $options{type_constraint}
+ if exists $options{type_constraint};
});
sub generate_accessor_method {
use strict;
use warnings;
use Moose;
-
+
has 'x' => (
- reader => 'x',
- type_constraint => Int(),
+ is => Int(),
+ reader => 'x',
);
-
+
has 'y' => (
- accessor => 'y',
- type_constraint => Int(),
+ is => Int(),
+ accessor => 'y',
);
sub clear {
extends 'Point';
- has 'z' => (type_constraint => Int());
+ has 'z' => (is => Int());
after 'clear' => sub {
my $self = shift;
use Moose;
has 'balance' => (
- accessor => 'balance',
- default => 0,
- type_constraint => Int(),
+ is => Int(),
+ accessor => 'balance',
+ default => 0,
);
sub deposit {
extends 'BankAccount';
has 'overdraft_account' => (
- accessor => 'overdraft_account',
- type_constraint => subtype Object => where { $_->isa('BankAccount') },
+ isa => 'BankAccount',
+ accessor => 'overdraft_account',
);
before 'withdraw' => sub {
use Moose;
has 'parent' => (
- predicate => 'has_parent',
- accessor => 'parent',
- weak_ref => 1,
- type_constraint => subtype Object => where { $_->isa('BinaryTree') },
+ isa => 'BinaryTree',
+ predicate => 'has_parent',
+ accessor => 'parent',
+ weak_ref => 1,
);
has 'left' => (
- predicate => 'has_left',
- accessor => 'left',
- type_constraint => subtype Object => where { $_->isa('BinaryTree') },
+ isa => 'BinaryTree',
+ predicate => 'has_left',
+ accessor => 'left',
);
has 'right' => (
- predicate => 'has_right',
- accessor => 'right',
- type_constraint => subtype Object => where { $_->isa('BinaryTree') },
+ isa => 'BinaryTree',
+ predicate => 'has_right',
+ accessor => 'right',
);
before 'right', 'left' => sub {