Template.pm

filter: Statementstmt 97.7% 88 / 90 Branchbran 68.1% 15 / 22 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 90.4% 19 / 21 Podpod 80.0% 4 / 5 totaltotal 85.9% 135 / 157 CC 18 SCAR 14.6
CC
18
Cov
89%
_data_dict_files
8.3
data
8.1
render
3.1
CRAP 4.3
« Model.pm ↑ Summary
1 package DBIx::Mojo::Template;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Mojo::Loader qw(data_section);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mojo::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::Util qw(url_unescape b64_decode class_to_path);#
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #~ has debug => $ENV{DEBUG_DBIx_Mojo_Template} || 0;
11 #~ my $pkg = __PACKAGE__;
12  
13 sub new {
14 2 ▶ my ($class) = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 2 bless $class->data(@_);
16 }
17  
18 sub singleton {
19 2 ▶ my ($class) = shift;
Subroutine singleton: called CC=1 SCAR=0.0
Pod documented
20 2 state $singleton = bless {};
21 2 my $data = $class->data(@_);
22 2 @$singleton{ keys %$data } = values %$data;
23 2 $singleton;
24 }
25  
26 sub data {
27 4 ▶ my ($class, $pkg, %arg) = @_;
Subroutine data: called CC=8 SCAR=20.9
Pod documented
28 4 ▶ die "Package not defined!"
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 4 4
29 unless $pkg;
30 4 my $dict = {};
31 4 ▶ my $data = data_section($pkg) || {};
Condition50%1/2
data_section($pkg) || {}
l!l
40
MC/DC0%0/1
data_section($pkg) || {}
data_section($pkg)
Truth table50%1/2
A
A= data_section($pkg)
Aresult
11
00
32 4 ▶ my $extra = $class->_data_dict_files($pkg => @{$arg{data} || []});
Branch: 1 branch
BranchTrueFalseTotal
$arg{"data"} or [] 3 1 4
33 #~ if ref($arg{data}) eq 'ARRAY';#$pkg ne 'main' &&
34 #~ @$data{keys %$extra} = values %$extra
35 #~ if ref($extra) eq 'HASH';
36 #prio: near over far
37 4 @$extra{keys %$data} = values %$data;
38
39 4 ▶ while ( my ($k, $t) = each %$extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$extra) 16 4 20
40 16 my $url = Mojo::URL->new($k);
41 16 my ($name, $param) = (url_unescape($url->path), $url->query->to_hash);
42 16 utf8::decode($name);
43 16 ▶ $dict->{$name} = DBIx::Mojo::Statement->new(dict=>$dict, name=>$name, raw=>$t, param=>$param, mt=>_mt(%{$arg{mt} || {}}), vars=>$arg{vars} || {});
Branch: 1 branch
BranchTrueFalseTotal
$arg{"mt"} or {} 11 5 16
Condition100%2/2
$arg{'vars'} || {}
l!l
511
MC/DC100%1/1
$arg{'vars'} || {}
$arg{'vars'}
Truth table100%2/2
A
A= $arg{'vars'}
Aresult
11
00
44 16 weaken $dict->{$name}->{dict};
45 }
46 4 ▶ die "None DATA dict in package [$pkg]"
Branch: 1 branch
BranchTrueFalseTotal
unless %$dict 0 4 4
47 unless %$dict;
48 4 return $dict;
49 }
50  
51 sub _mt {
52 16 ▶ Mojo::Template->new(vars => 1, prepend=>'no strict qw(vars); no warnings qw(uninitialized);', @_);# line_start=>'$$',
Subroutine _mt: called CC=1 SCAR=0.0
53 }
54  
55 0 ▶sub template { shift->render(@_) }
Subroutine template: not called CC=1 SCAR=6.9
Pod undocumented
56  
57 sub render {
58 10 ▶ my ($self, $key, %arg) = @_;
Subroutine render: called CC=2 SCAR=7.1
Pod documented
59 die "No such item by key [$key] on this DICT, please check processed package"
60 10 ▶ unless $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$key} 0 10 10
61 10 $self->{$key}->render(%arg);
62
63 }
64  
65 # можно задать для модуля доп файлы словаря
66 # $self->_data_dict_files('Foo::Bar'=>'Bar.pm.dict.sql')
67 # на входе модуль и список имен доп файлов ОТНОСИТЕЛЬНО папки модуля
68 # @return hashref dict
69 sub _data_dict_files {
70 4 ▶ my ($self, $pkg, @files) = @_;
Subroutine _data_dict_files: called CC=8 SCAR=21.2
71 #~ require Module::Path;
72 #~ Module::Path->import('module_path');module_path($pkg)
73 4 ▶ my $dir = Mojo::File->new($INC{class_to_path($pkg)} || '.')->dirname;##
Condition100%2/2
$INC{class_to_path($pkg)} || '.'
l!l
31
MC/DC100%1/1
$INC{class_to_path($pkg)} || '.'
$INC{class_to_path($pkg)}
Truth table100%2/2
A
A= $INC{class_to_path($pkg)}
Aresult
11
00
74 4 my $dict = {};
75 4 for my $file (@files) {
76 1 my $path = Mojo::File->new($file);
77 1 ▶ $path = $dir->child($file)
Branch: 1 branch
BranchTrueFalseTotal
unless $path->is_abs 1 0 1
78 unless $path->is_abs;
79 1 ▶ next unless -f $path && -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path and -r _ 0 1 1
Condition33%1/3
-f $path and -r _
!ll&&!rl&&r
001
MC/DC0%0/2
-f $path and -r _
-f $path -r _
Truth table33%1/3
A and B
A= -f $pathB= -r _
ABresult
0X0
100
111
80
81 1 my $data = $path->slurp;
82 1 utf8::decode($data);
83
84 ## copy-paste from Mojo::Loader
85 # Ignore everything before __DATA__ (some versions seek to start of file)
86 1 $data =~ s/^.*\n__DATA__\r?\n/\n/s;
87
88 # Ignore everything after __END__
89 1 $data =~ s/\n__END__\r?\n.*$/\n/s;
90
91 # Split files
92 1 (undef, my @f) = split /^@@\s*(.+?)\s*\r?\n/m, $data;
93
94 # Find data
95 1 ▶ while (@f) {
Branch: 1 branch
BranchTrueFalseTotal
if (@f) 2 1 3
96 2 my ($name, $data) = splice @f, 0, 2;
97 2 ▶ $dict->{$name} = $name =~ s/\s*\(\s*base64\s*\)$// ? b64_decode($data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/\s*\(\s*base64\s*\)$//u ? : 0 2 2
98 }
99 }
100 4 return $dict;
101 }
102  
103 2 our $VERSION = '0.060';
104  
105 #=============================================
106 package DBIx::Mojo::Statement;
107 #=============================================
108 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 2 ▶use Hash::Merge qw(merge);
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 2 has [qw(dict name raw param mt vars sth)];
113 # sth - attr for save cached dbi statement
114  
115 2 ▶use overload '""' => sub { shift->raw };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
116  
117 0 ▶sub template { shift->render(@_) }
Subroutine template: not called CC=1 SCAR=6.9
118  
119 sub render {
120 21 ▶ my $self = shift;
Subroutine render: called CC=3 SCAR=11.3
121 21 ▶ my $vars =ref $_[0] ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 21 21
122 21 my $merge = merge($vars, $self->vars);
123 21 ▶ $merge->{dict} ||= $self->dict;
Condition33%1/3
$merge->{"dict"} ||= $self->dict
l!l&&r!l&&!r
0210
MC/DC0%0/2
$merge->{"dict"} ||= $self->dict
$merge->{"dict"} $self->dict
Truth table33%1/3
A ||= B
A= $merge->{"dict"}B= $self->dict
ABresult
1X1
011
000
124 21 $merge->{DICT} = $self->dict;
125 21 $merge->{st} = $self;
126 21 weaken $merge->{st};
127
128 21 $self->mt->render($self->raw, $merge);#%$vars ? %{$self->vars} ? merge($vars, $self->vars) : $vars : $self->vars
129
130 }
131  
132 =pod
133  
134 =encoding utf8
135  
136 Доброго всем
137  
138 =head1 DBIx::Mojo::Template
139  
140 ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
141  
142 =head1 NAME
143  
144 DBIx::Mojo::Template - Render SQL statements by Mojo::Template
145  
146 =head1 VERSION
147  
148 0.060
149  
150 =head1 SYNOPSIS
151  
152 use DBIx::Mojo::Template;
153  
154 my $dict = DBIx::Mojo::Template->new(__PACKAGE__,mt=>{tag_start=>'%{', tag_end=>'%}',});
155
156 my $sql = $dict->{'foo'}->render(table=>'foo', where=> 'where id=?');
157 # or same
158 my $sql = $dict->render('bar', where=> 'where id=?');
159
160 __DATA__
161 @@ foo?cache=1
162 %# my foo statement with prepare_cached (model sth)
163 select *
164 from {% $table %}
165 {% $where %}
166  
167  
168 =head1 SUBROUTINES/METHODS
169  
170 =head2 new
171  
172 my $dict = DBIx::Mojo::Template->new('Foo::Bar', vars=>{...}, mt=>{...})
173  
174 where arguments:
175  
176 =over 4
177  
178 =item * $pkg (string)
179  
180 Package name, where __DATA__ section SQL dictionary. Package must be loaded (use/require) before!
181  
182 =item * vars (hashref)
183  
184 Hashref of this dict templates variables. Vars can be merged when render - see L<#render>.
185  
186 =item * mt (hashref)
187  
188 For Mojo::Template object attributes. See L<Mojo::Template#ATTRIBUTES>.
189  
190 mt=>{ line_start=>'+', }
191  
192 Defaults <mt> attrs:
193  
194 mt=> {vars => 1, prepend=>'no strict qw(vars); no warnings qw(uninitialized);',}
195  
196 =item * data (arrayref) - optional
197  
198 Define extra data files for dictionary. Absolute or relative to path of the module $pkg file point.
199  
200 =back
201  
202 =head2 singleton
203  
204 Merge ditcs packages to one. Arguments same as L<#new>.
205  
206 DBIx::Mojo::Template->singleton('Foo');
207 my $dict = DBIx::Mojo::Template->singleton('Bar');
208  
209 =head2 render
210  
211 Render template dict key.
212  
213 my $sql = $dict->render($key, var1=>..., var2 => ...,);
214  
215 Each dict item is a object DBIx::Mojo::Statement with one method C<render>:
216  
217 my $sql = $dict->{'key foo'}->render(bar=>'baz', ...);
218  
219 =head2 data
220  
221 Same as L<#new> but returns unblessed hashref dict.
222  
223 =head1 AUTHOR
224  
225 Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >>
226  
227 =head1 BUGS / CONTRIBUTING
228  
229 Please report any bugs or feature requests at L<https://github.com/mche/DBIx-Mojo-Template/issues>. Pull requests also welcome.
230  
231  
232 =head1 LICENSE AND COPYRIGHT
233  
234 Copyright 2016 Михаил Че (Mikhail Che).
235  
236 This module is free software; you can redistribute it and/or modify it under the term of the Perl itself.
237  
238  
239 =cut
240  
241 2 1; # End of DBIx::Mojo::Template
« Model.pm ↑ Summary