File Coverage

blib/lib/DBIx/Mojo/Template.pm
Criterion Covered Total %
statement 84 86 97.6
branch 11 18 61.1
condition 7 12 58.3
subroutine 19 21 90.4
pod 4 5 80.0
total 125 142 88.0


line stmt bran cond sub pod time code
1             package DBIx::Mojo::Template;
2 2     2   319745 use Mojo::Base -base;
  2         197434  
  2         21  
3 2     2   1562 use Mojo::Loader qw(data_section);
  2         69646  
  2         136  
4 2     2   1073 use Mojo::Template;
  2         7699  
  2         14  
5 2     2   1069 use Mojo::URL;
  2         15032  
  2         16  
6 2     2   89 use Mojo::Util qw(url_unescape b64_decode class_to_path);#
  2         5  
  2         102  
7 2     2   11 use Mojo::File;
  2         5  
  2         73  
8 2     2   12 use Scalar::Util 'weaken';
  2         6  
  2         1787  
9              
10             #~ has debug => $ENV{DEBUG_DBIx_Mojo_Template} || 0;
11             #~ my $pkg = __PACKAGE__;
12              
13             sub new {
14 2     2 1 2098 my ($class) = shift;
15 2         10 bless $class->data(@_);
16             }
17              
18             sub singleton {
19 2     2 1 2462 my ($class) = shift;
20 2         6 state $singleton = bless {};
21 2         7 my $data = $class->data(@_);
22 2         9 @$singleton{ keys %$data } = values %$data;
23 2         18 $singleton;
24             }
25              
26             sub data {
27 4     4 1 15 my ($class, $pkg, %arg) = @_;
28 4 50       13 die "Package not defined!"
29             unless $pkg;
30 4         9 my $dict = {};
31 4   50     18 my $data = data_section($pkg) || {};
32 4 100       443 my $extra = $class->_data_dict_files($pkg => @{$arg{data} || []});
  4         28  
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         23 @$extra{keys %$data} = values %$data;
38            
39 4         22 while ( my ($k, $t) = each %$extra) {
40 16         565 my $url = Mojo::URL->new($k);
41 16         3130 my ($name, $param) = (url_unescape($url->path), $url->query->to_hash);
42 16         4448 utf8::decode($name);
43 16 100 100     35 $dict->{$name} = DBIx::Mojo::Statement->new(dict=>$dict, name=>$name, raw=>$t, param=>$param, mt=>_mt(%{$arg{mt} || {}}), vars=>$arg{vars} || {});
  16         80  
44             #~ weaken $dict->{$name}->{dict};
45             }
46 4 50       164 die "None DATA dict in package [$pkg]"
47             unless %$dict;
48 4         55 return $dict;
49             }
50              
51             sub _mt {
52 16     16   55 Mojo::Template->new(vars => 1, prepend=>'no strict qw(vars); no warnings qw(uninitialized);', @_);# line_start=>'$$',
53             }
54              
55 0     0 0 0 sub template { shift->render(@_) }
56              
57             sub render {
58 10     10 1 9323 my ($self, $key, %arg) = @_;
59             die "No such item by key [$key] on this DICT, please check processed package"
60 10 50       36 unless $self->{$key};
61 10         53 $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     4   13 my ($self, $pkg, @files) = @_;
71             #~ require Module::Path;
72             #~ Module::Path->import('module_path');module_path($pkg)
73 4   100     18 my $dir = Mojo::File->new($INC{class_to_path($pkg)} || '.')->dirname;##
74 4         348 my $dict = {};
75 4         13 for my $file (@files) {
76 1         4 my $path = Mojo::File->new($file);
77 1 50       7 $path = $dir->child($file)
78             unless $path->is_abs;
79 1 50 33     34 next unless -f $path && -r _;
80            
81 1         45 my $data = $path->slurp;
82 1         124 utf8::decode($data);
83            
84             ## copy-paste from Mojo::Loader
85             # Ignore everything before __DATA__ (some versions seek to start of file)
86 1         3 $data =~ s/^.*\n__DATA__\r?\n/\n/s;
87            
88             # Ignore everything after __END__
89 1         2 $data =~ s/\n__END__\r?\n.*$/\n/s;
90            
91             # Split files
92 1         11 (undef, my @f) = split /^@@\s*(.+?)\s*\r?\n/m, $data;
93            
94             # Find data
95 1         4 while (@f) {
96 2         6 my ($name, $data) = splice @f, 0, 2;
97 2 50       11 $dict->{$name} = $name =~ s/\s*\(\s*base64\s*\)$// ? b64_decode($data) : $data;
98             }
99             }
100 4         10 return $dict;
101             }
102              
103             our $VERSION = '0.061';
104              
105             #=============================================
106             package DBIx::Mojo::Statement;
107             #=============================================
108 2     2   17 use Mojo::Base -base;
  2         5  
  2         11  
109 2     2   1369 use Hash::Merge qw(merge);
  2         19310  
  2         128  
110 2     2   19 use Scalar::Util 'weaken';
  2         6  
  2         173  
111              
112             has [qw(dict sth)], undef, weak=>1;
113             has [qw(name raw param mt vars )];
114              
115             # sth - attr for save cached dbi statement
116              
117 2     2   13 use overload '""' => sub { shift->raw };
  2     22   5  
  2         21  
  22         1906  
118              
119 0     0   0 sub template { shift->render(@_) }
120              
121             sub render {
122 21     21   14642 my $self = shift;
123 21 50       67 my $vars =ref $_[0] ? shift : { @_ };
124 21         56 my $merge = merge($vars, $self->vars);
125 21   33     1566 $merge->{dict} ||= $self->dict;
126 21         157 $merge->{DICT} = $self->dict;
127 21         79 $merge->{st} = $self;
128 21         65 weaken $merge->{st};
129            
130 21         45 $self->mt->render($self->raw, $merge);#%$vars ? %{$self->vars} ? merge($vars, $self->vars) : $vars : $self->vars
131            
132             }
133              
134             =pod
135              
136             =encoding utf8
137              
138             Доброго всем
139              
140             =head1 DBIx::Mojo::Template
141              
142             ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
143              
144             =head1 NAME
145              
146             DBIx::Mojo::Template - Render SQL statements by Mojo::Template
147              
148             =head1 VERSION
149              
150             0.061
151              
152             =head1 SYNOPSIS
153              
154             use DBIx::Mojo::Template;
155              
156             my $dict = DBIx::Mojo::Template->new(__PACKAGE__,mt=>{tag_start=>'%{', tag_end=>'%}',});
157            
158             my $sql = $dict->{'foo'}->render(table=>'foo', where=> 'where id=?');
159             # or same
160             my $sql = $dict->render('bar', where=> 'where id=?');
161            
162             __DATA__
163             @@ foo?cache=1
164             %# my foo statement with prepare_cached (model sth)
165             select *
166             from {% $table %}
167             {% $where %}
168              
169              
170             =head1 SUBROUTINES/METHODS
171              
172             =head2 new
173              
174             my $dict = DBIx::Mojo::Template->new('Foo::Bar', vars=>{...}, mt=>{...})
175              
176             where arguments:
177              
178             =over 4
179              
180             =item * $pkg (string)
181              
182             Package name, where __DATA__ section SQL dictionary. Package must be loaded (use/require) before!
183              
184             =item * vars (hashref)
185              
186             Hashref of this dict templates variables. Vars can be merged when render - see L<#render>.
187              
188             =item * mt (hashref)
189              
190             For Mojo::Template object attributes. See L.
191              
192             mt=>{ line_start=>'+', }
193              
194             Defaults attrs:
195              
196             mt=> {vars => 1, prepend=>'no strict qw(vars); no warnings qw(uninitialized);',}
197              
198             =item * data (arrayref) - optional
199              
200             Define extra data files for dictionary. Absolute or relative to path of the module $pkg file point.
201              
202             =back
203              
204             =head2 singleton
205              
206             Merge ditcs packages to one. Arguments same as L<#new>.
207              
208             DBIx::Mojo::Template->singleton('Foo');
209             my $dict = DBIx::Mojo::Template->singleton('Bar');
210              
211             =head2 render
212              
213             Render template dict key.
214              
215             my $sql = $dict->render($key, var1=>..., var2 => ...,);
216              
217             Each dict item is a object DBIx::Mojo::Statement with one method C:
218              
219             my $sql = $dict->{'key foo'}->render(bar=>'baz', ...);
220              
221             =head2 data
222              
223             Same as L<#new> but returns unblessed hashref dict.
224              
225             =head1 AUTHOR
226              
227             Михаил Че (Mikhail Che), C<< >>
228              
229             =head1 BUGS / CONTRIBUTING
230              
231             Please report any bugs or feature requests at L. Pull requests also welcome.
232              
233              
234             =head1 LICENSE AND COPYRIGHT
235              
236             Copyright 2016 Михаил Че (Mikhail Che).
237              
238             This module is free software; you can redistribute it and/or modify it under the term of the Perl itself.
239              
240              
241             =cut
242              
243             1; # End of DBIx::Mojo::Template