blib/lib/Gentoo/MetaEbuild/Spec/Base.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 146 SCAR 34.0
CC
5
Cov
0%
_build__spec_dir
12.0
_opt_check
12.0
_build__decoder
2.0
CRAP 30.0
1 use 5.008; # utf8
2 use strict;
3 use warnings;
4 use utf8;
5  
6 package Gentoo::MetaEbuild::Spec::Base;
7  
8 our $VERSION = '1.000001';
9  
10 # ABSTRACT: A Base Class for Gentoo MetaEbuild Specifications.
11  
12 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
13  
14 use Moose;
15 use MooseX::ClassAttribute qw( class_has );
16  
17 use File::ShareDir qw( module_dir );
18 use Path::Tiny qw( path );
19 use MooseX::Types::Moose qw( Str CodeRef );
20 use MooseX::Types::Perl qw( VersionObject );
21 use MooseX::Types::Path::Tiny qw( AbsPath AbsDir );
22 use Scalar::Util qw( blessed );
23 use MooseX::Has::Sugar qw( ro lazy_build rw coerce lazy );
24 use version;
25  
26 use namespace::autoclean;
27  
28 class_has '_decoder' => (
29 isa => CodeRef,
30 ro, lazy_build,
31 traits => [qw( Code )],
32 handles => { _decode => 'execute', },
33 );
34  
35 sub _build__decoder {
36 require JSON::MaybeXS;
37 my $decoder = JSON::MaybeXS->new()->utf8(1)->relaxed(1);
38 return sub {
39 $decoder->decode(shift);
40 };
41 }
42  
43 class_has '_spec_dir' => (
44 isa => AbsDir,
45 rw, lazy_build,
46 );
47  
48 sub _build__spec_dir {
49 my ($self) = shift;
50 my $classname;
51 if ( ref $self && blessed $self ) {
52 $classname = blessed $self;
53 }
54 elsif ( ref $self ) {
55 require Carp;
56 ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
57 Carp::confess q{$_[0] is not a Class/Object};
58 }
59 else {
60 $classname = $self;
61 }
62 return path( module_dir($classname) );
63 }
64  
65 class_has '_version' => (
66 isa => VersionObject,
67 coerce, ro, lazy, default => sub { q{0.1.0} },
68 );
69  
70 class_has '_extension' => (
71 isa => Str,
72 ro, lazy, default => sub { q{.json} },
73 );
74  
75 class_has '_schema_creator' => (
76 isa => CodeRef,
77 ro, lazy_build,
78 traits => [qw( Code )],
79 handles => { _make_schema => 'execute', },
80 );
81  
82 sub _build__schema_creator {
83 require Data::Rx;
84 my $rx = Data::Rx->new();
85 return sub {
86 $rx->make_schema(shift);
87 };
88 }
89  
90 sub _opt_check {
91 my ( $self, $opts ) = @_;
92 if ( not exists $opts->{version} ) {
93 $opts->{version} = $self->_version;
94 }
95 elsif ( blessed $opts->{version} ) {
96  
97 }
98 else {
99 $opts->{version} = version->parse( $opts->{version} );
100 }
101 return $opts;
102 }
103  
104 sub _spec_file {
105 my ( $self, $opts ) = @_;
106 $opts = $self->_opt_check($opts);
107 return $self->_spec_dir->child( $opts->{version}->normal . $self->_extension );
108 }
109  
110 sub _spec_data {
111 my ( $self, $opts ) = @_;
112 $opts = $self->_opt_check($opts);
113 return $self->_decode( scalar $self->_spec_file($opts)->slurp() );
114 }
115  
116 sub _schema {
117 my ( $self, $opts ) = @_;
118 $opts = $self->_opt_check($opts);
119 return $self->_make_schema( $self->_spec_data($opts) );
120 }
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132 sub check {
133 my ( $self, $json_data, $opts ) = @_;
134 $opts = $self->_opt_check($opts);
135 return $self->_schema($opts)->check($json_data);
136 }
137  
138 __PACKAGE__->meta->make_immutable;
139 no Moose;
140  
141 1;
142  
143 __END__
144  
145 =pod
146  
147 =encoding UTF-8
148  
149 =head1 NAME
150  
151 Gentoo::MetaEbuild::Spec::Base - A Base Class for Gentoo MetaEbuild Specifications.
152  
153 =head1 VERSION
154  
155 version 1.000001
156  
157 =head1 SYNOPSIS
158  
159 use Gentoo::MetaEbuild::Spec::Base; # or some derived class
160 Gentoo::MetaEbuild::Spec::Base->check( $datastructure );
161  
162 This base-class only validates the most basic of basic, that the data is a { } using Data::Rx
163 and using the shipped File::ShareDir v1.0.0.json spec to do that.
164  
165 This will be more practical in consuming classes as they'll override selected methods/ship different spec files,
166 but maintain the same useful interface.
167  
168 =head1 METHODS
169  
170 =head2 check
171  
172 Packagename->check( $datastructure );
173  
174 Packagename->check( $datastructure, \%opts );
175  
176 Packagename->check( $datastructure, { version => '0.1.0' });
177  
178 =head1 EXTENDING
179  
180 Extending should be this simple:
181  
182 package FooBarBaz;
183 use Moose;
184 extends 'Gentoo::MetaEbuild::Spec::Base';
185  
186 1;
187  
188 and then ship a directory of Data::Rx spec files as the Module ShareDir for that module.
189  
190 =head1 TESTING
191  
192 The only fun thing with testing is the File::ShareDir directory hasn't been installed yet, but its simple to get around.
193  
194 use FindBin;
195 use Path::Tiny qw( path );
196 use Gentoo::MetaEbuild::Spec::Base;
197  
198 Gentoo::MetaEbuild::Spec::Base->_spec_dir(
199 path($FindBin::Bin)->parent->child('share')
200 );
201  
202 # Code as per usual.
203  
204 my $shareroot = path($FindBin::Bin)->parent();
205  
206 =head1 AUTHOR
207  
208 Kent Fredric <kentnl@cpan.org>
209  
210 =head1 COPYRIGHT AND LICENSE
211  
212 This software is copyright (c) 2014 by Kent Fredric <kentnl@cpan.org>.
213  
214 This is free software; you can redistribute it and/or modify it under
215 the same terms as the Perl 5 programming language system itself.
216  
217 =cut