use Scalar::Util 'blessed', 'weaken', 'reftype';
use Carp 'confess';
-our $VERSION = '0.08';
+our $VERSION = '0.10';
+our $AUTHORITY = 'cpan:STEVAN';
use Moose::Meta::Method::Accessor;
use Moose::Util::TypeConstraints ();
reader => 'handles',
predicate => 'has_handles',
));
+__PACKAGE__->meta->add_attribute('documentation' => (
+ reader => 'documentation',
+ predicate => 'has_documentation',
+));
sub new {
my ($class, $name, %options) = @_;
sub clone_and_inherit_options {
my ($self, %options) = @_;
- # you can change default, required and coerce
+ # you can change default, required, coerce and documentation
my %actual_options;
- foreach my $legal_option (qw(default coerce required)) {
+ foreach my $legal_option (qw(default coerce required documentation)) {
if (exists $options{$legal_option}) {
$actual_options{$legal_option} = $options{$legal_option};
delete $options{$legal_option};
if (exists $options->{is}) {
if ($options->{is} eq 'ro') {
- $options->{reader} = $name;
+ $options->{reader} ||= $name;
(!exists $options->{trigger})
|| confess "Cannot have a trigger on a read-only attribute";
}
if (!defined $val && $self->has_default) {
$val = $self->default($instance);
}
+
if (defined $val) {
if ($self->has_type_constraint) {
my $type_constraint = $self->type_constraint;
sub set_value {
my ($self, $instance, $value) = @_;
+
+ my $attr_name = $self->name;
+
+ if ($self->is_required) {
+ defined($value)
+ || confess "Attribute ($attr_name) is required, so cannot be set to undef";
+ }
+
+ if ($self->has_type_constraint) {
+
+ my $type_constraint = $self->type_constraint;
+
+ if ($self->should_coerce) {
+ $value = $type_constraint->coerce($value);
+ }
+ defined($type_constraint->_compiled_type_constraint->($value))
+ || confess "Attribute ($attr_name) does not pass the type constraint ("
+ . $type_constraint->name . ") with " . (defined($value) ? ("'" . $value . "'") : "undef")
+ if defined($value);
+ }
+
+ my $meta_instance = Class::MOP::Class->initialize(blessed($instance))
+ ->get_meta_instance;
+
+ $meta_instance->set_slot_value($instance, $attr_name, $value);
+
+ if (ref $value && $self->is_weak_ref) {
+ $meta_instance->weaken_slot_value($instance, $attr_name);
+ }
+
+ if ($self->has_trigger) {
+ $self->trigger->($instance, $value, $self);
+ }
}
sub get_value {
my ($self, $instance) = @_;
-}
-
-sub has_value {
- my ($self, $instance) = @_;
-}
-
-sub clear_value {
- my ($self, $instance) = @_;
+
+ if ($self->is_lazy) {
+ unless ($self->has_value($instance)) {
+ if ($self->has_default) {
+ my $default = $self->default($instance);
+ $self->set_value($instance, $default);
+ }
+ else {
+ $self->set_value($instance, undef);
+ }
+ }
+ }
+
+ if ($self->should_auto_deref) {
+
+ my $type_constraint = $self->type_constraint;
+
+ if ($type_constraint->is_a_type_of('ArrayRef')) {
+ my $rv = $self->SUPER::get_value($instance);
+ return unless defined $rv;
+ return wantarray ? @{ $rv } : $rv;
+ }
+ elsif ($type_constraint->is_a_type_of('HashRef')) {
+ my $rv = $self->SUPER::get_value($instance);
+ return unless defined $rv;
+ return wantarray ? %{ $rv } : $rv;
+ }
+ else {
+ confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'";
+ }
+
+ }
+ else {
+
+ return $self->SUPER::get_value($instance);
+ }
}
## installing accessors
(!$associated_class->has_method($handle))
|| confess "You cannot overwrite a locally defined method ($handle) with a delegation";
+ # NOTE:
+ # handles is not allowed to delegate
+ # any of these methods, as they will
+ # override the ones in your class, which
+ # is almost certainly not what you want.
+ next if $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle);
+
if ((reftype($method_to_call) || '') eq 'CODE') {
$associated_class->add_method($handle => $method_to_call);
}
($self->has_type_constraint)
|| confess "Cannot delegate methods based on a RegExpr without a type constraint (isa)";
return map { ($_ => $_) }
- grep { $handles } $self->_get_delegate_method_list;
+ grep { /$handles/ } $self->_get_delegate_method_list;
}
elsif (ref($handles) eq 'CODE') {
return $handles->($self, $self->_find_delegate_metaclass);
=item B<initialize_instance_slot>
-=item B<generate_accessor_method>
+=item B<install_accessors>
-=item B<generate_writer_method>
+=item B<accessor_metaclass>
-=item B<generate_reader_method>
+=item B<get_value>
-=item B<install_accessors>
+=item B<set_value>
=back
the invocant and the new value. This can be used to handle I<basic>
bi-directional relations.
+=item B<documentation>
+
+This is a string which contains the documentation for this attribute.
+It serves no direct purpose right now, but it might in the future
+in some kind of automated documentation system perhaps.
+
+=item B<has_documentation>
+
+Returns true if this meta-attribute has any documentation.
+
=back
=head1 BUGS
=head1 COPYRIGHT AND LICENSE
-Copyright 2006 by Infinity Interactive, Inc.
+Copyright 2006, 2007 by Infinity Interactive, Inc.
L<http://www.iinteractive.com>