Dave Rolsky > Fey-ORM-0.44 > Fey::ORM::Policy



Annotate this POD


Open  0
Stalled  1
View/Report Bugs
Module Version: 0.44   Source   Latest Release: Fey-ORM-0.46


Fey::ORM::Policy - Declarative policies for Fey::ORM using classes


version 0.44


  package MyApp::Policy;

  use strict;
  use warnings;

  use Fey::ORM::Policy;
  use Lingua::EN::Inflect qw( PL_N );

         matching { $_[0]->type() eq 'date' }

      => inflate  { return unless defined $_[1];
                    return DateTime::Format::Pg->parse_date( $_[1] ) }

      => deflate  { defined $_[1] && ref $_[1]
                      ? DateTime::Format::Pg->format_date( $_[1] )
                      : $_[1] };

         matching { $_[0]->name() eq 'email_address' }

      => inflate  { return unless defined $_[1];
                    return Email::Address->parse( $_[1] ) }

      => deflate  { defined $_[1] && ref $_[1]
                      ? Email::Address->as_string
                      : $_[1] };

  has_one_namer  { my $name = $_[0]->name();
                   my @parts = map { lc } ( $name =~ /([A-Z][a-z]+)/g );

                   return join q{_}, @parts; };

  has_many_namer { my $name = $_[0]->name();
                   my @parts = map { lc } ( $name =~ /([A-Z][a-z]+)/g );

                   $parts[-1] = PL_N( $parts[-1] );

                   return join q{_}, @parts; };

  package User;

  use Fey::ORM::Table;

  has_policy 'MyApp::Policy';

  has_table ...;


This module allows you to declare a policy for your Fey::ORM::Table-using classes.

A policy can define transform rules which can be applied to matching columns, as well as a naming scheme for has_one and has_many methods. This allows you to spare yourself some drudgery, and allows you to consolidate decisions (like "all date type columns return a DateTime object") in a single place.


This module exports a bunch of sugar functions into your namespace so you can define your policy in a declarative manner:


This should be followed by a matching sub reference, and one of an inflate or deflate sub.

matching { ... }

This function takes a subroutine reference that will be called and passed a Fey::Column object as its argument. This sub should look at the column and return true if the associated inflate/deflate should be applied to the column.

Note that the matching subs are checked in the order they are defined by transform_all(), and the first one wins.

inflate { ... }

An inflator sub for the associated transform. See Fey::ORM::Table for more details on transforms.

deflate { ... }

A deflator sub for the associated transform. See Fey::ORM::Table for more details on transforms.

has_one_namer { ... }

A subroutine reference which will be used to generate a name for has_one() methods when a name is not explicitly provided.

This sub will receive the foreign table as its first argument, and the associate Fey::Meta::FK object as the second argument. In most cases, the foreign table will probably be sufficient to generate a name.

has_many_namer { ... }

Just like the has_one_namer(), but is called for naming has_many() methods.


This methods returns the Fey::Object::Policy object for your policy class. This method allows Fey::ORM::Table to go get a policy object from a policy class name.


Dave Rolsky <autarch@urth.org>


This software is copyright (c) 2011 by Dave Rolsky.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: