Teng/Schema/Declare.pm

filter: Statementstmt 76.8% 73 / 95 Branchbran 27.7% 5 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 15 / 20 Podpod 100% 3 / 3 totaltotal 70.5% 96 / 136 CC 10 SCAR 12.0
CC
10
Cov
69%
_current_schema
7.5
__ANON__
6.0
__ANON__
6.0
CRAP 3.3
« Teng/Plugin/FindOrCreate.pm ↑ Summary Teng/Plugin/Pager/MySQLFoundRows.pm »
1 package Teng::Schema::Declare;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Teng::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Teng::Schema::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our @EXPORT = qw(
9 schema
10 name
11 table
12 pk
13 columns
14 row_class
15 inflate
16 deflate
17 );
18 1 our $CURRENT_SCHEMA_CLASS;
19  
20 sub schema (&;$) {
21 1 ▶ my ($code, $schema_class) = @_;
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
22 1 local $CURRENT_SCHEMA_CLASS = $schema_class;
23 1 $code->();
24 1 _current_schema();
25 }
26  
27 sub row_namespace ($) {
28 1 ▶ my $table_name = shift;
Subroutine row_namespace: called CC=1 SCAR=0.0
Pod documented
29  
30 1 (my $caller = caller(1)) =~ s/::Schema$//;
31 1 join '::', $caller, 'Row', Teng::Schema::camelize($table_name);
32 }
33  
34 sub _current_schema {
35 2 ▶ my $class = __PACKAGE__;
Subroutine _current_schema: called CC=6 SCAR=20.1
36 2 my $schema_class;
37  
38 2 ▶ if ( $CURRENT_SCHEMA_CLASS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Teng::Schema::Declare::CURRENT_SCHEMA_CLASS) { } 2 0 2
39 2 $schema_class = $CURRENT_SCHEMA_CLASS;
40 } else {
41 0 my $i = 1;
42 0 ▶ while ( $schema_class = caller($i++) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_class = caller $i++) 0 0 0
43 0 ▶ if ( ! $schema_class->isa( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_class->isa($class)) 0 0 0
44 0 last;
45 }
46 }
47 }
48  
49 2 ▶ if (! $schema_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_class) 0 2 2
50 0 Carp::confess( "PANIC: cannot find a package name that is not ISA $class" );
51 }
52  
53 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶ if (! $schema_class->isa( 'Teng::Schema' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_class->isa("Teng::Schema")) 1 1 2
55 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 push @{ "$schema_class\::ISA" }, 'Teng::Schema';
57 1 my $schema = $schema_class->new();
58 1 $schema_class->set_default_instance( $schema );
59 }
60  
61 2 $schema_class->instance();
62 }
63  
64 sub pk(@);
65 sub columns(@);
66 sub name ($);
67 sub row_class ($);
68 sub inflate_rule ($@);
69 sub table(&) {
70 1 ▶ my $code = shift;
Subroutine table: called CC=3 SCAR=13.1
Pod documented
71 1 my $current = _current_schema();
72  
73 my (
74 1 $table_name,
75 @table_pk,
76 @table_columns,
77 @inflate,
78 @deflate,
79 $row_class,
80 );
81 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
82
83 1 my $dest_class = caller();
84 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 local *{"$dest_class\::name"} = sub ($) {
87 1 ▶ $table_name = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
88 1 $row_class = row_namespace($table_name);
89 1 };
90 1 ▶ local *{"$dest_class\::pk"} = sub (@) { @table_pk = @_ };
Subroutine __ANON__: not called CC=1 SCAR=0.4
91 1 ▶ local *{"$dest_class\::columns"} = sub (@) { @table_columns = @_ };
Subroutine __ANON__: not called CC=1 SCAR=0.4
92 1 ▶ local *{"$dest_class\::row_class"} = sub (@) { $row_class = shift };
Subroutine __ANON__: not called CC=1 SCAR=0.4
93 1 local *{"$dest_class\::inflate"} = sub ($&) {
94 0 ▶ my ($rule, $code) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
95 0 ▶ if (ref $rule ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule ne "Regexp") 0 0 0
96 0 $rule = qr/^\Q$rule\E$/;
97 }
98 0 push @inflate, ($rule, $code);
99 1 };
100 1 local *{"$dest_class\::deflate"} = sub ($&) {
101 0 ▶ my ($rule, $code) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
102 0 ▶ if (ref $rule ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule ne "Regexp") 0 0 0
103 0 $rule = qr/^\Q$rule\E$/;
104 }
105 0 push @deflate, ($rule, $code);
106 1 };
107  
108 1 $code->();
109  
110 1 my @col_names;
111 my %sql_types;
112 1 ▶ while ( @table_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@table_columns) 0 1 1
113 0 my $col_name = shift @table_columns;
114 0 ▶ if (ref $col_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $col_name) 0 0 0
115 0 my $sql_type = $col_name->{type};
116 0 $col_name = $col_name->{name};
117 0 $sql_types{$col_name} = $sql_type;
118 }
119 0 push @col_names, $col_name;
120 }
121  
122 $current->add_table(
123 1 Teng::Schema::Table->new(
124 columns => \@col_names,
125 name => $table_name,
126 primary_keys => \@table_pk,
127 sql_types => \%sql_types,
128 inflators => \@inflate,
129 deflators => \@deflate,
130 row_class => $row_class,
131 )
132 );
133 }
134  
135 1 1;
136  
137 __END__
138  
139 =head1 NAME
140  
141 Teng::Schema::Declare - DSL For Declaring Teng Schema
142  
143 =head1 NORMAL USE
144  
145 package MyDB::Schema;
146 use strict;
147 use warnings;
148 use Teng::Schema::Declare;
149  
150 table {
151 name "your_table_name";
152 pk "primary_key";
153 columns qw( col1 col2 col3 );
154 inflate 'col1' => sub {
155 my ($col_value) = @_;
156 return MyDB::Class->new(name => $col_value);
157 };
158 deflate 'col1' => sub {
159 my ($col_value) = @_;
160 return ref $col_value ? $col_value->name : $col_value;
161 };
162 row_class 'MyDB::Row'; # optional
163 };
164  
165 =head1 INLINE DECLARATION
166  
167 use Teng::Schema::Declare;
168 my $schema = schema {
169 table {
170 name "your_table_name";
171 columns qw( col1 col2 col3 );
172 };
173 } "MyDB::Schema";
174  
175 =head1 METHODS
176  
177 =over 4
178  
179 =item schema
180  
181 schema data creation wrapper.
182  
183 =item table
184  
185 set table name
186  
187 =item pk
188  
189 set primary key
190  
191 =item columns
192  
193 set columns
194  
195 =item inflate_rule
196  
197 set inflate rule
198  
199 =item row_namespace
200  
201 create Row class namespace
202  
203 =back
204  
205 =cut
206  
« Teng/Plugin/FindOrCreate.pm ↑ Summary Teng/Plugin/Pager/MySQLFoundRows.pm »