File Coverage

blib/lib/MooseX/TraitFor/Meta/TypeConstraint/Mooish.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 23 23 100.0


line stmt bran cond sub pod time code
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             our $AUTHORITY = 'cpan:RSRCHBOY';
12             $MooseX::TraitFor::Meta::TypeConstraint::Mooish::VERSION = '0.001';
13             # ABSTRACT: Handle Moo-style constraints
14              
15 2     2   1777 use Moose::Role;
  2         6376  
  2         10  
16 2     2   8317 use namespace::autoclean 0.24;
  2         45  
  2         13  
17 2     2   112 use Try::Tiny;
  2         3  
  2         521  
18              
19              
20             has original_constraint => (
21             is => 'ro',
22             isa => 'CodeRef',
23             writer => '_set_original_constraint',
24             predicate => 'has_original_constraint',
25             );
26              
27              
28             has mooish => (
29             is => 'ro',
30             isa => 'Bool',
31             default => 1,
32             );
33              
34              
35             before compile_type_constraint => sub {
36             my $self = shift @_;
37              
38             ### only wrap our given constraint iff we're supposed to be mooish...
39             return unless $self->mooish;
40              
41             ### wrap our type constraint, and set it...
42             my $wrapped_constraint = $self->_wrap_constraint($self->constraint);
43             $self->_set_original_constraint($self->constraint);
44             $self->_set_constraint($wrapped_constraint);
45              
46             return;
47             };
48              
49             sub _wrap_constraint {
50 1     1   7 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             return sub {
57 2     2   406 my @args = @_;
58             my $fail_msg = try {
59 2         69 local $_ = $args[0];
60 2         5 $constraint->(@args);
61 1         4 return;
62             }
63             catch {
64 1         39 return $_;
65 2         14 };
66              
67 2         25 return !$fail_msg;
68 1         7 };
69             }
70              
71              
72             around create_child_type => sub {
73             my ($orig, $self) = (shift, shift);
74              
75             return $self->$orig(mooish => 0, @_);
76             };
77              
78             !!42;
79              
80             __END__
81              
82             =pod
83              
84             =encoding UTF-8
85              
86             =for :stopwords Chris Weyl mooish
87              
88             =for :stopwords Wishlist flattr flattr'ed gittip gittip'ed
89              
90             =head1 NAME
91              
92             MooseX::TraitFor::Meta::TypeConstraint::Mooish - Handle Moo-style constraints
93              
94             =head1 VERSION
95              
96             This document describes version 0.001 of MooseX::TraitFor::Meta::TypeConstraint::Mooish - released March 12, 2015 as part of MooseX-Meta-TypeConstraint-Mooish.
97              
98             =head1 SYNOPSIS
99              
100             This trait implements the functionality described in
101             L<MooseX::Meta::TypeConstraint::Mooish>, and you, dear reader, are encouraged
102             to read about it over there. Here we simply document the nuts and bolts.
103              
104             =head1 DESCRIPTION
105              
106             # determining where this goes is left as an exercise for the reader
107             with 'MooseX::TraitFor::Meta::TypeConstraint::Mooish';
108              
109             =head1 ATTRIBUTES
110              
111             =head2 original_constraint
112              
113             The original constraint CodeRef is stashed away here.
114              
115             =head2 mooish
116              
117             If true, the constraint should be considered written in the style of L<Moo> constraints; that is, if an exception is thrown the
118             constraint is considered to have failed; otherwise it passes. Return values are ignored.
119              
120             Default is true.
121              
122             =head1 METHODS
123              
124             =head2 original_constraint()
125              
126             Reader for the L</original_constraint> attribute; returns the original
127             constraint as passed to new().
128              
129             =head2 has_original_constraint()
130              
131             Predicate for the L</original_constraint> attribute.
132              
133             =head2 mooish()
134              
135             Reader for the L</mooish> attribute.
136              
137             =head2 compile_type_constraint
138              
139             If L</mooish> is true, we wrap the L</original_constraint> in a sub that translates L<Moo> behaviors
140             (die on fail; otherwise success) to L<Moose::Meta::TypeConstraint> expectations (false on fail; true on success).
141              
142             We stash the original constraint in L</original_constraint> (surprise!),
143             and set the L<constraint attribute|Moose::Meta::TypeConstraint/constraint> to
144             the wrapped constraint.
145              
146             =head2 create_child_type
147              
148             Subtypes created here are not mooish, unless an explicit C<mooish => 1> is
149             passed.
150              
151             =head1 SEE ALSO
152              
153             Please see those modules/websites for more information related to this module.
154              
155             =over 4
156              
157             =item *
158              
159             L<MooseX::Meta::TypeConstraint::Mooish|MooseX::Meta::TypeConstraint::Mooish>
160              
161             =item *
162              
163             L<MooseX::Meta::TypeConstraint::Mooish|MooseX::Meta::TypeConstraint::Mooish>
164              
165             =back
166              
167             =head1 SOURCE
168              
169             The development version is on github at L<http://https://github.com/RsrchBoy/moosex-meta-typeconstraint-mooish>
170             and may be cloned from L<git://https://github.com/RsrchBoy/moosex-meta-typeconstraint-mooish.git>
171              
172             =head1 BUGS
173              
174             Please report any bugs or feature requests on the bugtracker website
175             https://github.com/RsrchBoy/moosex-meta-typeconstraint-mooish/issues
176              
177             When submitting a bug or request, please include a test-file or a
178             patch to an existing test-file that illustrates the bug or desired
179             feature.
180              
181             =head1 AUTHOR
182              
183             Chris Weyl <cweyl@alumni.drew.edu>
184              
185             =head2 I'm a material boy in a material world
186              
187             =begin html
188              
189             <a href="https://www.gittip.com/RsrchBoy/"><img src="https://raw.githubusercontent.com/gittip/www.gittip.com/master/www/assets/%25version/logo.png" /></a>
190             <a href="http://bit.ly/rsrchboys-wishlist"><img src="http://wps.io/wp-content/uploads/2014/05/amazon_wishlist.resized.png" /></a>
191             <a href="https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Fmoosex-meta-typeconstraint-mooish&title=RsrchBoy's%20CPAN%20MooseX-Meta-TypeConstraint-Mooish&tags=%22RsrchBoy's%20MooseX-Meta-TypeConstraint-Mooish%20in%20the%20CPAN%22"><img src="http://api.flattr.com/button/flattr-badge-large.png" /></a>
192              
193             =end html
194              
195             Please note B<I do not expect to be gittip'ed or flattr'ed for this work>,
196             rather B<it is simply a very pleasant surprise>. I largely create and release
197             works like this because I need them or I find it enjoyable; however, don't let
198             that stop you if you feel like it ;)
199              
200             L<Flattr this|https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Fmoosex-meta-typeconstraint-mooish&title=RsrchBoy's%20CPAN%20MooseX-Meta-TypeConstraint-Mooish&tags=%22RsrchBoy's%20MooseX-Meta-TypeConstraint-Mooish%20in%20the%20CPAN%22>,
201             L<gittip me|https://www.gittip.com/RsrchBoy/>, or indulge my
202             L<Amazon Wishlist|http://bit.ly/rsrchboys-wishlist>... If you so desire.
203              
204             =head1 COPYRIGHT AND LICENSE
205              
206             This software is Copyright (c) 2015 by Chris Weyl.
207              
208             This is free software, licensed under:
209              
210             The GNU Lesser General Public License, Version 2.1, February 1999
211              
212             =cut