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