File Coverage

lib/Package/Strictures/Register.pm
Criterion Covered Total %
statement 57 64 89.0
branch 11 22 50.0
condition 1 3 33.3
subroutine 12 12 100.0
pod n/a
total 81 101 80.2


line stmt bran cond sub pod time code
1 4     4   39431 use 5.008; # 8 = utf8, 6 = pragmas, our, 4 = __PACAKGE__
  4         13  
  4         157  
2              
3 4     4   20 use strict;
  4         9  
  4         116  
4 4     4   37 use warnings;
  4         7  
  4         97  
5 4     4   1795 use utf8;
  4         23  
  4         26  
6              
7             package Package::Strictures::Register;
8             $Package::Strictures::Register::VERSION = '1.000000';
9             # ABSTRACT: Create compile-time constants that can be tweaked by users with Package::Strictures.
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 4     4   1285 use Package::Strictures::Registry ();
  4         8  
  4         76  
14 4     4   21 use Carp ();
  4         7  
  4         3300  
15              
16             sub import {
17 3     3   63 my ( $self, %params ) = @_;
18              
19 3 50       22 if ( not %params ) {
20 0         0 Carp::carp( __PACKAGE__ . ' called with no parameters, skipping magic' );
21 0         0 return;
22             }
23              
24 3         15 my (@caller) = caller;
25 3         6 my $package = $caller[0];
26 3 50       13 $package = $params{-for} if exists $params{-for};
27              
28 3 50       15 if ( not exists $params{-setup} ) {
29 0         0 Carp::croak("Can't setup strictures for package '$package', need -setup ");
30             }
31              
32 3         13 $self->_setup( $params{-setup}, $package );
33              
34 3         189 return;
35             }
36              
37             sub _setup {
38 3     3   8 my ( $self, $params, $package ) = @_;
39              
40 3         8 my $reftype = ref $params;
41              
42 3 50       11 if ( not 'HASH' eq $reftype ) {
43 0         0 Carp::croak(qq/ -setup => can presently only support a HASH. Got '$reftype'/);
44             }
45              
46 3 0 33     16 if ( ( not exists $params->{-strictures} ) && ( not exists $params->{-groups} ) ) {
47 0         0 Carp::croak('Neither -setup => { -strictures } or -setup => { -groups } provided.');
48             }
49              
50 3 50       16 if ( exists $params->{-groups} ) {
51 3         9507 Carp::carp('-groups support is not yet implemented');
52             }
53              
54 3 50       212 $params->{-strictures} = {} unless exists $params->{-strictures};
55 3 50       16 $params->{-groups} = {} unless exists $params->{-groups};
56              
57 3         18 $self->_setup_strictures( $params->{-strictures}, $package );
58              
59 3         8 return;
60             }
61              
62             sub _setup_strictures {
63 3     3   8 my ( $self, $strictures, $package ) = @_;
64 3         9 my $reftype = ref $strictures;
65              
66 3 50       13 if ( not 'HASH' eq $reftype ) {
67 0         0 Carp::croak( qq/Can't handle anything except a HASH ( Got $reftype )/
68             . qq/ for param -setup => { -strictures => } in -setup for $package/ );
69             }
70              
71 3         4 for my $subname ( keys %{$strictures} ) {
  3         13  
72              
73 3         15 $self->_setup_stricture( $strictures->{$subname}, $package, $subname );
74             }
75              
76 3         12 return;
77             }
78              
79             sub _setup_stricture {
80             ## no critic 'ProhibitNoStrict'
81 3     3   9 my ( $self, $prototype, $package, $name ) = @_;
82 3 50       15 if ( not exists $prototype->{default} ) {
83 0         0 Carp::croak("Prototype for `$package`::`$name` lacks a [required] ->{'default'} ");
84             }
85              
86 3         17 $self->_advertise_stricture( $package, $name );
87              
88 3         5297 require Import::Into;
89 3         10029 require constant;
90              
91 3         32 constant->import::into( $package, $name, $self->_fetch_stricture_value( $package, $name, $prototype->{default} ) );
92              
93 3         763 return;
94             }
95              
96             sub _advertise_stricture {
97 3     3   7 my ( undef, $package, $name ) = @_;
98 3         31 Package::Strictures::Registry->advertise_value( $package, $name );
99 3         5 return;
100             }
101              
102             sub _fetch_stricture_value {
103 3     3   12 my ( undef, $package, $name, $default ) = @_;
104 3 100       30 if ( Package::Strictures::Registry->has_value( $package, $name ) ) {
105 2         17 return Package::Strictures::Registry->get_value( $package, $name );
106             }
107 1         8 return $default;
108             }
109              
110             1;
111              
112             __END__