File Coverage

blib/lib/Datastar/SSE/Types.pm
Criterion Covered Total %
statement 25 72 34.7
branch 4 42 9.5
condition 2 25 8.0
subroutine 12 37 32.4
pod 0 15 0.0
total 43 191 22.5


line stmt bran cond sub pod time code
1 6     6   110 use 5.008001;
  6         21  
2 6     6   26 use strict;
  6         9  
  6         133  
3 6     6   24 use warnings;
  6         11  
  6         432  
4              
5             package Datastar::SSE::Types;
6              
7 6     6   29 use Exporter ();
  6         9  
  6         105  
8 6     6   21 use Carp qw( croak );
  6         9  
  6         1119  
9              
10             our $TLC_VERSION = "0.008";
11             our @ISA = qw( Exporter );
12             our @EXPORT;
13             our @EXPORT_OK;
14             our %EXPORT_TAGS = (
15             is => [],
16             types => [],
17             assert => [],
18             );
19              
20 0         0 BEGIN {
21             package Datastar::SSE::Types::TypeConstraint;
22 6     6   12108 our $LIBRARY = "Datastar::SSE::Types";
23              
24             use overload (
25             fallback => !!1,
26             '|' => 'union',
27 0     0   0 bool => sub { !! 1 },
28 0     0   0 '""' => sub { shift->{name} },
29             '&{}' => sub {
30 0     0   0 my $self = shift;
31 0     0   0 return sub { $self->assert_return( @_ ) };
  0         0  
32             },
33 6     6   35 );
  6         10  
  6         69  
34              
35             sub union {
36 0     0   0 my @types = grep ref( $_ ), @_;
37 0         0 my @checks = map $_->{check}, @types;
38             bless {
39 0 0   0   0 check => sub { for ( @checks ) { return 1 if $_->(@_) } return 0 },
  0         0  
  0         0  
40 0         0 name => join( '|', map $_->{name}, @types ),
41             union => \@types,
42             }, __PACKAGE__;
43             }
44              
45             sub check {
46 0     0   0 $_[0]{check}->( $_[1] );
47             }
48              
49             sub get_message {
50             sprintf '%s did not pass type constraint "%s"',
51             defined( $_[1] ) ? $_[1] : 'Undef',
52 0 0   0   0 $_[0]{name};
53             }
54              
55             sub validate {
56 0 0   0   0 $_[0]{check}->( $_[1] )
57             ? undef
58             : $_[0]->get_message( $_[1] );
59             }
60              
61             sub assert_valid {
62 0 0   0   0 $_[0]{check}->( $_[1] )
63             ? 1
64             : Carp::croak( $_[0]->get_message( $_[1] ) );
65             }
66              
67             sub assert_return {
68 0 0   0   0 $_[0]{check}->( $_[1] )
69             ? $_[1]
70             : Carp::croak( $_[0]->get_message( $_[1] ) );
71             }
72              
73             sub to_TypeTiny {
74 0 0   0   0 if ( $_[0]{union} ) {
75 0         0 require Type::Tiny::Union;
76             return 'Type::Tiny::Union'->new(
77             display_name => $_[0]{name},
78 0         0 type_constraints => [ map $_->to_TypeTiny, @{ $_[0]{union} } ],
  0         0  
79             );
80             }
81 0 0       0 if ( my $library = $_[0]{library} ) {
82 0         0 local $@;
83 0 0       0 eval "require $library; 1" or die $@;
84 0         0 my $type = $library->get_type( $_[0]{library_name} );
85 0 0       0 return $type if $type;
86             }
87 0         0 require Type::Tiny;
88 0         0 my $check = $_[0]{check};
89 0         0 my $name = $_[0]{name};
90             return 'Type::Tiny'->new(
91             name => $name,
92 0     0   0 constraint => sub { $check->( $_ ) },
93 0     0   0 inlined => sub { sprintf '%s::is_%s(%s)', $LIBRARY, $name, pop }
94 0         0 );
95             }
96              
97             sub DOES {
98 0 0   0   0 return 1 if $_[1] eq 'Type::API::Constraint';
99 0 0       0 return 1 if $_[1] eq 'Type::Library::Compiler::TypeConstraint';
100 0         0 shift->SUPER::DOES( @_ );
101             }
102             };
103              
104             # ArrayRef
105             {
106             my $type;
107             sub ArrayRef () {
108 0   0 0 0 0 $type ||= bless( { check => \&is_ArrayRef, name => "ArrayRef", library => "Types::Standard", library_name => "ArrayRef" }, "Datastar::SSE::Types::TypeConstraint" );
109             }
110              
111             sub is_ArrayRef ($) {
112 41     41 0 201 (ref($_[0]) eq 'ARRAY')
113             }
114              
115             sub assert_ArrayRef ($) {
116 0 0   0 0 0 (ref($_[0]) eq 'ARRAY') ? $_[0] : ArrayRef->get_message( $_[0] );
117             }
118              
119             $EXPORT_TAGS{"ArrayRef"} = [ qw( ArrayRef is_ArrayRef assert_ArrayRef ) ];
120             push @EXPORT_OK, @{ $EXPORT_TAGS{"ArrayRef"} };
121             push @{ $EXPORT_TAGS{"types"} }, "ArrayRef";
122             push @{ $EXPORT_TAGS{"is"} }, "is_ArrayRef";
123             push @{ $EXPORT_TAGS{"assert"} }, "assert_ArrayRef";
124              
125             }
126              
127             # Datastar
128             {
129             my $type;
130             sub Datastar () {
131 0   0 0 0 0 $type ||= bless( { check => \&is_Datastar, name => "Datastar", library => 0, library_name => "Datastar" }, "Datastar::SSE::Types::TypeConstraint" );
132             }
133              
134             sub is_Datastar ($) {
135 30 50 33 30 0 52 do { (defined($_[0]) and !ref($_[0]) and $_[0] =~ m{\A(?:datastar\-(?:execute\-script|merge\-(?:fragments|signals)|remove\-(?:fragments|signals)))\z}) }
  30         370  
136             }
137              
138             sub assert_Datastar ($) {
139 0 0 0 0 0 0 do { (defined($_[0]) and !ref($_[0]) and $_[0] =~ m{\A(?:datastar\-(?:execute\-script|merge\-(?:fragments|signals)|remove\-(?:fragments|signals)))\z}) } ? $_[0] : Datastar->get_message( $_[0] );
  0 0       0  
140             }
141              
142             $EXPORT_TAGS{"Datastar"} = [ qw( Datastar is_Datastar assert_Datastar ) ];
143             push @EXPORT_OK, @{ $EXPORT_TAGS{"Datastar"} };
144             push @{ $EXPORT_TAGS{"types"} }, "Datastar";
145             push @{ $EXPORT_TAGS{"is"} }, "is_Datastar";
146             push @{ $EXPORT_TAGS{"assert"} }, "assert_Datastar";
147              
148             }
149              
150             # HashRef
151             {
152             my $type;
153             sub HashRef () {
154 0   0 0 0 0 $type ||= bless( { check => \&is_HashRef, name => "HashRef", library => "Types::Standard", library_name => "HashRef" }, "Datastar::SSE::Types::TypeConstraint" );
155             }
156              
157             sub is_HashRef ($) {
158 43     43 0 201 (ref($_[0]) eq 'HASH')
159             }
160              
161             sub assert_HashRef ($) {
162 0 0   0 0 0 (ref($_[0]) eq 'HASH') ? $_[0] : HashRef->get_message( $_[0] );
163             }
164              
165             $EXPORT_TAGS{"HashRef"} = [ qw( HashRef is_HashRef assert_HashRef ) ];
166             push @EXPORT_OK, @{ $EXPORT_TAGS{"HashRef"} };
167             push @{ $EXPORT_TAGS{"types"} }, "HashRef";
168             push @{ $EXPORT_TAGS{"is"} }, "is_HashRef";
169             push @{ $EXPORT_TAGS{"assert"} }, "assert_HashRef";
170              
171             }
172              
173             # Mergemode
174             {
175             my $type;
176             sub Mergemode () {
177 0   0 0 0 0 $type ||= bless( { check => \&is_Mergemode, name => "Mergemode", library => 0, library_name => "Mergemode" }, "Datastar::SSE::Types::TypeConstraint" );
178             }
179              
180             sub is_Mergemode ($) {
181 3 50 33 3 0 8 do { (defined($_[0]) and !ref($_[0]) and $_[0] =~ m{\A(?:(?:a(?:fter|ppend)|before|inner|morph|outer|prepend|upsertAttributes))\z}) }
  3         50  
182             }
183              
184             sub assert_Mergemode ($) {
185 0 0 0 0 0 0 do { (defined($_[0]) and !ref($_[0]) and $_[0] =~ m{\A(?:(?:a(?:fter|ppend)|before|inner|morph|outer|prepend|upsertAttributes))\z}) } ? $_[0] : Mergemode->get_message( $_[0] );
  0 0       0  
186             }
187              
188             $EXPORT_TAGS{"Mergemode"} = [ qw( Mergemode is_Mergemode assert_Mergemode ) ];
189             push @EXPORT_OK, @{ $EXPORT_TAGS{"Mergemode"} };
190             push @{ $EXPORT_TAGS{"types"} }, "Mergemode";
191             push @{ $EXPORT_TAGS{"is"} }, "is_Mergemode";
192             push @{ $EXPORT_TAGS{"assert"} }, "assert_Mergemode";
193              
194             }
195              
196             # ScalarRef
197             {
198             my $type;
199             sub ScalarRef () {
200 0   0 0 0 0 $type ||= bless( { check => \&is_ScalarRef, name => "ScalarRef", library => "Types::Standard", library_name => "ScalarRef" }, "Datastar::SSE::Types::TypeConstraint" );
201             }
202              
203             sub is_ScalarRef ($) {
204 30 100   30 0 164 (ref($_[0]) eq 'SCALAR' or ref($_[0]) eq 'REF')
205             }
206              
207             sub assert_ScalarRef ($) {
208 0 0 0 0 0   (ref($_[0]) eq 'SCALAR' or ref($_[0]) eq 'REF') ? $_[0] : ScalarRef->get_message( $_[0] );
209             }
210              
211             $EXPORT_TAGS{"ScalarRef"} = [ qw( ScalarRef is_ScalarRef assert_ScalarRef ) ];
212             push @EXPORT_OK, @{ $EXPORT_TAGS{"ScalarRef"} };
213             push @{ $EXPORT_TAGS{"types"} }, "ScalarRef";
214             push @{ $EXPORT_TAGS{"is"} }, "is_ScalarRef";
215             push @{ $EXPORT_TAGS{"assert"} }, "assert_ScalarRef";
216              
217             }
218              
219              
220             1;