Acme-Has-Tiny

 view release on metacpan or  search on metacpan

lib/Acme/Has/Tiny.pm  view on Meta::CPAN

		elsif ($isa)
		{
			push @code, (
				sprintf('if (exists($self->{%s})) {', $a),
				sprintf('$Acme::Has::Tiny::ATTRIBUTES{%s}{%s}{isa}->($self->{%s});', map perlstring($_), $class, $a, $a),
				'}',
			);
		}
	}
	
	return @code;
}

sub _find_parents
{
	my $me = shift;
	my $class = $_[0];
	
	if (eval { require mro } or eval { require MRO::Compat })
	{
		return @{ mro::get_linear_isa($class) };
	}
	
	require Class::ISA;
	return Class::ISA::self_and_super_path($class);
}

1;

__END__

=pod

=encoding utf-8

=for stopwords ro rw rwp isa

=head1 NAME

Acme::Has::Tiny - tiny implementation of Moose-like "has" keyword

=head1 SYNOPSIS

   package Person;
   
   use Acme::Has::Tiny qw(new has);
   use Types::Standard -types;
   
   has name => (isa => Str);
   has age  => (isa => Num);

=head1 DESCRIPTION

Acme::Has::Tiny provides a Moose-like C<has> function. It is not
particularly full-featured, providing just enough to be useful for
small OO projects.

Generally speaking, I'd recommend using L<Moo> or L<Moose> instead, but
if you want to use this then I'm fairly unlikely to hunt you down with dogs.

This module was originally written for Type::Tiny, but turned out to be
just a smidgen slower than the system it was replacing, so was abandoned.

=head2 Methods

=over

=item C<< has \@attrs, %spec >>

=item C<< has $attr, %spec >>

Create an attribute. This method can also be exported as a usable function.

The specification supports the following options:

=over

=item C<< is => "ro" | "rw" | "rwp" >>

Defaults to "ro".

=item C<< required => 1 >>

=item C<< default => $coderef >>

Defaults are always eager (not lazy).

=item C<< builder => $coderef | $method_name | 1 >>

Builders are always lazy.

=item C<< predicate => $method_name | 1 >>

=item C<< isa => $type >>

Type constraint (use L<Types::Standard> or another L<Type::Library>-based
type constraint library).

=back

=item C<< create_constructor $method_name, %options >>

If you want a constructor, then you could call this B<after> defining
your attributes. (Or you could just import C<new> from this module.)

   package Person;
   
   use Acme::Has::Tiny qw(has);
   use Types::Standard -types;
   
   has name => (isa => Str);
   has age  => (isa => Num);
   
   Acme::Has::Tiny->create_constructor("new");
   Acme::Has::Tiny->create_constructor(
      "new_from_arrayref",
      buildargs => sub {
         my ($class, $aref) = @_;
         return { name => $aref->[0], age => $aref->[1] };
      },
   );



( run in 1.225 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )