| 1 | package InterMine::Model::Role::Field; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | InterMine::Model::Role::Field - Provides common behaviour for field descriptors | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | =head1 AUTHOR | ||
| 10 | |||
| 11 | FlyMine C<< <support@flymine.org> >> | ||
| 12 | |||
| 13 | =head1 BUGS | ||
| 14 | |||
| 15 | Please report any bugs or feature requests to C<support@flymine.org>. | ||
| 16 | |||
| 17 | =head1 SUPPORT | ||
| 18 | |||
| 19 | You can find documentation for this module with the perldoc command. | ||
| 20 | |||
| 21 | perldoc InterMine::Model::Role::Field | ||
| 22 | |||
| 23 | You can also look for information at: | ||
| 24 | |||
| 25 | =over 4 | ||
| 26 | |||
| 27 | =item * FlyMine | ||
| 28 | |||
| 29 | L<http://www.flymine.org> | ||
| 30 | |||
| 31 | =back | ||
| 32 | |||
| 33 | =head1 COPYRIGHT & LICENSE | ||
| 34 | |||
| 35 | Copyright 2006,2007,2008,2009 FlyMine, all rights reserved. | ||
| 36 | |||
| 37 | This program is free software; you can redistribute it and/or modify it | ||
| 38 | under the same terms as Perl itself. | ||
| 39 | |||
| 40 | =head1 FUNCTIONS | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | =head2 new | ||
| 45 | |||
| 46 | Usage : this is an abstract class, construct an Attribute, Collection or | ||
| 47 | Reference instead | ||
| 48 | Function: create a new Field object | ||
| 49 | Args : args are passed in as: name => "value" | ||
| 50 | name - the field name | ||
| 51 | model - the Model | ||
| 52 | type - for attributes, the type of the field (eg. String, Integer) | ||
| 53 | referenced_type_name - for references and collections, the type of | ||
| 54 | the referenced object(s) | ||
| 55 | reverse_reference_name - for references and collections, the field | ||
| 56 | name of the reverse reference | ||
| 57 | |||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | |||
| 62 | use Moose::Role; | ||
| 63 | |||
| 64 | use InterMine::Model::Types qw(ClassDescriptor); | ||
| 65 | use MooseX::Types::Moose qw(Str); | ||
| 66 | use Scalar::Util qw(refaddr); | ||
| 67 | |||
| 68 | requires '_get_moose_type'; | ||
| 69 | |||
| 70 | =head2 name (Str, ro, required) | ||
| 71 | |||
| 72 | The name of the descriptor | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub name; | ||
| 77 | |||
| 78 | has name => ( | ||
| 79 | is => 'ro', | ||
| 80 | isa => Str, | ||
| 81 | required => 1, | ||
| 82 | ); | ||
| 83 | |||
| 84 | =head2 field_class | ||
| 85 | |||
| 86 | Usage : my $class = $field->field_class(); | ||
| 87 | Function: returns the ClassDescriptor of the (base) class that defines this | ||
| 88 | field | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | has field_class => ( | ||
| 93 | is => 'rw', | ||
| 94 | isa => ClassDescriptor, | ||
| 95 | weak_ref => 1, | ||
| 96 | handles => { | ||
| 97 | class_name => 'unqualified_name', | ||
| 98 | }, | ||
| 99 | ); | ||
| 100 | |||
| 101 | sub _get_moose_options { | ||
| 102 | my $self = shift; | ||
| 103 | return (isa => $self->_get_moose_type); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _type_is { | ||
| 107 | my $self = shift; | ||
| 108 | my $something = shift; | ||
| 109 | # if (refaddr($something)) { | ||
| 110 | # return ( | ||
| 111 | # refaddr($self->_get_moose_type) | ||
| 112 | # && | ||
| 113 | # refaddr($something) == refaddr($self->_get_moose_type) | ||
| 114 | # ); | ||
| 115 | # } else { | ||
| 116 | return ($something eq $self->_get_moose_type); | ||
| 117 | # } | ||
| 118 | } | ||
| 119 | |||
| 120 | =head1 METHODS | ||
| 121 | |||
| 122 | =head2 to_string | ||
| 123 | |||
| 124 | The string representation of a field descriptor, ie. its name | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub to_string { | ||
| 129 | my $self = shift; | ||
| 130 | return $self->name; | ||
| 131 | } | ||
| 132 | |||
| 133 | no Moose; | ||
| 134 | |||
| 135 | 1; |