| 1 | package InterMine::Model::Object; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use MooseX::Types::Moose qw(Str); | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | InterMine::Model::Object - the class all instantiated objects inherit from | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | # Do not use this class directly. | ||
| 13 | |||
| 14 | use Test::More; | ||
| 15 | |||
| 16 | my $gene = $model->make_new("Gene"); | ||
| 17 | |||
| 18 | can_ok($gene, qw/getObjectId hasObjectId class equals merge/); | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | A base class for instantiated objects. | ||
| 23 | |||
| 24 | =head2 ATTRIBUTES | ||
| 25 | |||
| 26 | =head2 objectId | ||
| 27 | |||
| 28 | The internal object id | ||
| 29 | |||
| 30 | =over 4 | ||
| 31 | |||
| 32 | =item reader: getObjectId | ||
| 33 | |||
| 34 | get the object id attribute. | ||
| 35 | |||
| 36 | =item predicate: hasObjectId | ||
| 37 | |||
| 38 | returns true if the object has an id. | ||
| 39 | |||
| 40 | =back | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | has objectId => ( | ||
| 45 | reader => "getObjectId", | ||
| 46 | predicate => "hasObjectId", | ||
| 47 | isa => Str, | ||
| 48 | ); | ||
| 49 | |||
| 50 | =head2 id | ||
| 51 | |||
| 52 | Read-Only synonym for getObjectId | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub id { | ||
| 57 | my $self = shift; | ||
| 58 | return $self->getObjectId; | ||
| 59 | } | ||
| 60 | |||
| 61 | =head1 METHODS | ||
| 62 | |||
| 63 | =head2 equals($something) | ||
| 64 | |||
| 65 | returns true if the other object has the same objectId | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub equals { | ||
| 70 | my ($self, $other) = @_; | ||
| 71 | if (blessed $other && $other->isa(__PACKAGE__)) { | ||
| 72 | return | ||
| 73 | ($self->hasObjectId && $other->hasObjectId && | ||
| 74 | $self->getObjectId eq $other->getObjectId); | ||
| 75 | } else { | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 merge($other) | ||
| 81 | |||
| 82 | merge the properties of other into self | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub merge { | ||
| 87 | my ($self, $other) = @_; | ||
| 88 | for my $attr ($other->class->get_all_attributes) { | ||
| 89 | my $reader = $attr->get_read_method; | ||
| 90 | my $writer = $attr->get_write_method or next; | ||
| 91 | if (my $value = $other->$reader()) { | ||
| 92 | next if (ref $value eq 'ARRAY' and not @$value); | ||
| 93 | $self->$writer($value); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | =head2 class | ||
| 99 | |||
| 100 | make class a synonym for meta | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub class { | ||
| 105 | my $self = shift; | ||
| 106 | return $self->meta; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 isa | ||
| 110 | |||
| 111 | overrides isa to respond to unqualified names as well as full ones. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub isa { | ||
| 116 | my $self = shift; | ||
| 117 | my $other = shift; | ||
| 118 | my $extended_name = $self->class->model->{perl_package} . $other; | ||
| 119 | if (UNIVERSAL::isa($self, $extended_name)) { | ||
| 120 | return 1; | ||
| 121 | } else { | ||
| 122 | return UNIVERSAL::isa($self, $other); | ||
| 123 | } | ||
| 124 | }; | ||
| 125 | |||
| 126 | =head1 OVERLOADING | ||
| 127 | |||
| 128 | =head2 EQUALITY (==, eq) | ||
| 129 | |||
| 130 | return true if objectIds match | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | use overload ( | ||
| 135 | '==' => 'equals', | ||
| 136 | 'eq' => 'equals', | ||
| 137 | fallback => 1, | ||
| 138 | ); | ||
| 139 | |||
| 140 | __PACKAGE__->meta->make_immutable; | ||
| 141 | no Moose; | ||
| 142 | 1; | ||
| 143 | |||
| 144 | =head1 SEE ALSO | ||
| 145 | |||
| 146 | =over 4 | ||
| 147 | |||
| 148 | =item * L<Moose::Object> | ||
| 149 | |||
| 150 | =back | ||
| 151 | |||
| 152 | =head1 AUTHOR | ||
| 153 | |||
| 154 | FlyMine C<< <support@flymine.org> >> | ||
| 155 | |||
| 156 | =head1 BUGS | ||
| 157 | |||
| 158 | Please report any bugs or feature requests to C<support@flymine.org>. | ||
| 159 | |||
| 160 | =head1 SUPPORT | ||
| 161 | |||
| 162 | You can find documentation for this module with the perldoc command. | ||
| 163 | |||
| 164 | perldoc InterMine::Model::Object | ||
| 165 | |||
| 166 | You can also look for information at: | ||
| 167 | |||
| 168 | =over 4 | ||
| 169 | |||
| 170 | =item * FlyMine | ||
| 171 | |||
| 172 | L<http://www.flymine.org> | ||
| 173 | |||
| 174 | =back | ||
| 175 | |||
| 176 | =head1 COPYRIGHT & LICENSE | ||
| 177 | |||
| 178 | Copyright 2006,2007,2008,2009, 2010, 2011 FlyMine, all rights reserved. | ||
| 179 | |||
| 180 | This program is free software; you can redistribute it and/or modify it | ||
| 181 | under the same terms as Perl itself. | ||
| 182 |