File Coverage

blib/lib/Statocles/Types.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 0 1 0.0
total 32 33 96.9


line stmt bran cond sub pod time code
1             package Statocles::Types;
2             our $VERSION = '0.085';
3             # ABSTRACT: Type constraints and coercions for Statocles
4              
5 68     68   44386 use strict;
  68         178  
  68         1987  
6 68     68   336 use warnings;
  68         120  
  68         1915  
7 68     68   327 use feature qw( :5.10 );
  68         126  
  68         7132  
8 68         705 use Type::Library -base, -declare => qw(
9             Store Theme Link LinkArray LinkHash LinkTree LinkTreeArray
10             DateTimeObj DateStr DateTimeStr
11             Person
12 68     68   19948 );
  68         1144013  
13 68     68   118014 use Type::Utils -all;
  68         250998  
  68         596  
14 68     68   219552 use Types::Standard -types;
  68         2735926  
  68         810  
15 68     68   278749 use DateTime::Moonpig;
  68         164  
  68         68056  
16              
17             role_type Store, { role => "Statocles::Store" };
18             coerce Store, from Str, via { Statocles::Store->new( path => $_ ) };
19             coerce Store, from InstanceOf['Path::Tiny'], via { Statocles::Store->new( path => $_ ) };
20              
21             class_type Theme, { class => "Statocles::Theme" };
22             coerce Theme, from Str, via { require Statocles::Theme; Statocles::Theme->new( store => $_ ) };
23             coerce Theme, from InstanceOf['Path::Tiny'], via { require Statocles::Theme; Statocles::Theme->new( store => $_ ) };
24              
25             class_type Link, { class => "Statocles::Link" };
26             coerce Link, from HashRef, via { Statocles::Link->new( $_ ) };
27             coerce Link, from Str, via { Statocles::Link->new( href => $_ ) };
28              
29             class_type LinkTree, { class => "Statocles::Link::Tree" };
30             coerce LinkTree, from HashRef, via { Statocles::Link::Tree->new( $_ ) };
31             coerce LinkTree, from Str, via { Statocles::Link::Tree->new( href => $_ ) };
32              
33             class_type Person, { class => 'Statocles::Person' };
34             coerce Person, from HashRef, via { Statocles::Person->new( $_ ) };
35             coerce Person, from Str, via { Statocles::Person->new( $_ ) };
36              
37             declare LinkArray, as ArrayRef[Link], coerce => 1;
38             coerce LinkArray, from ArrayRef[HashRef|Str],
39             via {
40             [ map { Link->coerce( $_ ) } @$_ ];
41             };
42              
43             declare LinkHash, as HashRef[LinkArray], coerce => 1;
44             # We need the parens in this "from" type union to fix an issue in Perl
45             # < 5.14 and Type::Tiny 1.000005
46             # (https://github.com/tobyink/p5-type-tiny/issues/29)
47             coerce LinkHash, from HashRef[(ArrayRef[HashRef|Str]|HashRef)|Str],
48             via {
49             my %hash = %$_;
50             my $out = {
51             ( map {; $_ => LinkArray->coerce( ref $hash{ $_ } ne 'ARRAY' ? [ $hash{ $_ } ] : $hash{ $_ } ) } keys %hash ),
52             };
53             return $out;
54             };
55              
56             declare LinkTreeArray, as ArrayRef[LinkTree], coerce => 1;
57             coerce LinkTreeArray, from ArrayRef[HashRef|Str],
58             via {
59             [ map { LinkTree->coerce( $_ ) } @$_ ];
60             };
61              
62             class_type DateTimeObj, { class => 'DateTime::Moonpig' };
63             declare DateStr, as Str, where { m{^\d{4}-?\d{2}-?\d{2}$} };
64             declare DateTimeStr, as Str, where { m{^\d{4}-?\d{2}-?\d{2}[T ]\d{2}:?\d{2}:?\d{2}} };
65             coerce DateTimeObj, from DateStr, via {
66             my ( $y, $m, $d ) = $_ =~ /^(\d{4})[-]?(\d{2})[-]?(\d{2})$/;
67             DateTime::Moonpig->new(
68             year => $y,
69             month => $m,
70             day => $d,
71             );
72             };
73             coerce DateTimeObj, from DateTimeStr, via {
74             my ( $y, $m, $d, $h, $n, $s ) = $_ =~ /^(\d{4})[-]?(\d{2})[-]?(\d{2})[T ]?(\d{2}):?(\d{2}):?(\d{2})/;
75             DateTime::Moonpig->new(
76             year => $y,
77             month => $m,
78             day => $d,
79             hour => $h,
80             minute => $n,
81             second => $s,
82             );
83             };
84              
85             sub DateTime::Moonpig::tzoffset {
86 1     1 0 820 my ( $self ) = @_;
87 1         11 warn "The tzoffset shim method will be removed in Statocles version 2.0. See Statocles::Help::Upgrading for instructions to remove this warning.\n";
88 1         9 return $self->offset * 100;
89             }
90              
91             # Down here to resolve circular dependencies
92             require Statocles::Store;
93             require Statocles::Link;
94             require Statocles::Link::Tree;
95             require Statocles::Person;
96              
97             1;
98              
99             __END__
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =head1 NAME
106              
107             Statocles::Types - Type constraints and coercions for Statocles
108              
109             =head1 VERSION
110              
111             version 0.085
112              
113             =head1 SYNOPSIS
114              
115             use Statocles::Class;
116             use Statocles::Types qw( :all );
117              
118             has store => (
119             isa => Store,
120             coerce => Store->coercion,
121             );
122              
123             has theme => (
124             isa => Theme,
125             coerce => Theme->coercion,
126             );
127              
128             has link => (
129             isa => Link,
130             coerce => Link->coercion,
131             );
132             has links => (
133             isa => LinkArray,
134             coerce => LinkArray->coercion,
135             );
136             has nav => (
137             isa => LinkHash,
138             coerce => LinkHash->coercion,
139             );
140              
141             has date => (
142             isa => DateTimeObj,
143             coerce => DateTimeObj->coercion,
144             );
145              
146             =head1 DESCRIPTION
147              
148             This is a L<type library|Type::Tiny::Manual::Library> for common Statocles types.
149              
150             =head1 TYPES
151              
152             =head2 Store
153              
154             A L<Statocles::Store> object.
155              
156             This can be coerced from any L<Path::Tiny> object or any String, which will be
157             used as the filesystem path to the store's documents (the L<path
158             attribute|Statocles::Store/path>). The coercion creates a
159             L<Statocles::Store> object.
160              
161             =head2 Theme
162              
163             A L<Statocles::Theme> object.
164              
165             This can be coerced from any L<Path::Tiny> object or any String, which will be
166             used as the L<store attribute|Statocles::Theme/store> (which will then be given
167             to the Store's path attribute).
168              
169             =head2 Link
170              
171             A L<Statocles::Link> object.
172              
173             This can be coerced from any HashRef.
174              
175             =head2 LinkArray
176              
177             An arrayref of L<Statocles::Link> objects.
178              
179             This can be coerced from any ArrayRef of HashRefs.
180              
181             =head2 LinkHash
182              
183             A hashref of arrayrefs of L<Statocles::Link> objects. Useful for the named links like
184             L<site navigation|Statocles::Site/nav>.
185              
186             This can be coerced from any HashRef of ArrayRef of HashRefs.
187              
188             =head2 DateTimeObj
189              
190             A L<DateTime::Moonpig> object representing a date/time. This can be coerced from a
191             C<YYYY-MM-DD> string or a C<YYYY-MM-DD HH:MM:SS> string.
192              
193             =head1 AUTHOR
194              
195             Doug Bell <preaction@cpan.org>
196              
197             =head1 COPYRIGHT AND LICENSE
198              
199             This software is copyright (c) 2016 by Doug Bell.
200              
201             This is free software; you can redistribute it and/or modify it under
202             the same terms as the Perl 5 programming language system itself.
203              
204             =cut