Skip to content

Instantly share code, notes, and snippets.

@tobyink
Created May 23, 2017 14:46
Show Gist options
  • Select an option

  • Save tobyink/efe161cd25ddf642f34b6e3d858d93a5 to your computer and use it in GitHub Desktop.

Select an option

Save tobyink/efe161cd25ddf642f34b6e3d858d93a5 to your computer and use it in GitHub Desktop.
package My::Person;
use Moo;
use MooX::Press;
has name => RO REQUIRED Str, documentation => 'Full name';
has favourite_number => RO DEFAULT { 7 } Int;
1;
use strict;
use warnings;
package MooX::Press;
use parent qw( Exporter::Tiny );
use Scalar::Util qw( blessed );
use Types::Standard qw( HashRef InstanceOf );
use namespace::autoclean qw();
our @EXPORT = qw(
RO RW RWP LAZY REQUIRED
BUILDER TRIGGER DEFAULT
true false blessed confess
);
sub _exporter_validate_opts {
my $class = shift;
my ($opt) = @_;
'Types::Standard'->import({ into => $opt->{into} }, -types)
unless $opt->{notypes};
'namespace::autoclean'->import( -cleanee => $opt->{into} )
unless $opt->{noclean} || ref $opt->{into};
}
sub tidy_spec {
my @spec;
while (@_) {
if (InstanceOf->of('Type::Tiny')->check($_[0])) {
push @spec, isa => shift;
}
elsif (HashRef->check($_[0])) {
push @spec, isa => shift;
}
else {
push @spec, splice @_, 0, 2;
}
}
@spec;
}
sub true () { !!1 }
sub false () { !!0 }
sub confess {
my $msg = shift;
require Carp;
@_ = sprintf($msg, @_);
goto \&Carp::confess; # hide myself from stack trace
}
sub RO {
( tidy_spec(@_), is => 'ro' );
}
sub RW {
( tidy_spec(@_), is => 'rw' );
}
sub RWP {
( tidy_spec(@_), is => 'rwp' );
}
sub LAZY {
my %spec = (my @tidy = tidy_spec(@_));
(
is => 'ro',
($spec{default} or $spec{builder})
? () :
(builder => true),
@tidy,
lazy => true,
);
}
sub REQUIRED {
( tidy_spec(@_), required => true );
}
sub BUILDER (&@) {
my $code = shift;
( tidy_spec(@_), builder => $code );
}
sub TRIGGER (&@) {
my $code = shift;
( tidy_spec(@_), trigger => $code );
}
sub DEFAULT (&@) {
my $code = shift;
( tidy_spec(@_), default => $code );
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment