File Coverage

blib/lib/DBIx/Mojo/Template.pm
Criterion Covered Total %
statement 81 83 97.5
branch 11 18 61.1
condition 7 12 58.3
subroutine 18 20 90.0
pod 4 5 80.0
total 121 138 87.6


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