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