| 1 | 5 | ▶ | use 5.008; # utf8 | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | package Gentoo::MetaEbuild::Spec::Base; | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 5 | our $VERSION = '1.000001'; | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | # ABSTRACT: A Base Class for Gentoo MetaEbuild Specifications. | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 5 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use MooseX::ClassAttribute qw( class_has ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use File::ShareDir qw( module_dir ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Path::Tiny qw( path ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use MooseX::Types::Moose qw( Str CodeRef ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use MooseX::Types::Perl qw( VersionObject ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use MooseX::Types::Path::Tiny qw( AbsPath AbsDir ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use MooseX::Has::Sugar qw( ro lazy_build rw coerce lazy ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use version; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 5 | 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 | 4 | ▶ | require JSON::MaybeXS; | ||||||||||||||||||||||||||||||
|
Subroutine
_build__decoder: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | 4 | my $decoder = JSON::MaybeXS->new()->utf8(1)->relaxed(1); | |||||||||||||||||||||||||||||||
| 38 | return sub { | ||||||||||||||||||||||||||||||||
| 39 | 10 | ▶ | $decoder->decode(shift); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 40 | 4 | }; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 5 | class_has '_spec_dir' => ( | |||||||||||||||||||||||||||||||
| 44 | isa => AbsDir, | ||||||||||||||||||||||||||||||||
| 45 | rw, lazy_build, | ||||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | sub _build__spec_dir { | ||||||||||||||||||||||||||||||||
| 49 | 4 | ▶ | my ($self) = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_build__spec_dir: called CC=4 SCAR=17.3
| |||||||||||||||||||||||||||||||||
| 50 | 4 | my $classname; | |||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | if ( ref $self && blessed $self ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
ref $self and builtin::blessed($self)
MC/DC0%0/2
ref $self and builtin::blessed($self)
ref $self
builtin::blessed($self)
Truth table33%1/3
A and B
A= ref $selfB= builtin::blessed($self)
| |||||||||||||||||||||||||||||||||
| 52 | 0 | $classname = blessed $self; | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | elsif ( ref $self ) { | ||||||||||||||||||||||||||||||||
| 55 | 0 | require Carp; | |||||||||||||||||||||||||||||||
| 56 | ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) | ||||||||||||||||||||||||||||||||
| 57 | 0 | Carp::confess q{$_[0] is not a Class/Object}; | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||||||
| 60 | 4 | $classname = $self; | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | 4 | return path( module_dir($classname) ); | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | class_has '_version' => ( | ||||||||||||||||||||||||||||||||
| 66 | isa => VersionObject, | ||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | coerce, ro, lazy, default => sub { q{0.1.0} }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 68 | 5 | ); | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | class_has '_extension' => ( | ||||||||||||||||||||||||||||||||
| 71 | isa => Str, | ||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | ro, lazy, default => sub { q{.json} }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 73 | 5 | ); | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 5 | 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 | 4 | ▶ | require Data::Rx; | ||||||||||||||||||||||||||||||
|
Subroutine
_build__schema_creator: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 84 | 4 | my $rx = Data::Rx->new(); | |||||||||||||||||||||||||||||||
| 85 | return sub { | ||||||||||||||||||||||||||||||||
| 86 | 10 | ▶ | $rx->make_schema(shift); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 87 | 4 | }; | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | sub _opt_check { | ||||||||||||||||||||||||||||||||
| 91 | 44 | ▶ | my ( $self, $opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_opt_check: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 92 | 44 | ▶ | if ( not exists $opts->{version} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 93 | 1 | $opts->{version} = $self->_version; | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | elsif ( blessed $opts->{version} ) { | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||||||
| 99 | 6 | $opts->{version} = version->parse( $opts->{version} ); | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | 44 | return $opts; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | sub _spec_file { | ||||||||||||||||||||||||||||||||
| 105 | 11 | ▶ | my ( $self, $opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_spec_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 106 | 11 | $opts = $self->_opt_check($opts); | |||||||||||||||||||||||||||||||
| 107 | 11 | return $self->_spec_dir->child( $opts->{version}->normal . $self->_extension ); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | sub _spec_data { | ||||||||||||||||||||||||||||||||
| 111 | 11 | ▶ | my ( $self, $opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_spec_data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 112 | 11 | $opts = $self->_opt_check($opts); | |||||||||||||||||||||||||||||||
| 113 | 11 | return $self->_decode( scalar $self->_spec_file($opts)->slurp() ); | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | sub _schema { | ||||||||||||||||||||||||||||||||
| 117 | 11 | ▶ | my ( $self, $opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_schema: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 118 | 11 | $opts = $self->_opt_check($opts); | |||||||||||||||||||||||||||||||
| 119 | 11 | return $self->_make_schema( $self->_spec_data($opts) ); | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | sub check { | ||||||||||||||||||||||||||||||||
| 133 | 11 | ▶ | my ( $self, $json_data, $opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 134 | 11 | $opts = $self->_opt_check($opts); | |||||||||||||||||||||||||||||||
| 135 | 11 | return $self->_schema($opts)->check($json_data); | |||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||
| 139 | 5 | ▶ | no Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 5 | 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 | ||||||||||||||||||||||||||||||||