| 1 | # | ||||||||||
| 2 | # This file is part of MooseX-Meta-TypeConstraint-Mooish | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is Copyright (c) 2015 by Chris Weyl. | ||||||||||
| 5 | # | ||||||||||
| 6 | # This is free software, licensed under: | ||||||||||
| 7 | # | ||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||
| 9 | # | ||||||||||
| 10 | package MooseX::TraitFor::Meta::TypeConstraint::Mooish; | ||||||||||
| 11 | 4 | our $AUTHORITY = 'cpan:RSRCHBOY'; | |||||||||
| 12 | 4 | $MooseX::TraitFor::Meta::TypeConstraint::Mooish::VERSION = '0.001'; | |||||||||
| 13 | # ABSTRACT: Handle Moo-style constraints | ||||||||||
| 14 | |||||||||||
| 15 | 4 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 4 | ▶ | use namespace::autoclean 0.24; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 4 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | 4 | has original_constraint => ( | |||||||||
| 21 | is => 'ro', | ||||||||||
| 22 | isa => 'CodeRef', | ||||||||||
| 23 | writer => '_set_original_constraint', | ||||||||||
| 24 | predicate => 'has_original_constraint', | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | 4 | has mooish => ( | |||||||||
| 29 | is => 'ro', | ||||||||||
| 30 | isa => 'Bool', | ||||||||||
| 31 | default => 1, | ||||||||||
| 32 | ); | ||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | 1 | ▶ | before compile_type_constraint => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 36 | 1 | my $self = shift @_; | |||||||||
| 37 | |||||||||||
| 38 | ### only wrap our given constraint iff we're supposed to be mooish... | ||||||||||
| 39 | 1 | ▶ | return unless $self->mooish; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | ### wrap our type constraint, and set it... | ||||||||||
| 42 | 1 | my $wrapped_constraint = $self->_wrap_constraint($self->constraint); | |||||||||
| 43 | 1 | $self->_set_original_constraint($self->constraint); | |||||||||
| 44 | 1 | $self->_set_constraint($wrapped_constraint); | |||||||||
| 45 | |||||||||||
| 46 | 1 | return; | |||||||||
| 47 | 4 | }; | |||||||||
| 48 | |||||||||||
| 49 | 1 | ▶ | sub _wrap_constraint { | ||||||||
|
Subroutine
_wrap_constraint: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 1 | my ($self, $constraint) = @_; | |||||||||
| 51 | |||||||||||
| 52 | # call the original constraint. if it does not die, return true; if it | ||||||||||
| 53 | # does die, return false. We might do something with the fail message | ||||||||||
| 54 | # down the road, but not right now. | ||||||||||
| 55 | |||||||||||
| 56 | 2 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 2 | my @args = @_; | |||||||||
| 58 | 2 | ▶ | my $fail_msg = try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 59 | 2 | local $_ = $args[0]; | |||||||||
| 60 | 2 | $constraint->(@args); | |||||||||
| 61 | 1 | return; | |||||||||
| 62 | } | ||||||||||
| 63 | 1 | ▶ | catch { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 1 | return $_; | |||||||||
| 65 | 2 | }; | |||||||||
| 66 | |||||||||||
| 67 | 2 | return !$fail_msg; | |||||||||
| 68 | 1 | }; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | around create_child_type => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 73 | 0 | my ($orig, $self) = (shift, shift); | |||||||||
| 74 | |||||||||||
| 75 | 0 | return $self->$orig(mooish => 0, @_); | |||||||||
| 76 | 4 | }; | |||||||||
| 77 | |||||||||||
| 78 | 4 | !!42; | |||||||||
| 79 | |||||||||||
| 80 | __END__ | ||||||||||