Acme-Has-Tiny

 view release on metacpan or  search on metacpan

CONTRIBUTING  view on Meta::CPAN

        not, then try <https://github.com/tobyink> or submit a bug report.
        (As far as I'm concerned the lack of a link is a bug.) Many of my
        distributions are also mirrored at <https://bitbucket.org/tobyink>.

        To submit the patch, do a pull request on GitHub or Bitbucket, or
        attach a diff file to a bug report. Unless otherwise stated, I'll
        assume that your contributions are licensed under the same terms as
        the rest of the project.

        (If using git, feel free to work in a branch. For Mercurial, I'd
        prefer bookmarks within the default branch.)

    *   Documentation

        If there's anything unclear in the documentation, please submit this
        as a bug report or patch as above.

        Non-toy example scripts that I can bundle would also be appreciated.

    *   Translation

README  view on Meta::CPAN

    `has $attr, %spec`
        Create an attribute. This method can also be exported as a usable
        function.

        The specification supports the following options:

        `is => "ro" | "rw" | "rwp"`
            Defaults to "ro".

        `required => 1`
        `default => $coderef`
            Defaults are always eager (not lazy).

        `builder => $coderef | $method_name | 1`
            Builders are always lazy.

        `predicate => $method_name | 1`
        `isa => $type`
            Type constraint (use Types::Standard or another
            Type::Library-based type constraint library).

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

	my ($class, $hash) = @_;
	
	my @validator = map {
		$VALIDATORS{$_} ||= $me->_compile_validator($_, $ATTRIBUTES{$_});
	} $me->_find_parents($class);
	
	$_->($hash) for @validator;
	return $hash;
}

my $default_buildargs = sub
{
	my $class = shift;
	return +{
		(@_ == 1 && ref($_[0]) eq q(HASH)) ? %{$_[0]} : @_
	};
};

sub create_constructor
{
	my $me = shift;
	my ($method, %options) = @_;
	
	my $class     = $options{class} || caller;
	my $build     = $options{build};
	my $buildargs = $options{buildargs} || $default_buildargs;
	
	my $code = sub
	{
		my $class = shift;
		my $self = bless($class->$buildargs(@_), $class);
		$me->assert_valid($class, $self);
		$self->$build if $options{build};
		return $self;
	};
	

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

sub _build_validator_parts
{
	my $me = shift;
	my ($class, $attributes) = @_;
	
	my @code;
	for my $a (sort keys %$attributes)
	{
		my $spec = $attributes->{$a};
		
		if ($spec->{default})
		{
			push @code, sprintf(
				'exists($self->{%s}) or $self->{%s} = $Acme::Has::Tiny::ATTRIBUTES{%s}{%s}{default}->();',
				map perlstring($_), $a, $a, $class, $a,
			);
		}
		elsif ($spec->{required})
		{
			push @code, sprintf(
				'exists($self->{%s}) or Acme::Has::Tiny::_croak("Attribute %%s is required by %%s", %s, %s);',
				map perlstring($_), $a, $a, $class,
			);
		}

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

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 >>



( run in 0.455 second using v1.01-cache-2.11-cpan-0a6323c29d9 )