| 1 | package Oryx::Parent; | ||
| 2 | |||
| 3 | use Scalar::Util qw(blessed); | ||
| 4 | |||
| 5 | use base qw(Oryx::MetaClass); | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Oryx::Parent - multiple inheritance meta-type for Oryx | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | package Fruit; | ||
| 14 | use base qw(Oryx::Class); | ||
| 15 | our $schema = { | ||
| 16 | attributes => [{ | ||
| 17 | colour => 'String', | ||
| 18 | }], | ||
| 19 | } | ||
| 20 | 1; | ||
| 21 | |||
| 22 | package Food; | ||
| 23 | use base qw(Oryx::Class); | ||
| 24 | our $schema = { | ||
| 25 | attributes => [{ | ||
| 26 | energy => 'Float', | ||
| 27 | }], | ||
| 28 | } | ||
| 29 | 1; | ||
| 30 | |||
| 31 | package Orange; | ||
| 32 | use base qw(Fruit Food); | ||
| 33 | our $schema = { | ||
| 34 | attributes => [{ | ||
| 35 | segments => 'Integer', | ||
| 36 | }] | ||
| 37 | } | ||
| 38 | 1; | ||
| 39 | |||
| 40 | use Orange; | ||
| 41 | my $orange = Orange->create({ | ||
| 42 | segments => 10, | ||
| 43 | energy => 543.21, | ||
| 44 | colour => 'orange', | ||
| 45 | }); | ||
| 46 | |||
| 47 | $orange->update; | ||
| 48 | $orange->commit; | ||
| 49 | |||
| 50 | my $id = $orange->id; | ||
| 51 | undef $orange; | ||
| 52 | |||
| 53 | my $retrieved = Orange->retrieve($id); | ||
| 54 | print $retrieved->colour; # prints 'orange' | ||
| 55 | |||
| 56 | my $food_instance = $retrieved->PARENT('Food'); | ||
| 57 | print $food_instance->energy; # prints 543.21 | ||
| 58 | |||
| 59 | $food_instance->energy(42.00); | ||
| 60 | $food_instance->update; | ||
| 61 | |||
| 62 | my $changed_orange = Orange->retrieve($id); | ||
| 63 | print $changed_orange->energy; # prints 42.00 (parent instance updated) | ||
| 64 | |||
| 65 | =head1 DESCRIPTION | ||
| 66 | |||
| 67 | Oryx::Parent objects are constructed during L<Oryx::Class> initialization | ||
| 68 | by inspecting your class' C<@ISA> array, so you get one of these hanging | ||
| 69 | off your class for each superclass that is also an L<Oryx::Class> derivative. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub new { | ||
| 74 | my $_class = shift; | ||
| 75 | my ($class, $child) = @_; | ||
| 76 | my $self = bless { | ||
| 77 | class => $class, # superclass | ||
| 78 | child => $child, # subclass | ||
| 79 | }, $_class; | ||
| 80 | |||
| 81 | eval "use $class"; $self->_croak($@) if $@; | ||
| 82 | |||
| 83 | unless (UNIVERSAL::can($child, 'PARENT')) { | ||
| 84 | no strict 'refs'; | ||
| 85 | *{$child.'::PARENT'} = $self->_mk_accessor; | ||
| 86 | } | ||
| 87 | |||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub dbh { $_[0]->{class}->dbh } | ||
| 92 | |||
| 93 | sub class { $_[0]->{class} } | ||
| 94 | |||
| 95 | sub child { $_[0]->{child} } | ||
| 96 | |||
| 97 | sub link_table { | ||
| 98 | lc($_[0]->child->name.'_parents'); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub child_field { | ||
| 102 | return lc($_[0]->child->name.'_id'); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _mk_accessor { | ||
| 106 | return sub { | ||
| 107 | my $self = shift; | ||
| 108 | my $class = shift; | ||
| 109 | $self->{__parents} = { } unless defined $self->{__parents}; | ||
| 110 | if (@_) { | ||
| 111 | $self->{__parents}->{$class} = shift; | ||
| 112 | } else { | ||
| 113 | $self->{__parents}->{$class}; | ||
| 114 | } | ||
| 115 | }; | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | __END__ |