| 1 | package DBIx::Class::Objects; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 5 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 5 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 5 | ▶ | use Moose::Util qw( apply_all_roles ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | # using this because it will be applied to result classes, not because we want | ||||||||||||||
| 8 | # to import this behavior | ||||||||||||||
| 9 | 5 | ▶ | use DBIx::Class::Objects::Role::Result (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 5 | ▶ | use Class::Load 'try_load_class'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 5 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 5 | our $VERSION = '0.04'; | |||||||||||||
| 15 | |||||||||||||||
| 16 | 5 | has 'schema' => ( | |||||||||||||
| 17 | is => 'ro', | ||||||||||||||
| 18 | isa => 'DBIx::Class::Schema', | ||||||||||||||
| 19 | required => 1, | ||||||||||||||
| 20 | ); | ||||||||||||||
| 21 | |||||||||||||||
| 22 | 5 | has 'debug' => ( | |||||||||||||
| 23 | is => 'rw', | ||||||||||||||
| 24 | isa => 'Bool', | ||||||||||||||
| 25 | default => 0, | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 5 | has 'base_class' => ( | |||||||||||||
| 29 | is => 'ro', | ||||||||||||||
| 30 | isa => 'Str', | ||||||||||||||
| 31 | required => 1, | ||||||||||||||
| 32 | default => 'DBIx::Class::Objects::Base', | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 5 | has 'result_role' => ( | |||||||||||||
| 36 | is => 'ro', | ||||||||||||||
| 37 | isa => 'Str', | ||||||||||||||
| 38 | required => 1, | ||||||||||||||
| 39 | default => 'DBIx::Class::Objects::Role::Result', | ||||||||||||||
| 40 | ); | ||||||||||||||
| 41 | |||||||||||||||
| 42 | has roles => ( | ||||||||||||||
| 43 | is => 'ro', | ||||||||||||||
| 44 | isa => 'ArrayRef[Str]', | ||||||||||||||
| 45 | required => 0, | ||||||||||||||
| 46 | 3 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | 5 | ); | |||||||||||||
| 48 | |||||||||||||||
| 49 | 5 | has 'object_base' => ( | |||||||||||||
| 50 | is => 'rw', | ||||||||||||||
| 51 | isa => 'Str', | ||||||||||||||
| 52 | required => 0, # XXX Fix this later | ||||||||||||||
| 53 | writer => '_set_object_base', | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | |||||||||||||||
| 56 | around 'BUILDARGS' => sub { | ||||||||||||||
| 57 | 4 | ▶ | my $orig = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||
| 58 | 4 | my $self = shift; | |||||||||||||
| 59 | 4 | ▶ | my %args = ref $_[0] ? %{$_[0]} : @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 4 | $args{object_base} =~ s/::$//; | |||||||||||||
| 61 | 4 | $self->$orig(\%args); | |||||||||||||
| 62 | 5 | }; | |||||||||||||
| 63 | |||||||||||||||
| 64 | sub _get_object_class_name { | ||||||||||||||
| 65 | 86 | ▶ | my ( $self, $source_name ) = @_; | ||||||||||||
|
Subroutine
_get_object_class_name: called CC=1 SCAR=0.0
| |||||||||||||||
| 66 | 86 | return $self->object_base . '::' . $source_name; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | sub objectset { | ||||||||||||||
| 70 | 13 | ▶ | my ( $self, $source_name ) = @_; | ||||||||||||
|
Subroutine
objectset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 71 | |||||||||||||||
| 72 | 13 | return $self->_create_object_set( | |||||||||||||
| 73 | $self->schema->resultset($source_name) ); | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | sub _create_object_set { | ||||||||||||||
| 77 | |||||||||||||||
| 78 | # this method allows the user to ensure that resultsets contain instances | ||||||||||||||
| 79 | # of DBIx::Class::Objects instead of DBIx::Class::Result | ||||||||||||||
| 80 | 15 | ▶ | my ( $self, $resultset ) = @_; | ||||||||||||
|
Subroutine
_create_object_set: called CC=2 SCAR=7.0
| |||||||||||||||
| 81 | |||||||||||||||
| 82 | 15 | my %methods; | |||||||||||||
| 83 | 15 | foreach my $method (qw/next create single/) { | |||||||||||||
| 84 | |||||||||||||||
| 85 | # Haven't debugged this, but simply declaring a single subroutine and | ||||||||||||||
| 86 | # assigning it to the keys doesn't work. You get errors like this: | ||||||||||||||
| 87 | # | ||||||||||||||
| 88 | # DBIx::Class::ResultSet::find(): find() expects either a column/value | ||||||||||||||
| 89 | # hashref, or a list of values corresponding to the columns of the | ||||||||||||||
| 90 | # specified unique constraint 'primary' at t/resultset.t line 30 | ||||||||||||||
| 91 | # | ||||||||||||||
| 92 | # Doing it this way results in a fresh copy of the subref for every | ||||||||||||||
| 93 | # method at a slight performance cost. Caching bug? | ||||||||||||||
| 94 | $methods{$method} = sub { | ||||||||||||||
| 95 | 11 | ▶ | my $this = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.0
| |||||||||||||||
| 96 | 11 | ▶ | my $result = $this->next::method(@_) or return; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 97 | 9 | my $source_name = $result->result_source->source_name; | |||||||||||||
| 98 | 9 | ▶ | unless ($source_name) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 0 | my $type = ref $result; | |||||||||||||
| 100 | 0 | croak("Panic: Couldn't determine source name in '$method' for '$type'"); | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | 9 | ▶ | my $object_class = $self->_get_object_class_name($source_name) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | or croak( | ||||||||||||||
| 104 | "Panic: Couldn't determine object class in '$method' for '$source_name'"); | ||||||||||||||
| 105 | 9 | return $object_class->new( { result_source => $result, object_source => $self, } ); | |||||||||||||
| 106 | 45 | }; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | # we do it this way because they might have created a custom | ||||||||||||||
| 110 | # resultset class and thus, we don't know *which* class we're inheriting | ||||||||||||||
| 111 | # from. | ||||||||||||||
| 112 | my $meta = Moose::Meta::Class->create_anon_class( | ||||||||||||||
| 113 | superclasses => [ ref $resultset ], | ||||||||||||||
| 114 | cache => 1, | ||||||||||||||
| 115 | methods => { | ||||||||||||||
| 116 | %methods, | ||||||||||||||
| 117 | all => sub { | ||||||||||||||
| 118 | 5 | ▶ | my $this = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Subroutine
all: called CC=2 SCAR=7.0
| |||||||||||||||
| 119 | 5 | my @all = $this->next::method(@_); | |||||||||||||
| 120 | 5 | ▶ | return unless @all; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 121 | 4 | my $object_class = $self->_get_object_class_name( | |||||||||||||
| 122 | $all[0]->result_source->source_name ); | ||||||||||||||
| 123 | return | ||||||||||||||
| 124 | 4 | map { $object_class->new( { result_source => $_, object_source => $self, } ) } @all; | |||||||||||||
| 125 | }, | ||||||||||||||
| 126 | }, | ||||||||||||||
| 127 | 15 | ); | |||||||||||||
| 128 | 15 | $meta->rebless_instance($resultset); | |||||||||||||
| 129 | 15 | return $resultset; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | sub load_objects { | ||||||||||||||
| 133 | 4 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
load_objects: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||
| 134 | 4 | my $schema = $self->schema; | |||||||||||||
| 135 | |||||||||||||||
| 136 | 4 | ▶ | apply_all_roles( $self->base_class, @{ $self->roles } ) if scalar(@{ $self->roles }); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 137 | |||||||||||||||
| 138 | 4 | foreach my $source_name ($schema->sources) { | |||||||||||||
| 139 | 28 | my $object_class = $self->_get_object_class_name($source_name); | |||||||||||||
| 140 | |||||||||||||||
| 141 | 28 | $self->_debug("Trying to load $object_class"); | |||||||||||||
| 142 | |||||||||||||||
| 143 | 28 | ▶ | if ( try_load_class($object_class) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | 8 | $self->_debug("\t$object_class found."); | |||||||||||||
| 145 | } | ||||||||||||||
| 146 | else { | ||||||||||||||
| 147 | 20 | $self->_debug("\t$object_class not found. Building."); | |||||||||||||
| 148 | 20 | Moose::Meta::Class->create( | |||||||||||||
| 149 | $object_class, | ||||||||||||||
| 150 | superclasses => [ $self->base_class ], | ||||||||||||||
| 151 | ); | ||||||||||||||
| 152 | 20 | ▶ | unless ( Class::Load::is_class_loaded($object_class) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | 0 | die "$object_class didn't load"; | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | } | ||||||||||||||
| 156 | |||||||||||||||
| 157 | # XXX Not sure about this. If they forget to inherit from | ||||||||||||||
| 158 | # DBIx::Class::Objects::Base, we do it for them. It works around an | ||||||||||||||
| 159 | # issue the programmer might forget, but is this too much magic, given | ||||||||||||||
| 160 | # that we're already doing a lot of it? | ||||||||||||||
| 161 | 28 | my $meta = $object_class->meta; | |||||||||||||
| 162 | 28 | my $was_immutable = $meta->is_immutable; | |||||||||||||
| 163 | 28 | ▶ | $meta->make_mutable if $was_immutable; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 164 | 28 | ▶ | unless ( $object_class->isa( $self->base_class ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | 0 | $meta->superclasses( $meta->superclasses, $self->base_class ); | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | 28 | $self->_add_methods( $object_class, $source_name ); | |||||||||||||
| 168 | |||||||||||||||
| 169 | 28 | ▶ | $meta->make_immutable if $was_immutable; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 170 | } | ||||||||||||||
| 171 | } | ||||||||||||||
| 172 | |||||||||||||||
| 173 | sub _add_methods { | ||||||||||||||
| 174 | 28 | ▶ | my ( $self, $class, $source_name ) = @_; | ||||||||||||
|
Subroutine
_add_methods: called CC=3 SCAR=11.0
| |||||||||||||||
| 175 | 28 | my $schema = $self->schema; | |||||||||||||
| 176 | 28 | my $meta = $class->meta; | |||||||||||||
| 177 | |||||||||||||||
| 178 | 28 | my $source = $schema->resultset($source_name)->result_source; | |||||||||||||
| 179 | |||||||||||||||
| 180 | 28 | $self->result_role->meta->apply( | |||||||||||||
| 181 | $meta, | ||||||||||||||
| 182 | handles => [ $source->columns ], | ||||||||||||||
| 183 | result_source_class => $source->result_class, | ||||||||||||||
| 184 | source => $class, | ||||||||||||||
| 185 | ); | ||||||||||||||
| 186 | |||||||||||||||
| 187 | 28 | my @relationships = $source->relationships; | |||||||||||||
| 188 | 28 | foreach my $relationship (@relationships) { | |||||||||||||
| 189 | 40 | my $info = $source->relationship_info($relationship); | |||||||||||||
| 190 | |||||||||||||||
| 191 | # if the accessor is a "multi" access, it returns a resultset, not a | ||||||||||||||
| 192 | # result. | ||||||||||||||
| 193 | 40 | my $is_multi = 'multi' eq $info->{attrs}{accessor}; | |||||||||||||
| 194 | my $source | ||||||||||||||
| 195 | 40 | = $schema->resultset( $info->{source} )->result_source->source_name; | |||||||||||||
| 196 | 40 | my $other_class = $self->_get_object_class_name($source); | |||||||||||||
| 197 | |||||||||||||||
| 198 | # XXX Bless me father for I have sinned ... | ||||||||||||||
| 199 | 40 | $info->{_result_class_to_object_class} = $other_class; | |||||||||||||
| 200 | |||||||||||||||
| 201 | 40 | ▶ | if ($is_multi) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 202 | |||||||||||||||
| 203 | # all resultsets get blessed into our resultset class | ||||||||||||||
| 204 | $meta->add_method( | ||||||||||||||
| 205 | $relationship => sub { | ||||||||||||||
| 206 | 2 | ▶ | my $this = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||
| 207 | 2 | ▶ | my $resultset = $this->result_source->$relationship(@_) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 208 | or return; | ||||||||||||||
| 209 | 2 | return $self->_create_object_set($resultset); | |||||||||||||
| 210 | }, | ||||||||||||||
| 211 | 16 | ); | |||||||||||||
| 212 | } | ||||||||||||||
| 213 | else { | ||||||||||||||
| 214 | # all results are our objects, not results | ||||||||||||||
| 215 | $meta->add_method( | ||||||||||||||
| 216 | $relationship => sub { | ||||||||||||||
| 217 | 4 | ▶ | my $this = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||
| 218 | 4 | ▶ | my $response = $this->result_source->$relationship | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 219 | or return; | ||||||||||||||
| 220 | 4 | return $other_class->new( | |||||||||||||
| 221 | { result_source => $response, object_source => $self, } ); | ||||||||||||||
| 222 | } | ||||||||||||||
| 223 | 24 | ); | |||||||||||||
| 224 | } | ||||||||||||||
| 225 | } | ||||||||||||||
| 226 | } | ||||||||||||||
| 227 | |||||||||||||||
| 228 | sub _debug { | ||||||||||||||
| 229 | 56 | ▶ | my ( $self, $message ) = @_; | ||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=8.1
| |||||||||||||||
| 230 | 56 | ▶ | return unless $self->debug; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 231 | 0 | warn "$message\n"; | |||||||||||||
| 232 | } | ||||||||||||||
| 233 | |||||||||||||||
| 234 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 235 | |||||||||||||||
| 236 | 5 | 1; | |||||||||||||
| 237 | |||||||||||||||
| 238 | __END__ | ||||||||||||||
| 239 | |||||||||||||||
| 240 | =head1 NAME | ||||||||||||||
| 241 | |||||||||||||||
| 242 | DBIx::Class::Objects - Rewrite your DBIC objects via inheritance | ||||||||||||||
| 243 | |||||||||||||||
| 244 | =head1 VERSION | ||||||||||||||
| 245 | |||||||||||||||
| 246 | 0.04 | ||||||||||||||
| 247 | |||||||||||||||
| 248 | =head1 SYNOPSIS | ||||||||||||||
| 249 | |||||||||||||||
| 250 | my $schema = My::DBIx::Class::Schema->connect(@args); | ||||||||||||||
| 251 | |||||||||||||||
| 252 | my $objects = DBIx::Class::Objects->new({ | ||||||||||||||
| 253 | schema => $schema, | ||||||||||||||
| 254 | object_base => 'My::Object', | ||||||||||||||
| 255 | roles => [qw( My::Role::Thing )], | ||||||||||||||
| 256 | }); | ||||||||||||||
| 257 | $objects->load_objects; | ||||||||||||||
| 258 | |||||||||||||||
| 259 | my $person = $objects->objectset('Person') | ||||||||||||||
| 260 | ->find( { email => 'not@home.com' } ); | ||||||||||||||
| 261 | |||||||||||||||
| 262 | # If found, $person is a My::Object::Person object, not a | ||||||||||||||
| 263 | # My::DBIx::Class::Schema::Result::Person | ||||||||||||||
| 264 | |||||||||||||||
| 265 | =head1 WARNING | ||||||||||||||
| 266 | |||||||||||||||
| 267 | The C<DBIx::Class::Objects> module is an experiment to "fix" (for some values | ||||||||||||||
| 268 | of "fix") some issues we traditionally have with ORMs by allowing the | ||||||||||||||
| 269 | programmer to use easily use objects as they wish to rather than the hierarchy | ||||||||||||||
| 270 | forced on them by C<DBIx::Class>. | ||||||||||||||
| 271 | |||||||||||||||
| 272 | This is B<ALPHA> code and may be a very bad idea. Use at your own risk. | ||||||||||||||
| 273 | |||||||||||||||
| 274 | =head1 DESCRIPTION | ||||||||||||||
| 275 | |||||||||||||||
| 276 | Consider a database where you have people and each person might be a customer. | ||||||||||||||
| 277 | The following two tables might demonstrate that relationship. | ||||||||||||||
| 278 | |||||||||||||||
| 279 | CREATE TABLE people ( | ||||||||||||||
| 280 | person_id INTEGER PRIMARY KEY AUTOINCREMENT, | ||||||||||||||
| 281 | name VARCHAR(255) NOT NULL, | ||||||||||||||
| 282 | email VARCHAR(255) NULL UNIQUE, | ||||||||||||||
| 283 | birthday DATETIME NOT NULL | ||||||||||||||
| 284 | ); | ||||||||||||||
| 285 | |||||||||||||||
| 286 | CREATE TABLE customers ( | ||||||||||||||
| 287 | customer_id INTEGER PRIMARY KEY AUTOINCREMENT, | ||||||||||||||
| 288 | person_id INTEGER NOT NULL UNIQUE, | ||||||||||||||
| 289 | first_purchase DATETIME NOT NULL, | ||||||||||||||
| 290 | FOREIGN KEY(person_id) REFERENCES people(person_id) | ||||||||||||||
| 291 | ); | ||||||||||||||
| 292 | |||||||||||||||
| 293 | If your schema starts with C<Sample::Schema::>, in C<DBIx::Class> terms you'll | ||||||||||||||
| 294 | find that C<Sample::Schema::Result::Person> I<might_have> a | ||||||||||||||
| 295 | C<Sample::Schema::Result::Customer>: | ||||||||||||||
| 296 | |||||||||||||||
| 297 | __PACKAGE__->might_have( | ||||||||||||||
| 298 | "customer", | ||||||||||||||
| 299 | "Sample::Schema::Result::Customer", | ||||||||||||||
| 300 | { "foreign.person_id" => "self.person_id" }, | ||||||||||||||
| 301 | ); | ||||||||||||||
| 302 | |||||||||||||||
| 303 | As a programmer, you might find that frustrating. From your viewpoint, you | ||||||||||||||
| 304 | might think that C<Customer> I<isa> C<Person>. Or perhaps you also have | ||||||||||||||
| 305 | C<Employee>s in your database and a person can be both a customer and an | ||||||||||||||
| 306 | employee, how do you model that? For C<DBIx::Class>, you have delegation: | ||||||||||||||
| 307 | |||||||||||||||
| 308 | my $customer = $person->customer; | ||||||||||||||
| 309 | my $employee = $person->employee; | ||||||||||||||
| 310 | |||||||||||||||
| 311 | Whereas for OO code, you might want to have a C<Person> class and C<Employee> | ||||||||||||||
| 312 | and C<Customer> roles. Or maybe you're a fan of multiple inheritance (I hope | ||||||||||||||
| 313 | not) and you create a C<CustomerEmployee> class which tries to inherit from | ||||||||||||||
| 314 | both C<Customer> and C<Employee>. | ||||||||||||||
| 315 | |||||||||||||||
| 316 | Not having full control over your object hierarchy is merely one of the | ||||||||||||||
| 317 | problems with the L<Object-Relational Impedence Mismatch|http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch>. | ||||||||||||||
| 318 | |||||||||||||||
| 319 | Or maybe you're dismayed to instantiate a C<Sample::Schema::Result::Person> | ||||||||||||||
| 320 | object and discover that you have 157 methods because you were forced to | ||||||||||||||
| 321 | inherit from C<DBIx::Class::Core>, when all you wanted was the name, email and | ||||||||||||||
| 322 | birthday. This experiment tries to minimize that. | ||||||||||||||
| 323 | |||||||||||||||
| 324 | =head1 METHODS | ||||||||||||||
| 325 | |||||||||||||||
| 326 | =head2 C<new> | ||||||||||||||
| 327 | |||||||||||||||
| 328 | my $objects = DBIx::Class::Objects->new({ | ||||||||||||||
| 329 | schema => $schema, | ||||||||||||||
| 330 | object_base => 'My::Object', | ||||||||||||||
| 331 | }); | ||||||||||||||
| 332 | |||||||||||||||
| 333 | The C<new> constructor takes two required arguments and one optional argument: | ||||||||||||||
| 334 | |||||||||||||||
| 335 | =over 4 | ||||||||||||||
| 336 | |||||||||||||||
| 337 | =item * C<schema> (required) | ||||||||||||||
| 338 | |||||||||||||||
| 339 | A C<DBIx::Class::Schema> object. | ||||||||||||||
| 340 | |||||||||||||||
| 341 | =item * C<object_base> (required) | ||||||||||||||
| 342 | |||||||||||||||
| 343 | The package prefix of your name objects. If your schema classes resemble | ||||||||||||||
| 344 | something like C<Sample::Schema::Result::Person>, your returned objects will | ||||||||||||||
| 345 | have names like C<My::Object::Person> (assuming you used C<My::Object> for the | ||||||||||||||
| 346 | C<object_base> parameter). | ||||||||||||||
| 347 | |||||||||||||||
| 348 | =item * C<debug> (optional) | ||||||||||||||
| 349 | |||||||||||||||
| 350 | At the present time, this will print to STDERR a list of objects you're trying | ||||||||||||||
| 351 | to build and whether or not a concrete implementation was found or it's being | ||||||||||||||
| 352 | built on the fly. | ||||||||||||||
| 353 | |||||||||||||||
| 354 | Trying to load My::Object::Person | ||||||||||||||
| 355 | My::Object::Person found. | ||||||||||||||
| 356 | Trying to load My::Object::Order | ||||||||||||||
| 357 | My::Object::Order not found. Building. | ||||||||||||||
| 358 | Trying to load My::Object::Customer | ||||||||||||||
| 359 | My::Object::Customer found. | ||||||||||||||
| 360 | Trying to load My::Object::Item | ||||||||||||||
| 361 | My::Object::Item not found. Building. | ||||||||||||||
| 362 | Trying to load My::Object::OrderItem | ||||||||||||||
| 363 | My::Object::OrderItem not found. Building. | ||||||||||||||
| 364 | |||||||||||||||
| 365 | =item * C<roles> (optional) | ||||||||||||||
| 366 | |||||||||||||||
| 367 | This will apply the optional Moose role(s) to your My::Object classes. Useful | ||||||||||||||
| 368 | for if you have some utility functions you would like applied to each table | ||||||||||||||
| 369 | without having to create files for every table. | ||||||||||||||
| 370 | |||||||||||||||
| 371 | =back | ||||||||||||||
| 372 | |||||||||||||||
| 373 | =head2 C<load_objects> | ||||||||||||||
| 374 | |||||||||||||||
| 375 | $objects->load_objects; | ||||||||||||||
| 376 | |||||||||||||||
| 377 | Similar to L<DBIx::Class::Schema>'s C<load_namespaces>, but it's an instane | ||||||||||||||
| 378 | method instead of a class method. It will load all of your objects for you. It | ||||||||||||||
| 379 | will ensure that your objects inherit from L<DBIx::Class::Objects::Base> and | ||||||||||||||
| 380 | will apply the parameterized role L<DBIx::Class::Objects::Role::Result>. | ||||||||||||||
| 381 | |||||||||||||||
| 382 | The base class is what allows things like C<update> to be called directly on | ||||||||||||||
| 383 | the object. Is is the parameterized role which sets up the delegation to the | ||||||||||||||
| 384 | C<DBIx::Class> objects. | ||||||||||||||
| 385 | |||||||||||||||
| 386 | =head2 C<objectset> | ||||||||||||||
| 387 | |||||||||||||||
| 388 | my $person = $objects->objectset('Person') | ||||||||||||||
| 389 | ->find( { email => 'not@home.com' } ); | ||||||||||||||
| 390 | |||||||||||||||
| 391 | This method is similar to C<< $schema->resultset >>, but it returns sets of | ||||||||||||||
| 392 | C<DBIx::Class::Objects> objects instead of results. The interface is the same | ||||||||||||||
| 393 | as C<DBIx::Class::ResultSet>, but calling methods like C<find>, C<next>, | ||||||||||||||
| 394 | C<first>, C<all> and so on should I<do the right thing> (famous last words). | ||||||||||||||
| 395 | |||||||||||||||
| 396 | =head1 LET'S DELEGATE TO DBIx::Class RESULTS | ||||||||||||||
| 397 | |||||||||||||||
| 398 | C<DBIx::Class::Objects> is an attempt to allow you to recompose your | ||||||||||||||
| 399 | C<DBIx::Class> objects as you would like. Instead of C<DBIx::Class> returning | ||||||||||||||
| 400 | resultsets and results, C<DBIx::Class::Objects> returns objectsets and | ||||||||||||||
| 401 | objects. You can do anything you want with the latter. | ||||||||||||||
| 402 | |||||||||||||||
| 403 | =head2 Basic Usage | ||||||||||||||
| 404 | |||||||||||||||
| 405 | Using this module is as simple as this: | ||||||||||||||
| 406 | |||||||||||||||
| 407 | my $schema = Sample::Schema->connect(@args); | ||||||||||||||
| 408 | |||||||||||||||
| 409 | my $objects = DBIx::Class::Objects->new({ | ||||||||||||||
| 410 | schema => $schema, | ||||||||||||||
| 411 | object_base => 'My::Object', | ||||||||||||||
| 412 | }); | ||||||||||||||
| 413 | $objects->load_objects; | ||||||||||||||
| 414 | |||||||||||||||
| 415 | my $person = $objects->objectset('Person') | ||||||||||||||
| 416 | ->find( { email => 'not@home.com' } ); | ||||||||||||||
| 417 | |||||||||||||||
| 418 | And you'll discover that you get back a C<My::Object::Person> object instead | ||||||||||||||
| 419 | of a C<Sample::Schema::Result::Person> object. In C<DBIx::Class>, if you don't | ||||||||||||||
| 420 | explicitly create resultset classes, a default resultset class will be created | ||||||||||||||
| 421 | for you. In C<DBIx::Class::Objects>, if you don't explicitly create object | ||||||||||||||
| 422 | classes, a default one is created for you. For example, if you don't have a | ||||||||||||||
| 423 | C<My::Object::Person> class written (or if C<DBIx::Class::Objects> can't find | ||||||||||||||
| 424 | it), you will have a basic C<My::Object::Person> instance with the following | ||||||||||||||
| 425 | methods (according to the debugger): | ||||||||||||||
| 426 | |||||||||||||||
| 427 | DB<2> m $person | ||||||||||||||
| 428 | BUILD | ||||||||||||||
| 429 | _my_object_person | ||||||||||||||
| 430 | birthday | ||||||||||||||
| 431 | customer | ||||||||||||||
| 432 | |||||||||||||||
| 433 | meta | ||||||||||||||
| 434 | name | ||||||||||||||
| 435 | object_source | ||||||||||||||
| 436 | person_id | ||||||||||||||
| 437 | result_source | ||||||||||||||
| 438 | update | ||||||||||||||
| 439 | via DBIx::Class::Objects::Base: DESTROY | ||||||||||||||
| 440 | via DBIx::Class::Objects::Base: new | ||||||||||||||
| 441 | via DBIx::Class::Objects::Base -> Moose::Object: BUILDALL | ||||||||||||||
| 442 | via DBIx::Class::Objects::Base -> Moose::Object: BUILDARGS | ||||||||||||||
| 443 | via DBIx::Class::Objects::Base -> Moose::Object: DEMOLISHALL | ||||||||||||||
| 444 | via DBIx::Class::Objects::Base -> Moose::Object: DOES | ||||||||||||||
| 445 | via DBIx::Class::Objects::Base -> Moose::Object: does | ||||||||||||||
| 446 | via DBIx::Class::Objects::Base -> Moose::Object: dump | ||||||||||||||
| 447 | via UNIVERSAL: VERSION | ||||||||||||||
| 448 | via UNIVERSAL: can | ||||||||||||||
| 449 | via UNIVERSAL: isa | ||||||||||||||
| 450 | |||||||||||||||
| 451 | That's actually not too bad, compared to C<DBIx::Class>. If you remove | ||||||||||||||
| 452 | C<UNIVERSAL> methods and methods in ALL CAPS, you get this: | ||||||||||||||
| 453 | |||||||||||||||
| 454 | _my_object_person | ||||||||||||||
| 455 | birthday | ||||||||||||||
| 456 | customer | ||||||||||||||
| 457 | |||||||||||||||
| 458 | meta | ||||||||||||||
| 459 | name | ||||||||||||||
| 460 | object_source | ||||||||||||||
| 461 | person_id | ||||||||||||||
| 462 | result_source | ||||||||||||||
| 463 | update | ||||||||||||||
| 464 | via DBIx::Class::Objects::Base: new | ||||||||||||||
| 465 | via DBIx::Class::Objects::Base -> Moose::Object: does | ||||||||||||||
| 466 | via DBIx::Class::Objects::Base -> Moose::Object: dump | ||||||||||||||
| 467 | |||||||||||||||
| 468 | That's actually a fairly clean object. The C<person_id>, C<email>, C<name> and | ||||||||||||||
| 469 | C<birthday> objects are handled by the C<result_source>. If you want to update | ||||||||||||||
| 470 | the object, you do this: | ||||||||||||||
| 471 | |||||||||||||||
| 472 | $person->name($new_name); | ||||||||||||||
| 473 | $person->update; | ||||||||||||||
| 474 | |||||||||||||||
| 475 | =head2 Creating your own objects | ||||||||||||||
| 476 | |||||||||||||||
| 477 | Having these objects spring up automatically is great and if you have 100 | ||||||||||||||
| 478 | result sources, it's nice that you don't have to write 100 object classes. | ||||||||||||||
| 479 | However, though you have far fewer methods, what's the point? | ||||||||||||||
| 480 | |||||||||||||||
| 481 | Well, you can write your own classes: | ||||||||||||||
| 482 | |||||||||||||||
| 483 | package My::Object::Person; | ||||||||||||||
| 484 | |||||||||||||||
| 485 | use Moose; | ||||||||||||||
| 486 | use namespace::autoclean; | ||||||||||||||
| 487 | |||||||||||||||
| 488 | # this is optional. If you forget to include it, DBIx::Class::Objects will | ||||||||||||||
| 489 | # inject this for you. However, it's good to have it here for | ||||||||||||||
| 490 | # documentation purposes. | ||||||||||||||
| 491 | extends 'DBIx::Class::Objects::Base'; | ||||||||||||||
| 492 | |||||||||||||||
| 493 | sub is_customer { | ||||||||||||||
| 494 | my $self = shift; | ||||||||||||||
| 495 | return defined $self->customer; | ||||||||||||||
| 496 | } | ||||||||||||||
| 497 | |||||||||||||||
| 498 | __PACKAGE__->meta->make_immutable; | ||||||||||||||
| 499 | |||||||||||||||
| 500 | 1; | ||||||||||||||
| 501 | |||||||||||||||
| 502 | Again, that's not much of a win, but what if you want inheritance? | ||||||||||||||
| 503 | |||||||||||||||
| 504 | package My::Object::Customer; | ||||||||||||||
| 505 | |||||||||||||||
| 506 | use Moose; | ||||||||||||||
| 507 | extends 'My::Object::Person'; | ||||||||||||||
| 508 | |||||||||||||||
| 509 | __PACKAGE__->meta->make_immutable; | ||||||||||||||
| 510 | |||||||||||||||
| 511 | 1; | ||||||||||||||
| 512 | |||||||||||||||
| 513 | You've now inherited the delegated methods from C<My::Object::Person>. | ||||||||||||||
| 514 | |||||||||||||||
| 515 | my $customer_os = $objects->objectset('Customer')->search( | ||||||||||||||
| 516 | \%dbix_class_search_args | ||||||||||||||
| 517 | ); | ||||||||||||||
| 518 | foreach my $customer ($customer_os->next) { | ||||||||||||||
| 519 | if ( $some_condition ) { | ||||||||||||||
| 520 | $customer->name('new name'); | ||||||||||||||
| 521 | $customer->update; # updates $customer->person, too | ||||||||||||||
| 522 | } | ||||||||||||||
| 523 | } | ||||||||||||||
| 524 | |||||||||||||||
| 525 | For every object, calling C<result_source> gets you the original | ||||||||||||||
| 526 | C<DBIx::Class::Result>, | ||||||||||||||
| 527 | |||||||||||||||
| 528 | say $customer->result_source; # Sample::Schema::Result::Customer | ||||||||||||||
| 529 | say $customer->person->result_source; # Sample::Schema::Result::Person | ||||||||||||||
| 530 | |||||||||||||||
| 531 | and calling C<object_source> gets you the encapsulating C<DBIx::Class::Objects> | ||||||||||||||
| 532 | object. | ||||||||||||||
| 533 | |||||||||||||||
| 534 | =head1 AUTHOR | ||||||||||||||
| 535 | |||||||||||||||
| 536 | Curtis "Ovid" Poe, C<< <ovid at cpan.org> >> | ||||||||||||||
| 537 | |||||||||||||||
| 538 | Dan Burke C<< dburke at addictmud.org >> | ||||||||||||||
| 539 | |||||||||||||||
| 540 | =head1 BUGS | ||||||||||||||
| 541 | |||||||||||||||
| 542 | Please report any bugs or feature requests to C<bug-object-bridge at | ||||||||||||||
| 543 | rt.cpan.org>, or through the web interface at | ||||||||||||||
| 544 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-Objects>. I will be | ||||||||||||||
| 545 | notified, and then you'll automatically be notified of progress on your bug as | ||||||||||||||
| 546 | I make changes. | ||||||||||||||
| 547 | |||||||||||||||
| 548 | =head1 SUPPORT | ||||||||||||||
| 549 | |||||||||||||||
| 550 | You can find documentation for this module with the perldoc command. | ||||||||||||||
| 551 | |||||||||||||||
| 552 | perldoc DBIx::Class::Objects | ||||||||||||||
| 553 | |||||||||||||||
| 554 | You can also look for information at: | ||||||||||||||
| 555 | |||||||||||||||
| 556 | =over 4 | ||||||||||||||
| 557 | |||||||||||||||
| 558 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||
| 559 | |||||||||||||||
| 560 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-Objects> | ||||||||||||||
| 561 | |||||||||||||||
| 562 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||
| 563 | |||||||||||||||
| 564 | L<http://annocpan.org/dist/DBIx-Class-Objects> | ||||||||||||||
| 565 | |||||||||||||||
| 566 | =item * CPAN Ratings | ||||||||||||||
| 567 | |||||||||||||||
| 568 | L<http://cpanratings.perl.org/d/DBIx-Class-Objects> | ||||||||||||||
| 569 | |||||||||||||||
| 570 | =item * Search CPAN | ||||||||||||||
| 571 | |||||||||||||||
| 572 | L<http://search.cpan.org/dist/DBIx-Class-Objects/> | ||||||||||||||
| 573 | |||||||||||||||
| 574 | =back | ||||||||||||||
| 575 | |||||||||||||||
| 576 | |||||||||||||||
| 577 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||
| 578 | |||||||||||||||
| 579 | |||||||||||||||
| 580 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 581 | |||||||||||||||
| 582 | Copyright 2014 Curtis "Ovid" Poe. | ||||||||||||||
| 583 | |||||||||||||||
| 584 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 585 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||
| 586 | copy of the full license at: | ||||||||||||||
| 587 | |||||||||||||||
| 588 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||
| 589 | |||||||||||||||
| 590 | Any use, modification, and distribution of the Standard or Modified | ||||||||||||||
| 591 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||||||
| 592 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||||||
| 593 | or distribute the Package, if you do not accept this license. | ||||||||||||||
| 594 | |||||||||||||||
| 595 | If your Modified Version has been derived from a Modified Version made | ||||||||||||||
| 596 | by someone other than you, you are nevertheless required to ensure that | ||||||||||||||
| 597 | your Modified Version complies with the requirements of this license. | ||||||||||||||
| 598 | |||||||||||||||
| 599 | This license does not grant you the right to use any trademark, service | ||||||||||||||
| 600 | mark, tradename, or logo of the Copyright Holder. | ||||||||||||||
| 601 | |||||||||||||||
| 602 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||||||
| 603 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||||||
| 604 | otherwise transfer the Package with respect to any patent claims | ||||||||||||||
| 605 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||||||
| 606 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||||||
| 607 | counterclaim) against any party alleging that the Package constitutes | ||||||||||||||
| 608 | direct or contributory patent infringement, then this Artistic License | ||||||||||||||
| 609 | to you shall terminate on the date that such litigation is filed. | ||||||||||||||
| 610 | |||||||||||||||
| 611 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||||||
| 612 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||||||
| 613 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||||||
| 614 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||||||
| 615 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||||||
| 616 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||||||
| 617 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||||||
| 618 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||