| 1 | package SQL::Translator::Schema::Role::Extra; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | SQL::Translator::Schema::Role::Extra - "extra" attribute for schema classes | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||
| 8 | |||||||||||||||
| 9 | package Foo; | ||||||||||||||
| 10 | use Moo; | ||||||||||||||
| 11 | with qw(SQL::Translator::Schema::Role::Extra); | ||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||
| 14 | |||||||||||||||
| 15 | This role provides methods to set and get a hashref of extra attributes | ||||||||||||||
| 16 | for schema objects. | ||||||||||||||
| 17 | |||||||||||||||
| 18 | =cut | ||||||||||||||
| 19 | |||||||||||||||
| 20 | 77 | ▶ | use Moo::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 77 | ▶ | use Sub::Quote qw(quote_sub); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||
| 24 | |||||||||||||||
| 25 | =head2 extra | ||||||||||||||
| 26 | |||||||||||||||
| 27 | Get or set the objects "extra" attributes (e.g., "ZEROFILL" for MySQL fields). | ||||||||||||||
| 28 | Call with no args to get all the extra data. | ||||||||||||||
| 29 | Call with a single name arg to get the value of the named extra attribute, | ||||||||||||||
| 30 | returned as a scalar. Call with a hash or hashref to set extra attributes. | ||||||||||||||
| 31 | Returns a hash or a hashref. | ||||||||||||||
| 32 | |||||||||||||||
| 33 | $field->extra( qualifier => 'ZEROFILL' ); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | $qualifier = $field->extra('qualifier'); | ||||||||||||||
| 36 | |||||||||||||||
| 37 | %extra = $field->extra; | ||||||||||||||
| 38 | $extra = $field->extra; | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =cut | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 77 | has extra => (is => 'rwp', default => quote_sub(q{ +{} })); | |||||||||||||
| 43 | |||||||||||||||
| 44 | 5027 | ▶ | around extra => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||
| 45 | 5027 | my ($orig, $self) = (shift, shift); | |||||||||||||
| 46 | |||||||||||||||
| 47 | 5027 | ▶ | @_ = %{ $_[0] } if ref $_[0] eq "HASH"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | 5027 | my $extra = $self->$orig; | |||||||||||||
| 49 | |||||||||||||||
| 50 | 5027 | ▶ | if (@_ == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 51 | 278 | return $extra->{ $_[0] }; | |||||||||||||
| 52 | } elsif (@_) { | ||||||||||||||
| 53 | 41 | my %args = @_; | |||||||||||||
| 54 | 41 | ▶ | while (my ($key, $value) = each %args) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 75 | $extra->{$key} = $value; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 4749 | ▶ | return wantarray ? %$extra : $extra; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 77 | }; | |||||||||||||
| 61 | |||||||||||||||
| 62 | =head2 remove_extra | ||||||||||||||
| 63 | |||||||||||||||
| 64 | L</extra> can only be used to get or set "extra" attributes but not to | ||||||||||||||
| 65 | remove some. Call with no args to remove all extra attributes that | ||||||||||||||
| 66 | have been set before. Call with a list of key names to remove | ||||||||||||||
| 67 | certain extra attributes only. | ||||||||||||||
| 68 | |||||||||||||||
| 69 | # remove all extra attributes | ||||||||||||||
| 70 | $field->remove_extra(); | ||||||||||||||
| 71 | |||||||||||||||
| 72 | # remove timezone and locale attributes only | ||||||||||||||
| 73 | $field->remove_extra(qw/timezone locale/); | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =cut | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 4 | ▶ | sub remove_extra { | ||||||||||||
|
Subroutine
remove_extra: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 78 | 4 | my ($self, @keys) = @_; | |||||||||||||
| 79 | 4 | ▶ | unless (@keys) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 2 | $self->_set_extra({}); | |||||||||||||
| 81 | } else { | ||||||||||||||
| 82 | 2 | delete @{ $self->extra }{@keys}; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 77 | 1; | |||||||||||||