| 1 | package Fey::ORM::Policy; | ||||||||||||||||||||
| 2 | { | ||||||||||||||||||||
| 3 | 1 | $Fey::ORM::Policy::VERSION = '0.46'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use Fey::Object::Policy; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | { | ||||||||||||||||||||
| 12 | 1 | my @subs; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | BEGIN { | ||||||||||||||||||||
| 15 | 1 | ▶ | @subs = qw( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | Policy | ||||||||||||||||||||
| 17 | transform_all | ||||||||||||||||||||
| 18 | matching | ||||||||||||||||||||
| 19 | inflate | ||||||||||||||||||||
| 20 | deflate | ||||||||||||||||||||
| 21 | has_one_namer | ||||||||||||||||||||
| 22 | has_many_namer | ||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 1 | use Sub::Exporter -setup => { | |||||||||||||||||||
| 27 | exports => \@subs, | ||||||||||||||||||||
| 28 | groups => { default => \@subs }, | ||||||||||||||||||||
| 29 | 1 | ▶ | }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # I could use MooseX::ClassAttribute and add a class attribute to the | ||||||||||||||||||||
| 33 | # calling class, but really, that class doesn't need to use Moose, | ||||||||||||||||||||
| 34 | # since it's just a name we can use to find the associated policy | ||||||||||||||||||||
| 35 | # object. | ||||||||||||||||||||
| 36 | { | ||||||||||||||||||||
| 37 | 1 | my %Policies; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | sub Policy { | ||||||||||||||||||||
| 40 | 8 | ▶ | my $caller = shift; | ||||||||||||||||||
|
Subroutine
Policy: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 8 | ▶ | return $Policies{$caller} ||= Fey::Object::Policy->new(); | ||||||||||||||||||
|
Condition66%2/3
$Policies{$caller} ||= "Fey::Object::Policy"->new
MC/DC0%0/2
$Policies{$caller} ||= "Fey::Object::Policy"->new
$Policies{$caller}
"Fey::Object::Policy"->new
Truth table66%2/3
A ||= B
A= $Policies{$caller}B= "Fey::Object::Policy"->new
| |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 1 | sub transform_all { | |||||||||||||||||||
| 47 | 1 | ▶ | my $class = caller(); | ||||||||||||||||||
|
Subroutine
transform_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 1 | $class->Policy()->add_transform( {@_} ); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | sub matching (&) { | ||||||||||||||||||||
| 53 | 1 | ▶ | return ( matching => $_[0] ); | ||||||||||||||||||
|
Subroutine
matching: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | sub inflate (&) { | ||||||||||||||||||||
| 57 | 1 | ▶ | return ( inflate => $_[0] ); | ||||||||||||||||||
|
Subroutine
inflate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub deflate (&) { | ||||||||||||||||||||
| 61 | 1 | ▶ | return ( deflate => $_[0] ); | ||||||||||||||||||
|
Subroutine
deflate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | sub has_one_namer (&) { | ||||||||||||||||||||
| 65 | 1 | ▶ | my $class = caller(); | ||||||||||||||||||
|
Subroutine
has_one_namer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 1 | $class->Policy()->set_has_one_namer( $_[0] ); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | sub has_many_namer (&) { | ||||||||||||||||||||
| 71 | 1 | ▶ | my $class = caller(); | ||||||||||||||||||
|
Subroutine
has_many_namer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 1 | $class->Policy()->set_has_many_namer( $_[0] ); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 1 | 1; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # ABSTRACT: Declarative policies for Fey::ORM using classes | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =pod | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head1 NAME | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | Fey::ORM::Policy - Declarative policies for Fey::ORM using classes | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head1 VERSION | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | version 0.46 | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head1 SYNOPSIS | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | package MyApp::Policy; | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | use strict; | ||||||||||||||||||||
| 97 | use warnings; | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | use Fey::ORM::Policy; | ||||||||||||||||||||
| 100 | use Lingua::EN::Inflect qw( PL_N ); | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | transform_all | ||||||||||||||||||||
| 103 | matching { $_[0]->type() eq 'date' } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | => inflate { return unless defined $_[1]; | ||||||||||||||||||||
| 106 | return DateTime::Format::Pg->parse_date( $_[1] ) } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | => deflate { defined $_[1] && ref $_[1] | ||||||||||||||||||||
| 109 | ? DateTime::Format::Pg->format_date( $_[1] ) | ||||||||||||||||||||
| 110 | : $_[1] }; | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | transform_all | ||||||||||||||||||||
| 113 | matching { $_[0]->name() eq 'email_address' } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | => inflate { return unless defined $_[1]; | ||||||||||||||||||||
| 116 | return Email::Address->parse( $_[1] ) } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | => deflate { defined $_[1] && ref $_[1] | ||||||||||||||||||||
| 119 | ? Email::Address->as_string | ||||||||||||||||||||
| 120 | : $_[1] }; | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | has_one_namer { my $name = $_[0]->name(); | ||||||||||||||||||||
| 123 | my @parts = map { lc } ( $name =~ /([A-Z][a-z]+)/g ); | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | return join q{_}, @parts; }; | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | has_many_namer { my $name = $_[0]->name(); | ||||||||||||||||||||
| 128 | my @parts = map { lc } ( $name =~ /([A-Z][a-z]+)/g ); | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | $parts[-1] = PL_N( $parts[-1] ); | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | return join q{_}, @parts; }; | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | package User; | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | use Fey::ORM::Table; | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | has_policy 'MyApp::Policy'; | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | has_table ...; | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =head1 DESCRIPTION | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | This module allows you to declare a policy for your | ||||||||||||||||||||
| 145 | L<Fey::ORM::Table>-using classes. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | A policy can define transform rules which can be applied to matching | ||||||||||||||||||||
| 148 | columns, as well as a naming scheme for has_one and has_many | ||||||||||||||||||||
| 149 | methods. This allows you to spare yourself some drudgery, and allows | ||||||||||||||||||||
| 150 | you to consolidate decisions (like "all date type columns return a | ||||||||||||||||||||
| 151 | C<DateTime> object") in a single place. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =head1 FUNCTIONS | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | This module exports a bunch of sugar functions into your namespace so | ||||||||||||||||||||
| 156 | you can define your policy in a declarative manner: | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head2 transform_all | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | This should be followed by a C<matching> sub reference, and one of an | ||||||||||||||||||||
| 161 | C<inflate> or C<deflate> sub. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =head2 matching { ... } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | This function takes a subroutine reference that will be called and | ||||||||||||||||||||
| 166 | passed a L<Fey::Column> object as its argument. This sub should look | ||||||||||||||||||||
| 167 | at the column and return true if the associated inflate/deflate should | ||||||||||||||||||||
| 168 | be applied to the column. | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | Note that the matching subs are checked in the order they are defined | ||||||||||||||||||||
| 171 | by C<transform_all()>, and the first one wins. | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =head2 inflate { ... } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | An inflator sub for the associated transform. See L<Fey::ORM::Table> | ||||||||||||||||||||
| 176 | for more details on transforms. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head2 deflate { ... } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | A deflator sub for the associated transform. See L<Fey::ORM::Table> | ||||||||||||||||||||
| 181 | for more details on transforms. | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | =head2 has_one_namer { ... } | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | A subroutine reference which will be used to generate a name for | ||||||||||||||||||||
| 186 | C<has_one()> methods when a name is not explicitly provided. | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | This sub will receive the foreign table as its first argument, and the | ||||||||||||||||||||
| 189 | associated FK object as the second argument. In most cases, the foreign table | ||||||||||||||||||||
| 190 | will probably be sufficient to generate a name. | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head2 has_many_namer { ... } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | Just like the C<has_one_namer()>, but is called for naming | ||||||||||||||||||||
| 195 | C<has_many()> methods. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =head2 Policy | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | This methods returns the L<Fey::Object::Policy> object for your policy | ||||||||||||||||||||
| 200 | class. This method allows L<Fey::ORM::Table> to go get a policy object | ||||||||||||||||||||
| 201 | from a policy class name. | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =head1 AUTHOR | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | This software is copyright (c) 2011 by Dave Rolsky. | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 212 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||