| 1 | package InterMine::Model::Attribute; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | InterMine::Model::Attribute - represents an attribute of an InterMine class | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use InterMine::Model::Attribute; | ||
| 10 | |||
| 11 | ... | ||
| 12 | my $field = InterMine::Model::Attribute->new(name => 'age', model => $model, | ||
| 13 | type => 'Integer'); | ||
| 14 | |||
| 15 | ... | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | Objects of this class describe the attributes of class in an InterMine model. | ||
| 20 | Attribute objects are generally part of ClassDescriptor objects. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | use Moose; | ||
| 25 | with ( | ||
| 26 | 'InterMine::Model::Role::Field', | ||
| 27 | 'InterMine::Model::Role::Descriptor', | ||
| 28 | ); | ||
| 29 | |||
| 30 | use MooseX::Types::Moose qw(Str Int Num Bool Value); | ||
| 31 | use InterMine::Model::Types qw(BigInt ISO8601DateStamp); | ||
| 32 | |||
| 33 | =head1 ATTRIBUTES | ||
| 34 | |||
| 35 | =head2 type (Str) | ||
| 36 | |||
| 37 | =head3 reader: java_type | ||
| 38 | |||
| 39 | The java type of the attribute (Integer, java.lang.String, etc...) | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | has type => ( | ||
| 44 | reader => 'java_type', | ||
| 45 | isa => Str, | ||
| 46 | required => 1, | ||
| 47 | ); | ||
| 48 | |||
| 49 | =head2 attribute_type | ||
| 50 | |||
| 51 | The java_type of the attribute, stripped of any dotted bits | ||
| 52 | (Date, String, int, etc) | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub attribute_type { | ||
| 57 | my $self = shift; | ||
| 58 | my $value = $self->java_type; | ||
| 59 | $value =~ s/.*\.//; | ||
| 60 | return $value; | ||
| 61 | } | ||
| 62 | |||
| 63 | my %moose_translation_for = ( | ||
| 64 | string => Str, | ||
| 65 | short => Int, | ||
| 66 | integer => Int, | ||
| 67 | int => Int, | ||
| 68 | long => BigInt, | ||
| 69 | double => Num, | ||
| 70 | float => Num, | ||
| 71 | boolean => Bool, | ||
| 72 | date => ISO8601DateStamp, | ||
| 73 | ); | ||
| 74 | |||
| 75 | sub _get_moose_type { | ||
| 76 | my $self = shift; | ||
| 77 | my $type = lc($self->attribute_type); | ||
| 78 | if (exists $moose_translation_for{$type}) { | ||
| 79 | return $moose_translation_for{$type}; | ||
| 80 | } else { | ||
| 81 | return Value; # The broadest possible scalar type | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _get_moose_options { | ||
| 86 | my $self = shift; | ||
| 87 | my @options = (isa => $self->_get_moose_type); | ||
| 88 | if ($self->attribute_type =~ /^(?:long|boolean)$/i) { | ||
| 89 | push @options, (coerce => 1); | ||
| 90 | } | ||
| 91 | return @options; | ||
| 92 | } | ||
| 93 | |||
| 94 | =head1 METHODS | ||
| 95 | |||
| 96 | =head2 to_xml | ||
| 97 | |||
| 98 | The xml representation of the attribute descriptor | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub to_xml { | ||
| 103 | my $self = shift; | ||
| 104 | return sprintf(qq{<attribute name="%s" type="%s"/>}, | ||
| 105 | $self->name, $self->java_type); | ||
| 106 | } | ||
| 107 | |||
| 108 | __PACKAGE__->meta->make_immutable; | ||
| 109 | no Moose; | ||
| 110 | 1; | ||
| 111 | |||
| 112 | __END__ | ||
| 113 | |||
| 114 | =head1 SEE ALSO | ||
| 115 | |||
| 116 | =over 4 | ||
| 117 | |||
| 118 | =item L<InterMine::Model::ClassDescriptor> | ||
| 119 | |||
| 120 | =item L<InterMine::Model::Role::Field> | ||
| 121 | |||
| 122 | =item L<InterMine::Model::Role::Descriptor> | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | =head1 AUTHOR | ||
| 127 | |||
| 128 | FlyMine C<< <support@flymine.org> >> | ||
| 129 | |||
| 130 | =head1 BUGS | ||
| 131 | |||
| 132 | Please report any bugs or feature requests to C<support@flymine.org>. | ||
| 133 | |||
| 134 | =head1 SUPPORT | ||
| 135 | |||
| 136 | You can find documentation for this module with the perldoc command. | ||
| 137 | |||
| 138 | perldoc InterMine::Model::Attribute | ||
| 139 | |||
| 140 | You can also look for information at: | ||
| 141 | |||
| 142 | =over 4 | ||
| 143 | |||
| 144 | =item * FlyMine | ||
| 145 | |||
| 146 | L<http://www.flymine.org> | ||
| 147 | |||
| 148 | =back | ||
| 149 | |||
| 150 | =head1 COPYRIGHT & LICENSE | ||
| 151 | |||
| 152 | Copyright 2006,2007,2008,2009,2010,2011 FlyMine, all rights reserved. | ||
| 153 | |||
| 154 | This program is free software; you can redistribute it and/or modify it | ||
| 155 | under the same terms as Perl itself. |