Mooify SQLT::Schema
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Role / Extra.pm
1 package SQL::Translator::Schema::Role::Extra;
2 use Moo::Role;
3
4
5 =head1 Methods
6
7 The following methods are defined here, therefore all schema objects
8 using this role will have them.
9
10 =head2 extra
11
12 Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
13 Call with no args to get all the extra data.
14 Call with a single name arg to get the value of the named extra attribute,
15 returned as a scalar. Call with a hash or hashref to set extra attributes.
16 Returns a hash or a hashref.
17
18   $field->extra( qualifier => 'ZEROFILL' );
19
20   $qualifier = $field->extra('qualifier');
21
22   %extra = $field->extra;
23   $extra = $field->extra;
24
25 =cut
26
27 has extra => ( is => 'rwp', default => sub { +{} } );
28
29 around extra => sub {
30     my ($orig, $self) = (shift, shift);
31
32     @_ = %{$_[0]} if ref $_[0] eq "HASH";
33     my $extra = $self->$orig;
34
35     if (@_==1) {
36         return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
37     }
38     elsif (@_) {
39         my %args = @_;
40         while ( my ( $key, $value ) = each %args ) {
41             $extra->{$key} = $value;
42         }
43     }
44
45     return wantarray ? %$extra : $extra;
46 };
47
48 =head2 remove_extra
49
50 L</extra> can only be used to get or set "extra" attributes but not to
51 remove some. Call with no args to remove all extra attributes that
52 have been set before. Call with a list of key names to remove
53 certain extra attributes only.
54
55   # remove all extra attributes
56   $field->remove_extra();
57
58   # remove timezone and locale attributes only
59   $field->remove_extra(qw/timezone locale/);
60
61 =cut
62
63 sub remove_extra {
64     my ( $self, @keys ) = @_;
65     unless (@keys) {
66         $self->_set_extra({});
67     }
68     else {
69         delete @{$self->extra}{@keys};
70     }
71 }
72
73 1;