aboutsummaryrefslogtreecommitdiff
path: root/lib/Sietima/Types.pm
blob: c6c73817d7da30882edf48d0927d0bd33201693b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package Sietima::Types; 
use Sietima::Policy;
use Type::Utils -all;
use Types::Standard qw(Str HashRef Defined Str);
use namespace::clean;
use Type::Library
    -base,
    -declare => qw(SietimaObj
                   Address AddressFromStr
                   TagName
                   EmailMIME Message
                   HeaderUri HeaderUriFromThings
                   Subscriber SubscriberFromAddress SubscriberFromStr SubscriberFromHashRef
                   Transport MailStore MailStoreFromHashRef);
 
# VERSION 
# ABSTRACT: type library for Sietima 
 
=head1 DESCRIPTION
 
This module is a L<< C<Type::Library> >>. It declares a few type
constraints nad coercions.
 
=type C<SietimaObj>
 
An instance of L<< C<Sietima> >>.
 
=cut
 
class_type SietimaObj, { class => 'Sietima' };
 
=type C<EmailMIME>
 
An instance of L<< C<Email::MIME> >>.
 
=cut
 
class_type EmailMIME, { class => 'Email::MIME' };
 
=type C<Transport>
 
An object that consumes the role L<< C<Email::Sender::Transport> >>.
 
=cut
 
role_type Transport, { role => 'Email::Sender::Transport' };
 
=type C<MailStore>
 
An object that consumes the role L<< C<Sietima::MailStore> >>.
 
Coercions:
 
=over
 
=item C<MailStoreFromHashRef>
 
  has store => ( isa => MailStore->plus_coercions(MailStoreFromHashRef) );
 
Using this coercion, a hashref of the form:
 
  {
    class => 'Some::Store::Class',
    %constructor_args,
  }
 
will be converted into an instance of C<Some::Store::Class> built with
the C<%constructor_args>.
 
=back
 
=cut
 
role_type MailStore, { role => 'Sietima::MailStore' };
 
declare_coercion MailStoreFromHashRef,
    to_type MailStore, from HashRef,
    q{ require Module::Runtime; } .
    q{ Module::Runtime::use_module(delete $_->{class})->new($_); };
 
=type C<Address>
 
An instance of L<< C<Email::Address> >>.
 
Coercions:
 
=over
 
=item C<AddressFromStr>
 
  has address => ( isa => Address->plus_coercions(AddressFromStr) );
 
Using this coercion, a string will be parsed into an L<<
C<Email::Address> >>. If the string contains more than one address,
only the first one will be used.
 
=back
 
=cut
 
class_type Address, { class => 'Email::Address' };
declare_coercion AddressFromStr,
    to_type Address, from Str,
    q{ (Email::Address->parse($_))[0] };
 
=type C<TagName>
 
A string composed exclusively of "word" (C</\w/>) characters. Used by
L<mail stores|Sietima::MailStore> to tag messages.
 
=cut
 
declare TagName, as Str,
    where { /\A\w+\z/ },
    inline_as sub($constraint,$varname,@){
        $constraint->parent->inline_check($varname)
            .qq{ && ($varname =~/\\A\\w+\\z/) };
    };
 
=type C<Message>
 
An instance of L<< C<Sietima::Message> >>.
 
=cut
 
class_type Message, { class => 'Sietima::Message' };
 
class_type HeaderUri, { class => 'Sietima::HeaderURI' };
 
declare_coercion HeaderUriFromThings,
    to_type HeaderUri, from Defined,
q{ Sietima::HeaderURI->new($_) };
 
=type C<Subscriber>
 
An instance of L<< C<Sietima::Subscriber> >>.
 
Coercions:
 
=over
 
=item C<SubscriberFromAddress>
 
  has sub => ( isa => Subscriber->plus_coercions(SubscriberFromAddress) );
 
Using this coercion, an L<< C<Email::Address> >> will be converted
into a subscriber that has that address as its primary.
 
=item C<SubscriberFromStr>
 
  has sub => ( isa => Subscriber->plus_coercions(SubscriberFromStr) );
 
Using this coercion, a string will be converted into a subscriber that
has the first address parsed from that string as its primary.
 
=item C<SubscriberFromHashRef>
 
  has sub => ( isa => Subscriber->plus_coercions(SubscriberFromHashRef) );
 
Using this coercion, a hashref will be converted into a subscriber by
passing it to the constructor.
 
=back
 
=cut
 
class_type Subscriber, { class => 'Sietima::Subscriber' };
 
declare_coercion SubscriberFromAddress,
    to_type Subscriber, from Address,
    q{ Sietima::Subscriber->new(primary=>$_) };
 
declare_coercion SubscriberFromStr,
    to_type Subscriber, from Str,
    q{ Sietima::Subscriber->new(primary=>(Email::Address->parse($_))[0]) };
 
declare_coercion SubscriberFromHashRef,
    to_type Subscriber, from HashRef,
    q{ Sietima::Subscriber->new($_) };
 
1;