Acme-Has-Tiny

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    `assert_valid($class, \%params)`
        Check that a hash of parameters is valid according to type constraints
        and required attributes of $class and any classes it inherits from.

        Returns the hashref or dies.

           sub new {
              my ($class, %params) = @_;
              ...; # other stuff here
              my $self = bless(
                 Acme::Has::Tiny->assert_valid($class, \%params),
                 $class,
              );
              ...; # other stuff here
              return $self;
           }

  Constants
    `CAN_HAZ_XS`
        Whether Class::XSAccessor can be used.

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


use 5.008;
use strict;
use warnings;
no warnings qw(uninitialized once void numeric);

our $AUTHORITY = "cpan:TOBYINK";
our $VERSION   = "0.002";

use B qw(perlstring);
use Scalar::Util qw(blessed);

our %ATTRIBUTES;
our %VALIDATORS;

sub _croak ($;@)
{
	my $msg = shift;
	require Carp;
	$Carp::CarpInternal{+__PACKAGE__} = 1;
	Carp::croak(sprintf($msg, @_));

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

	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;
	};
	
	no strict qw(refs);
	if ($options{replace})
	{
		no warnings qw(redefine);
		*{"$class\::$method"} = $code;

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

	return sprintf('sub %s { exists $_[0]{%s} }', $method, perlstring($attr));
}

sub _build_writer
{
	my $me = shift;
	my ($class, $attr, $spec, $method) = @_;
	
	my $inlined;
	my $isa = $spec->{isa};
	if (blessed($isa) and $isa->isa('Type::Tiny') and $isa->can_be_inlined)
	{
		$inlined = $isa->inline_assert('$_[1]');
	}
	elsif ($isa)
	{
		$inlined = sprintf('$Acme::Has::Tiny::ATTRIBUTES{%s}{%s}{isa}->($_[1]);', perlstring($class), perlstring($attr));
	}
	
	if (CAN_HAZ_XS and not $inlined)
	{

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

		: sprintf('sub %s {     $_[0]{%s} = $_[1] }', $method,           perlstring($attr));
}

sub _build_accessor
{
	my $me = shift;
	my ($class, $attr, $spec, $method) = @_;
	
	my $inlined;
	my $isa = $spec->{isa};
	if (blessed($isa) and $isa->can_be_inlined)
	{
		$inlined = $isa->inline_assert('$_[1]');
	}
	elsif ($isa)
	{
		$inlined = sprintf('$Acme::Has::Tiny::ATTRIBUTES{%s}{%s}{isa}->($_[1]);', perlstring($class), perlstring($attr));
	}
	
	if (CAN_HAZ_XS and not $inlined)
	{

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

		}
		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,
			);
		}
		
		my $isa = $spec->{isa};
		if (blessed($isa) and $isa->can_be_inlined)
		{
			push @code, (
				sprintf('if (exists($self->{%s})) {', $a),
				$isa->inline_assert(sprintf '$self->{%s}', perlstring($a)),
				'}',
			);
		}
		elsif ($isa)
		{
			push @code, (

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

=item C<< assert_valid($class, \%params) >>

Check that a hash of parameters is valid according to type constraints and
required attributes of C<< $class >> and any classes it inherits from.

Returns the hashref or dies.

   sub new {
      my ($class, %params) = @_;
      ...; # other stuff here
      my $self = bless(
         Acme::Has::Tiny->assert_valid($class, \%params),
         $class,
      );
      ...; # other stuff here
      return $self;
   }

=back

=head2 Constants



( run in 1.090 second using v1.01-cache-2.11-cpan-de7293f3b23 )