| 1 | package Coat::Persistent::Meta; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use base 'Exporter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | # The placeholder for all meta-information saved for Coat::Persistent models. | ||||||||||||
| 8 | 3 | my $META = {}; | |||||||||||
| 9 | |||||||||||||
| 10 | # supported meta attributes for models | ||||||||||||
| 11 | 3 | my @attributes = qw(table_name primary_key accessor); | |||||||||||
| 12 | |||||||||||||
| 13 | # accessor to the meta information of a model | ||||||||||||
| 14 | # ex: Coat::Persistent::Meta->model('User') | ||||||||||||
| 15 | 0 | ▶ | sub registry { $META->{ $_[1] } } | ||||||||||
|
Subroutine
registry: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 16 | |||||||||||||
| 17 | 0 | ▶ | sub attribute { | ||||||||||
|
Subroutine
attribute: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 18 | 0 | my ($self, $class, $attribute) = @_; | |||||||||||
| 19 | 0 | ▶ | $META->{ $class }{attributes} ||= []; | ||||||||||
|
Condition0%0/2
$META->{$class}{"attributes"} ||= []
MC/DC0%0/1
$META->{$class}{"attributes"} ||= []
$META->{$class}{"attributes"}
Truth table0%0/2
A
A= $META->{$class}{"attributes"}
| |||||||||||||
| 20 | 0 | push @{ $META->{ $class }{'attributes'} }, $attribute; | |||||||||||
| 21 | } | ||||||||||||
| 22 | |||||||||||||
| 23 | 0 | ▶ | sub attribute_exists { | ||||||||||
|
Subroutine
attribute_exists: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 24 | 0 | my ($self, $class, $attribute) = @_; | |||||||||||
| 25 | 0 | return grep /^$attribute$/, @{ $META->{ $class }{'attributes'} }; | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 0 | ▶ | sub attributes { | ||||||||||
|
Subroutine
attributes: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 29 | 0 | my ($self, $class) = @_; | |||||||||||
| 30 | 0 | ▶ | $META->{ $class }{'attributes'} ||= []; | ||||||||||
|
Condition0%0/2
$META->{$class}{"attributes"} ||= []
MC/DC0%0/1
$META->{$class}{"attributes"} ||= []
$META->{$class}{"attributes"}
Truth table0%0/2
A
A= $META->{$class}{"attributes"}
| |||||||||||||
| 31 | 0 | return @{ $META->{ $class }{'attributes'} }; | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | 0 | ▶ | sub linearized_attributes { | ||||||||||
|
Subroutine
linearized_attributes: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 35 | 0 | my ($self, $class) = @_; | |||||||||||
| 36 | |||||||||||||
| 37 | 0 | my @all = (); | |||||||||||
| 38 | 0 | foreach my $c (reverse Coat::Meta->linearized_isa( $class ) ) { | |||||||||||
| 39 | 0 | foreach my $attr (Coat::Persistent::Meta->attributes( $c )) { | |||||||||||
| 40 | 0 | ▶ | push(@all, $attr) unless (grep(/^$attr$/, @all)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | } | ||||||||||||
| 42 | } | ||||||||||||
| 43 | 0 | return @all; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | # this is to avoid writing several times the same setters and | ||||||||||||
| 47 | # writers for the class | ||||||||||||
| 48 | # (closures are the hidden gold behind Perl!) | ||||||||||||
| 49 | # Examples: | ||||||||||||
| 50 | # - set the table name for a model | ||||||||||||
| 51 | # Coat::Persistent::Meta->table_name('User', 'users'); | ||||||||||||
| 52 | # - get the primary_key | ||||||||||||
| 53 | # Coat::Persistent::Meta->primary_key('User'); | ||||||||||||
| 54 | # | ||||||||||||
| 55 | 9 | ▶ | sub _create_model_accessor { | ||||||||||
|
Subroutine
_create_model_accessor: called CC=1 SCAR=0.2
| |||||||||||||
| 56 | 9 | my ($attribute) = @_; | |||||||||||
| 57 | |||||||||||||
| 58 | 0 | ▶ | my $sub_class_accessor = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 59 | 0 | my ($self, $model, $value) = @_; | |||||||||||
| 60 | (@_ == 2) | ||||||||||||
| 61 | ? return $META->{$model}{$attribute} | ||||||||||||
| 62 | 0 | ▶ | : return $META->{$model}{$attribute} = $value; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 9 | }; | |||||||||||
| 64 | |||||||||||||
| 65 | # the real magic occurs now! | ||||||||||||
| 66 | 9 | my $symbol = "Coat::Persistent::Meta::${attribute}"; | |||||||||||
| 67 | { | ||||||||||||
| 68 | 3 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 69 | 3 | ▶ | no warnings 'redefine'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 70 | 9 | *$symbol = $sub_class_accessor; | |||||||||||
| 71 | } | ||||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | # When the package is imported, define the symbols | ||||||||||||
| 75 | 3 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||
| 76 | 3 | _create_model_accessor($_) for @attributes; | |||||||||||
| 77 | 3 | __PACKAGE__->export_to_level( 1, @_ ); | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 3 | 1; | |||||||||||
| 81 | __END__ | ||||||||||||