File Coverage

blib/lib/DBIx/NoSQL/ClassScaffold.pm
Criterion Covered Total %
statement 64 66 96.9
branch n/a
condition n/a
subroutine 19 20 95.0
pod 1 6 16.6
total 84 92 91.3


line stmt bran cond sub pod time code
1             package DBIx::NoSQL::ClassScaffold;
2             our $AUTHORITY = 'cpan:YANICK';
3             # Scaffold
4             $DBIx::NoSQL::ClassScaffold::VERSION = '0.0021';
5 9     9   88729 use strict;
  9         10  
  9         255  
6 9     9   31 use warnings;
  9         10  
  9         521  
7              
8             my $__serial__ = -1;
9             our $serial = sub {
10             return $__serial__ += 1;
11             };
12              
13 9     9   506 use Moose;
  9         329965  
  9         53  
14              
15             has package => qw/ is ro lazy_build 1 /;
16             sub _build_package {
17 31     31   95 return 'DBIx::NoSQL::__Anonymous__::Class' . $serial->();
18             }
19              
20             has package_meta => qw/ is ro lazy_build 1 /;
21             sub _build_package_meta {
22 11     11   16 my $self = shift;
23 11         276 return Moose::Meta::Class->create($self->package);
24             }
25              
26             sub push_ISA {
27 21     21 0 35 my $self = shift;
28 21         36 my $target = shift;
29              
30 21         557 my $package = $self->package;
31 21         1746 eval "push \@${package}::ISA, '$target'";
32 21         74 return $self;
33             }
34              
35             sub become_Schema {
36 10     10 0 2156 my $self = shift;
37              
38 10         57 require DBIx::Class::Schema;
39 10         269 $self->package_meta->superclasses( 'DBIx::NoSQL::ClassScaffold::Schema' );
40 10         8664 return $self;
41             }
42              
43             sub become_ResultClass {
44 21     21 0 2613 my $self = shift;
45              
46 21         130 require DBIx::Class::Core;
47 21         81 $self->push_ISA( 'DBIx::NoSQL::ClassScaffold::ResultClass' );
48 21         320 return $self;
49             }
50              
51             sub become_ResultClass_Store {
52 10     10 0 2073 my $self = shift;
53              
54 10         36 $self->become_ResultClass;
55 10         274 my $package = $self->package;
56              
57 10         376 $package->table( '__Store__' );
58 10         8578 $package->add_columns(
59             __model__ => {
60             data_type => 'text',
61             },
62             __key__ => {
63             data_type => 'text',
64             },
65             __value__ => {
66             data_type => 'text',
67             default_value => '{}',
68             },
69             );
70 10         11942 $package->set_primary_key(qw/ __model__ __key__ /);
71 10         7400 return $self;
72             }
73              
74             package DBIx::NoSQL::ClassScaffold::Schema;
75             our $AUTHORITY = 'cpan:YANICK';
76             $DBIx::NoSQL::ClassScaffold::Schema::VERSION = '0.0021';
77 9     9   43523 use Moose;
  9         13  
  9         33  
78              
79             extends qw/ DBIx::Class::Schema /;
80              
81 9     9   40174 use JSON; our $json = JSON->new->pretty;
  9         8389  
  9         59  
82 9     9   1880 use Digest::SHA qw/ sha1_hex /;
  9         2027  
  9         3439  
83              
84             has store => qw/ is rw weak_ref 1 /;
85              
86             has deployment_statements => qw/ accessor _deployment_statements lazy_build 1 /;
87             sub _build_deployment_statements {
88 8     8   41 return shift->build_deployment_statements;
89             }
90              
91             sub build_deployment_statements {
92 25     25 0 41 my $self = shift;
93 25         181 my $sql = $self->deployment_statements( undef, undef, undef, { add_drop_table => 1 } );
94 25         2721671 $sql =~ s/^--[^\n]*$//gsm;
95 25         375 return $sql;
96             }
97              
98             around deployment_statements => sub {
99             my $inner = shift;
100             my $self = shift;
101             return $inner->( $self, @_ ) if @_;
102             return $self->_deployment_statements;
103             };
104              
105             has version => qw/ is ro lazy_build 1 /;
106             sub _build_version {
107 0     0   0 my $self = shift;
108 0         0 return sha1_hex( $self->sql );
109             }
110              
111             sub deploy {
112 8     8 1 14 my $self = shift;
113              
114 8         71 my $deployment_statements = $self->deployment_statements;
115 8         195 s/^\s*//, s/\s*$// for $deployment_statements;
116 8         56 my @deployment_statements = split m/;\n\s*/, $deployment_statements;
117              
118 8         229 $self->store->storage->do( $_ ) for @deployment_statements;
119             }
120              
121             package DBIx::NoSQL::ClassScaffold::ResultClass;
122             our $AUTHORITY = 'cpan:YANICK';
123             $DBIx::NoSQL::ClassScaffold::ResultClass::VERSION = '0.0021';
124 9     9   79 use strict;
  9         21  
  9         205  
125 9     9   35 use warnings;
  9         15  
  9         286  
126              
127 9     9   33 use base qw/ DBIx::Class::Core /;
  9         11  
  9         4834  
128              
129             sub register {
130 10     10   289 my $class = shift;
131 10         18 my $schema_class = shift;
132 10         17 my $moniker = shift;
133              
134 10         93 $schema_class->register_class( $moniker => $class );
135             }
136              
137             1;
138              
139             __END__
140              
141             =pod
142              
143             =encoding UTF-8
144              
145             =head1 NAME
146              
147             DBIx::NoSQL::ClassScaffold
148              
149             =head1 VERSION
150              
151             version 0.0021
152              
153             =head1 AUTHORS
154              
155             =over 4
156              
157             =item *
158              
159             Robert Krimen <robertkrimen@gmail.com>
160              
161             =item *
162              
163             Yanick Champoux <yanick@cpan.org>
164              
165             =back
166              
167             =head1 COPYRIGHT AND LICENSE
168              
169             This software is copyright (c) 2017 by Robert Krimen.
170              
171             This is free software; you can redistribute it and/or modify it under
172             the same terms as the Perl 5 programming language system itself.
173              
174             =cut