| 1 | package DBIx::Class::PseudoEnum; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | our $VERSION = '1.0003'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | our $AUTHORITY = 'cpan:GEEKRUTH'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Schema-based enumerations independent of database | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use parent 'DBIx::Class::Row'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use DBIx::Class::Candy::Exports; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Sub::Quote qw(quote_sub); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # Exports for DBIx::Class::Candy | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | export_methods [qw/enumerations_use_column_names enumerate/]; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | # New public methods | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 16 | ▶ | sub enumerate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enumerate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 16 | my ( $class, $column, $values ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 16 | my $info = $class->source_info(); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 16 | ▶ | $info->{enumerations} //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$info->{"enumerations"} //= {}
MC/DC100%1/1
$info->{"enumerations"} //= {}
$info->{"enumerations"}
Truth table100%2/2
A
A= $info->{"enumerations"}
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 16 | $info->{enumerations}->{$column} = $values; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 16 | $class->source_info($info); | |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | sub enumerations_use_column_names { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enumerations_use_column_names: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | my $info = $class->source_info(); | |||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | $info->{enumerations} //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"enumerations"} //= {}
MC/DC0%0/1
$info->{"enumerations"} //= {}
$info->{"enumerations"}
Truth table50%1/2
A
A= $info->{"enumerations"}
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 4 | $info->{enumerations}->{__use_column_names} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | $class->source_info($info); | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # Overriden methods | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 46 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 46 | my ( $class, $attrs ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 46 | my $new = $class->next::method($attrs); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 46 | $new->_register_enumeration_methods(); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 46 | return $new; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 46 | ▶ | sub insert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 46 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 46 | my %data = $self->get_columns(); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 46 | my $info = $self->source_info()->{enumerations}; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 46 | foreach my $data_key ( keys %$info ) { | |||||||||||||||||||||||||||||||||||||||||
| 46 | # undef values will fall through and be captured by other | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # parts of the schema, if they're not allowed. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 84 | ▶ | next if !defined $data{$data_key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 70 | ▶ | if ( !grep { $_ eq $data{$data_key} } @{ $info->{$data_key} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | $self->throw_exception( | |||||||||||||||||||||||||||||||||||||||||
| 51 | "You have attempted to assign a value to $data_key that is not valid: $data{$data_key}"); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 44 | return $self->next::method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 8 | ▶ | sub set_column { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_column: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 8 | my ($self, $column_name, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | my $values = $self->source_info()->{enumerations}->{$column_name}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | # $self->discard_changes(); # But I don't really wanna do this. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # TODO *if* we have dirty columns, and *if* a column is enumerated, we need to check here, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # and...I dunno, put in the old value from storage? | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 8 | ▶ | if (defined $values && defined $value && !grep { $value eq $_ } @$values) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $values and defined $value and not grep({$value eq $_;} @$values)
defined $values and defined $value
MC/DC100%3/3
defined $values and defined $value and not grep({$value eq $_;} @$values)
defined $values
defined $value
not grep({$value eq $_;} @$values)
Truth table100%4/4
A and B and not(C)
A= defined $valuesB= defined $valueC= not grep({$value eq $_;} @$values)
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | $self->throw_exception( | |||||||||||||||||||||||||||||||||||||||||
| 66 | "You have attempted to assign a value to $column_name that is not valid: $value"); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | return shift->next::method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # Internal methods | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 46 | ▶ | sub _register_enumeration_methods { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_register_enumeration_methods: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 46 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 46 | my $classname = ref $class; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 46 | my $rs = $class->result_source->resultset; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 46 | my $rs_classname = ref $rs; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 46 | my $enumerations = $class->source_info->{enumerations}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 46 | my $use_column_names = defined $enumerations->{__use_column_names}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 46 | foreach my $column ( keys %$enumerations ) { | |||||||||||||||||||||||||||||||||||||||||
| 80 | 85 | ▶ | next if $column =~ /^__/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 75 | my $header = "$classname\:\:"; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 75 | my $rs_header = "$rs_classname\:\:"; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 75 | ▶ | if ($use_column_names) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 20 | $header = "$classname\:\:${column}_"; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 20 | $rs_header = "$rs_classname\:\:${column}_"; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 75 | foreach my $value ( @{ $enumerations->{$column} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 88 | 264 | my $term = lc $value; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 264 | $term =~ s/[[:punct:][:space:]]/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 264 | quote_sub "${header}is_$term", qq/ | |||||||||||||||||||||||||||||||||||||||||
| 91 | my \$class=shift; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | return 0 if !\$class->$column; | ||||||||||||||||||||||||||||||||||||||||||
| 93 | return \$class->$column eq '$value'; | ||||||||||||||||||||||||||||||||||||||||||
| 94 | /, { '$rel' => \$column, '$column' => \$value }; | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 264 | quote_sub "${rs_header}is_$term", qq/ | |||||||||||||||||||||||||||||||||||||||||
| 96 | my \$class=shift; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | return \$class->search({ $column => '$value'}); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | /, { '$rel' => \$column, '$column' => \$value }; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||||||||||||||||