Mooify SQLT::Schema::View
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Role / Extra.pm
CommitLineData
5f7fd749 1package SQL::Translator::Schema::Role::Extra;
2use Moo::Role;
3
4
5=head1 Methods
6
7The following methods are defined here, therefore all schema objects
8using this role will have them.
9
10=head2 extra
11
12Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
13Call with no args to get all the extra data.
14Call with a single name arg to get the value of the named extra attribute,
15returned as a scalar. Call with a hash or hashref to set extra attributes.
16Returns 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
27has extra => ( is => 'rwp', default => sub { +{} } );
28
29around 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
50L</extra> can only be used to get or set "extra" attributes but not to
51remove some. Call with no args to remove all extra attributes that
52have been set before. Call with a list of key names to remove
53certain 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
63sub remove_extra {
64 my ( $self, @keys ) = @_;
65 unless (@keys) {
66 $self->_set_extra({});
67 }
68 else {
69 delete @{$self->extra}{@keys};
70 }
71}
72
731;