| 1 | |||||||||||||||
| 2 | =head1 NAME | ||||||||||||||
| 3 | |||||||||||||||
| 4 | Util::DataThing - A simple framework for creating introspectable business objects | ||||||||||||||
| 5 | |||||||||||||||
| 6 | =head1 SYNOPSIS | ||||||||||||||
| 7 | |||||||||||||||
| 8 | package ExampleApp::Person; | ||||||||||||||
| 9 | use base qw(Util::DataThing); | ||||||||||||||
| 10 | |||||||||||||||
| 11 | __PACKAGE__->declare_property('name', STRING); | ||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||
| 14 | |||||||||||||||
| 15 | This class provides a framework for providing business objects | ||||||||||||||
| 16 | within an application. | ||||||||||||||
| 17 | |||||||||||||||
| 18 | Subclasses of this class support introspection via the | ||||||||||||||
| 19 | C<type> method, which returns a L<Util::DataThing::Type> object | ||||||||||||||
| 20 | describing the class. | ||||||||||||||
| 21 | |||||||||||||||
| 22 | The internal structure of the objects is opaque to subclasses | ||||||||||||||
| 23 | and should not be relied upon. Subclasses may define new methods | ||||||||||||||
| 24 | in addition to those created with this class's C<register_> | ||||||||||||||
| 25 | methods, but they should be implemented in terms of the | ||||||||||||||
| 26 | property accessors created by this class, not direct access | ||||||||||||||
| 27 | to the object internals. | ||||||||||||||
| 28 | |||||||||||||||
| 29 | This is not an ORM or other mechanism for abstracting away | ||||||||||||||
| 30 | your data access layer, but if you want to use an ORM then | ||||||||||||||
| 31 | objects created with this class may be a good thing for your | ||||||||||||||
| 32 | ORM to return. | ||||||||||||||
| 33 | |||||||||||||||
| 34 | TODO: Write more docs | ||||||||||||||
| 35 | |||||||||||||||
| 36 | =cut | ||||||||||||||
| 37 | |||||||||||||||
| 38 | package Util::DataThing; | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | 4 | ▶ | use Util::DataThing::Type; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | 4 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | 4 | ▶ | use Sub::Name; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | 4 | our $VERSION = "0.01_01"; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 4 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 49 | 4 | my $class = shift; | |||||||||||||
| 50 | |||||||||||||||
| 51 | 4 | ▶ | my $args = (scalar(@_) == 1 ? $_[0] : {@_}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 4 | my $self = bless {}, $class; | |||||||||||||
| 54 | 4 | $self->{data} = {}; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 4 | foreach my $property (keys %$args) { | |||||||||||||
| 57 | 4 | $self->$property($args->{$property}); | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 4 | return $self; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 11 | ▶ | sub register_property { | ||||||||||||
|
Subroutine
register_property: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 64 | 11 | my ($class, $name, $type) = @_; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 11 | my ($existing_type, $existing_class) = $class->type->property_type($name); | |||||||||||||
| 67 | 11 | ▶ | croak("$class tried to override property ${existing_class}->$name") if $existing_type; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | |||||||||||||||
| 69 | # Create an accessor for this field | ||||||||||||||
| 70 | { | ||||||||||||||
| 71 | 4 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 72 | 10 | my $full_name = "${class}::${name}"; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 10 | my $coerce_in = $type->coerce_in; | |||||||||||||
| 75 | 10 | my $coerce_out = $type->coerce_out; | |||||||||||||
| 76 | |||||||||||||||
| 77 | 21 | ▶ | my $method = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
Subroutine
some_boolean: called CC=5 SCAR=16.2
| |||||||||||||||
| 78 | 21 | my $self = shift; | |||||||||||||
| 79 | |||||||||||||||
| 80 | 21 | ▶ | if (@_) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 81 | 5 | my $value = shift; | |||||||||||||
| 82 | 5 | ▶ | croak("Unexpected extra arguments to ${class}->${name}") if @_; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 83 | 5 | ▶ | return $self->{data}{$name} = defined($value) ? $coerce_in->($value, $type) : undef; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 84 | } | ||||||||||||||
| 85 | else { | ||||||||||||||
| 86 | 16 | my $value = $self->{data}{$name}; | |||||||||||||
| 87 | 16 | ▶ | return defined($value) ? $coerce_out->($value, $type) : undef; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 88 | } | ||||||||||||||
| 89 | 10 | }; | |||||||||||||
| 90 | |||||||||||||||
| 91 | 10 | Sub::Name::subname($full_name, $method); | |||||||||||||
| 92 | 10 | *{$full_name} = $method; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | # Tell Util::DataThing::Type about this field using | ||||||||||||||
| 96 | # our super-secret backdoor! | ||||||||||||||
| 97 | 10 | Util::DataThing::Type->_register_object_property($class, $name, $type); | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 13 | ▶ | sub type { | ||||||||||||
|
Subroutine
type: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 101 | 13 | my ($thing) = @_; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 13 | ▶ | my $class = ref($thing) ? ref($thing) : $thing; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 13 | return Util::DataThing::Type->object($class); | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 4 | 1; | |||||||||||||
| 108 | |||||||||||||||
| 109 | =head1 AUTHOR AND COPYRIGHT | ||||||||||||||
| 110 | |||||||||||||||
| 111 | Written and maintained by Martin Atkins <mart@degeneration.co.uk>. | ||||||||||||||
| 112 | |||||||||||||||
| 113 | Copyright 2009 Six Apart Ltd. All Rights Reserved. | ||||||||||||||
| 114 | |||||||||||||||
| 115 | The items in this distribution may be distributed under the same | ||||||||||||||
| 116 | terms as Perl itself. | ||||||||||||||
| 117 | |||||||||||||||
| 118 | |||||||||||||||