| 1 | package Fey::Meta::Role::Relationship::HasOne; | ||||||||||
| 2 | { | ||||||||||
| 3 | 10 | $Fey::Meta::Role::Relationship::HasOne::VERSION = '0.46'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | 10 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 10 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 10 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 10 | ▶ | use Fey::Exceptions qw( param_error ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 10 | ▶ | use Fey::ORM::Types qw( Bool Item Maybe ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 10 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 10 | with 'Fey::Meta::Role::Relationship'; | |||||||||
| 16 | |||||||||||
| 17 | 10 | has associated_attribute => ( | |||||||||
| 18 | is => 'rw', | ||||||||||
| 19 | isa => Maybe['Moose::Meta::Attribute'], | ||||||||||
| 20 | init_arg => undef, | ||||||||||
| 21 | lazy => 1, | ||||||||||
| 22 | builder => '_build_associated_attribute', | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 10 | has associated_method => ( | |||||||||
| 26 | is => 'rw', | ||||||||||
| 27 | isa => Maybe['Moose::Meta::Method'], | ||||||||||
| 28 | init_arg => undef, | ||||||||||
| 29 | lazy => 1, | ||||||||||
| 30 | builder => '_build_associated_method', | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 10 | has allows_undef => ( | |||||||||
| 34 | is => 'ro', | ||||||||||
| 35 | isa => Bool, | ||||||||||
| 36 | lazy => 1, | ||||||||||
| 37 | builder => '_build_allows_undef', | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | 10 | has handles => ( | |||||||||
| 41 | is => 'ro', | ||||||||||
| 42 | |||||||||||
| 43 | # just gets passed on for attribute creation | ||||||||||
| 44 | isa => Item, | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | sub _build_associated_attribute { | ||||||||||
| 48 | 9 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_associated_attribute: called CC=4 SCAR=13.9
| |||||||||||
| 49 | |||||||||||
| 50 | 9 | ▶ | return unless $self->is_cached(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | # It'd be nice to set isa to the actual foreign class, but we may | ||||||||||
| 53 | # not be able to map a table to a class yet, since that depends on | ||||||||||
| 54 | # the related class being loaded. It doesn't really matter, since | ||||||||||
| 55 | # this accessor is read-only, so there's really no typing issue to | ||||||||||
| 56 | # deal with. | ||||||||||
| 57 | 9 | my $type = 'Fey::Object::Table'; | |||||||||
| 58 | 9 | ▶ | $type = "Maybe[$type]" if $self->allows_undef(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | |||||||||||
| 60 | 7 | my %attr_p = ( | |||||||||
| 61 | is => 'rw', | ||||||||||
| 62 | isa => $type, | ||||||||||
| 63 | lazy => 1, | ||||||||||
| 64 | default => $self->_make_subref(), | ||||||||||
| 65 | writer => q{_set_} . $self->name(), | ||||||||||
| 66 | predicate => q{_has_} . $self->name(), | ||||||||||
| 67 | clearer => q{_clear_} . $self->name(), | ||||||||||
| 68 | ); | ||||||||||
| 69 | |||||||||||
| 70 | 7 | ▶ | $attr_p{handles} = $self->handles() | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | if $self->handles(); | ||||||||||
| 72 | |||||||||||
| 73 | 7 | return $self->associated_class()->attribute_metaclass()->new( | |||||||||
| 74 | $self->name(), | ||||||||||
| 75 | %attr_p, | ||||||||||
| 76 | ); | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 8 | ▶ | sub _build_is_cached {1} | ||||||||
|
Subroutine
_build_is_cached: called CC=1 SCAR=0.0
| |||||||||||
| 80 | |||||||||||
| 81 | sub _build_associated_method { | ||||||||||
| 82 | 3 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_associated_method: called CC=2 SCAR=6.9
| |||||||||||
| 83 | |||||||||||
| 84 | 3 | ▶ | return if $self->is_cached(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | |||||||||||
| 86 | 2 | return $self->associated_class()->method_metaclass()->wrap( | |||||||||
| 87 | name => $self->name(), | ||||||||||
| 88 | package_name => $self->associated_class()->name(), | ||||||||||
| 89 | body => $self->_make_subref(), | ||||||||||
| 90 | ); | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | sub attach_to_class { | ||||||||||
| 94 | 11 | ▶ | my $self = shift; | ||||||||
|
Subroutine
attach_to_class: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 95 | 11 | my $class = shift; | |||||||||
| 96 | |||||||||||
| 97 | 11 | $self->_set_associated_class($class); | |||||||||
| 98 | |||||||||||
| 99 | 11 | ▶ | if ( $self->is_cached() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 9 | $class->add_attribute( $self->associated_attribute() ); | |||||||||
| 101 | } | ||||||||||
| 102 | else { | ||||||||||
| 103 | 2 | $class->add_method( $self->name() => $self->associated_method() ); | |||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | sub detach_from_class { | ||||||||||
| 108 | 8 | ▶ | my $self = shift; | ||||||||
|
Subroutine
detach_from_class: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 109 | |||||||||||
| 110 | 8 | ▶ | return unless $self->associated_class(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | |||||||||||
| 112 | 8 | ▶ | if ( $self->is_cached() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 7 | $self->associated_class->remove_attribute( $self->name() ); | |||||||||
| 114 | } | ||||||||||
| 115 | else { | ||||||||||
| 116 | 1 | $self->associated_class->remove_method( $self->name() ); | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 8 | $self->_clear_associated_class(); | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 10 | 1; | |||||||||
| 123 | |||||||||||
| 124 | # ABSTRACT: A role for has-one metaclasses | ||||||||||
| 125 | |||||||||||
| 126 | __END__ | ||||||||||
| 127 | |||||||||||
| 128 | =pod | ||||||||||
| 129 | |||||||||||
| 130 | =head1 NAME | ||||||||||
| 131 | |||||||||||
| 132 | Fey::Meta::Role::Relationship::HasOne - A role for has-one metaclasses | ||||||||||
| 133 | |||||||||||
| 134 | =head1 VERSION | ||||||||||
| 135 | |||||||||||
| 136 | version 0.46 | ||||||||||
| 137 | |||||||||||
| 138 | =head1 DESCRIPTION | ||||||||||
| 139 | |||||||||||
| 140 | This role provides functionality for the two has-one metaclasses, | ||||||||||
| 141 | L<Fey::Meta::HasOne::ViaFK> and L<Fey::Meta::HasOne::ViaSelect>. | ||||||||||
| 142 | |||||||||||
| 143 | =head1 CONSTRUCTOR OPTIONS | ||||||||||
| 144 | |||||||||||
| 145 | This role adds the following constructor options: | ||||||||||
| 146 | |||||||||||
| 147 | =over 4 | ||||||||||
| 148 | |||||||||||
| 149 | =item * handles | ||||||||||
| 150 | |||||||||||
| 151 | This will simply be passed on when an attribute for this has-one relationship | ||||||||||
| 152 | is created. Note that this is ignored if C<is_cached> is false. | ||||||||||
| 153 | |||||||||||
| 154 | =item * allows_undef | ||||||||||
| 155 | |||||||||||
| 156 | A boolean indicating whether or not the relationship's value can be | ||||||||||
| 157 | C<undef>. | ||||||||||
| 158 | |||||||||||
| 159 | =item * is_cached | ||||||||||
| 160 | |||||||||||
| 161 | Defaults to true for this class. | ||||||||||
| 162 | |||||||||||
| 163 | =back | ||||||||||
| 164 | |||||||||||
| 165 | =head1 METHODS | ||||||||||
| 166 | |||||||||||
| 167 | This role provides the following methods: | ||||||||||
| 168 | |||||||||||
| 169 | =head2 $ho->name() | ||||||||||
| 170 | |||||||||||
| 171 | Corresponds to the value passed to the constructor. | ||||||||||
| 172 | |||||||||||
| 173 | =head2 $ho->table() | ||||||||||
| 174 | |||||||||||
| 175 | Corresponds to the value passed to the constructor. | ||||||||||
| 176 | |||||||||||
| 177 | =head2 $ho->foreign_table() | ||||||||||
| 178 | |||||||||||
| 179 | Corresponds to the value passed to the constructor. | ||||||||||
| 180 | |||||||||||
| 181 | =head2 $ho->is_cached() | ||||||||||
| 182 | |||||||||||
| 183 | Corresponds to the value passed to the constructor, or the calculated | ||||||||||
| 184 | default. | ||||||||||
| 185 | |||||||||||
| 186 | =head2 $ho->allows_undef() | ||||||||||
| 187 | |||||||||||
| 188 | Corresponds to the value passed to the constructor. | ||||||||||
| 189 | |||||||||||
| 190 | =head2 $ho->handles() | ||||||||||
| 191 | |||||||||||
| 192 | Corresponds to the value passed to the constructor. | ||||||||||
| 193 | |||||||||||
| 194 | =head2 $ho->attach_to_class($class) | ||||||||||
| 195 | |||||||||||
| 196 | This method takes a F<Fey::Meta::Class::Table> object and attaches the | ||||||||||
| 197 | relationship to the associated class. If this relationship is cached, | ||||||||||
| 198 | it creates a new attribute, otherwise it creates a new method. | ||||||||||
| 199 | |||||||||||
| 200 | The method/attribute returns an object belonging to the class | ||||||||||
| 201 | associated with the foreign table. It can return C<undef> if | ||||||||||
| 202 | C<allows_undef> is true. | ||||||||||
| 203 | |||||||||||
| 204 | =head2 $ho->associated_class() | ||||||||||
| 205 | |||||||||||
| 206 | The class associated with this object. This is undefined until C<< | ||||||||||
| 207 | $ho->attach_to_class() >> is called. | ||||||||||
| 208 | |||||||||||
| 209 | =head2 $ho->associated_attribute() | ||||||||||
| 210 | |||||||||||
| 211 | Returns the attribute associated with this object, if any. | ||||||||||
| 212 | |||||||||||
| 213 | =head2 $ho->associated_method() | ||||||||||
| 214 | |||||||||||
| 215 | Returns the method associated with this object, if any. | ||||||||||
| 216 | |||||||||||
| 217 | =head2 $ho->detach_from_class() | ||||||||||
| 218 | |||||||||||
| 219 | If this object was attached to a class, it removes any attribute or | ||||||||||
| 220 | method it made, and unsets the C<associated_class>. | ||||||||||
| 221 | |||||||||||
| 222 | =head1 AUTHOR | ||||||||||
| 223 | |||||||||||
| 224 | Dave Rolsky <autarch@urth.org> | ||||||||||
| 225 | |||||||||||
| 226 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 227 | |||||||||||
| 228 | This software is copyright (c) 2011 by Dave Rolsky. | ||||||||||
| 229 | |||||||||||
| 230 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 231 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 232 | |||||||||||
| 233 | =cut | ||||||||||