| 1 | =head1 NAME | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | Declare::Constraints::Simple::Library::Exportable - Export Facilities | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | =cut | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | package Declare::Constraints::Simple::Library::Exportable; | ||||||||||||||||||
| 8 | 14 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 14 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | 14 | ▶ | use Carp::Clan qw(^Declare::Constraints::Simple); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | 14 | ▶ | use Class::Inspector; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 14 | ▶ | use aliased 'Declare::Constraints::Simple::Library::Base' => 'LibraryBase'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | sub Library () { 'Declare::Constraints::Simple::Library' } | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | This contains the constraint export logic of the module. | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | =head1 METHODS | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | =head2 import($flag, @args) | ||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | use ExportableModule->All; | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | # or | ||||||||||||||||||
| 28 | use ExportableModule-Only => qw(Constraint1 ...); | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | # or | ||||||||||||||||||
| 31 | use ExportableModule-Library; | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | Exports the constraints to the calling namespace. This includes all | ||||||||||||||||||
| 34 | libraries in L<Declare::Constraints::Simple::Library>, that package itself | ||||||||||||||||||
| 35 | (providing all default constraints) or L<Declare::Constraints::Simple> | ||||||||||||||||||
| 36 | itself as a shortcut. | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | Possible flags are | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | =over | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | =item All | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | Imports all constraints registered in the class and its base classes. | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | =item Only | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | use Declare::Constraints::Simple::Library::Scalar-Only => 'HasLength'; | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | The above line would only import the C<HasLength> constraints from the | ||||||||||||||||||
| 51 | C<Scalar> default library. Note however, that you could also just have | ||||||||||||||||||
| 52 | said | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | use Declare::Constraints::Simple-Only => 'HasLength'; | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | as both C<::Simple> and C<::Simple::Library> work on all default | ||||||||||||||||||
| 57 | libraries. | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | =item Library | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | You can use this to define your own constraint library. For more | ||||||||||||||||||
| 62 | information, see L<Declare::Constraints::Simple::Library::Base>. | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | =back | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | =cut | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 121 | ▶ | sub import { | ||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=16.1
| |||||||||||||||||||
| 69 | 121 | my ($class, $flag, @args) = @_; | |||||||||||||||||
| 70 | 121 | ▶ | return unless $flag; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 119 | my $handle_map = $class->_build_handle_map; | |||||||||||||||||
| 73 | 119 | my $target = scalar(caller); | |||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 119 | ▶ | if ($flag =~ /^-?all$/i) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 76 | 5 | $class->_export_all($target, $handle_map); | |||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | elsif ($flag =~ /^-?only$/i) { | ||||||||||||||||||
| 79 | 8 | $class->_export_these($target, $handle_map, @args); | |||||||||||||||||
| 80 | } | ||||||||||||||||||
| 81 | elsif ($flag =~ /^-?library$/i) { | ||||||||||||||||||
| 82 | 106 | LibraryBase->install_into($target); | |||||||||||||||||
| 83 | } | ||||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | 119 | 1; | |||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | =head2 _build_handle_map() | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | Internal method to build constraint-to-class mappings. | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | =cut | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | 119 | ▶ | sub _build_handle_map { | ||||||||||||||||
|
Subroutine
_build_handle_map: called CC=9 SCAR=22.0
| |||||||||||||||||||
| 95 | 119 | my ($class) = @_; | |||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 119 | ▶ | if ($class eq 'Declare::Constraints::Simple') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 98 | 117 | $class = Library; | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | 119 | ▶ | if ($class eq Library) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 102 | 117 | ▶ | unless (Class::Inspector->loaded(Library)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 103 | 12 | require Class::Inspector->filename(Library); | |||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | 119 | my (%seen, %handle_map, @walk, %walked); | |||||||||||||||||
| 108 | 119 | @walk = do { | |||||||||||||||||
| 109 | 14 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 110 | 119 | ($class, @{$class . '::ISA'}); | |||||||||||||||||
| 111 | }; | ||||||||||||||||||
| 112 | |||||||||||||||||||
| 113 | 119 | ▶ | while (my $w = shift @walk) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | 471 | ▶ | next if $walked{$w}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 116 | 272 | $walked{$w} = 1; | |||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 272 | ▶ | if ($w->can('fetch_constraint_declarations')) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 119 | 153 | my @decl = $w->fetch_constraint_declarations; | |||||||||||||||||
| 120 | 153 | for my $d (@decl) { | |||||||||||||||||
| 121 | 531 | ▶ | next if exists $seen{$d}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 122 | 530 | $seen{$d} = 1; | |||||||||||||||||
| 123 | 530 | $handle_map{$d} = $w; | |||||||||||||||||
| 124 | } | ||||||||||||||||||
| 125 | } | ||||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | push @walk, | ||||||||||||||||||
| 128 | 261 | grep { not exists $walked{$_} } | |||||||||||||||||
| 129 | 14 | ▶ | do { no strict 'refs' ; @{$w . '::ISA'} }; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 130 | } | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | 119 | return \%handle_map; | |||||||||||||||||
| 133 | } | ||||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | =head2 _export_all($target, $handle_map) | ||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | Internal method. Exports all handles in C<$handle_map> into the C<$target> | ||||||||||||||||||
| 138 | namespace. | ||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | =cut | ||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | 5 | ▶ | sub _export_all { | ||||||||||||||||
|
Subroutine
_export_all: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 143 | 5 | my ($class, $target, $handle_map) = @_; | |||||||||||||||||
| 144 | 5 | return $class->_export_these($target, $handle_map, keys %$handle_map); | |||||||||||||||||
| 145 | } | ||||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | =head2 _export_these($target, $handle_map, @constraints) | ||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | Internal method. Exports all C<@constraints> from C<$handle_map> into the | ||||||||||||||||||
| 150 | C<$target> namespace. | ||||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | =cut | ||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | 13 | ▶ | sub _export_these { | ||||||||||||||||
|
Subroutine
_export_these: called CC=4 SCAR=14.0
| |||||||||||||||||||
| 155 | 13 | my ($class, $target, $handle_map, @decl) = @_; | |||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | 13 | for my $d (@decl) { | |||||||||||||||||
| 158 | 220 | ▶ | my $handle = $handle_map->{$d} | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 159 | or croak "Constraint '$d' cannot be found in $class"; | ||||||||||||||||||
| 160 | 220 | my $gen = $handle_map->{$d}->fetch_constraint_generator($d); | |||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | croak sprintf | ||||||||||||||||||
| 163 | 'Constraint Generator for $s in %s did not return a closure', | ||||||||||||||||||
| 164 | 220 | ▶ | $d, $handle_map->{$d} | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 165 | unless ref($gen) eq 'CODE'; | ||||||||||||||||||
| 166 | |||||||||||||||||||
| 167 | 14 | ▶ | { no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 168 | 220 | *{$target . '::' . $d} = $gen; | |||||||||||||||||
| 169 | } | ||||||||||||||||||
| 170 | } | ||||||||||||||||||
| 171 | } | ||||||||||||||||||
| 172 | |||||||||||||||||||
| 173 | =head1 SEE ALSO | ||||||||||||||||||
| 174 | |||||||||||||||||||
| 175 | L<Declare::Constraints::Simple>, L<Declare::Constraints::Simple::Library>, | ||||||||||||||||||
| 176 | L<Declare::Constraints::Simple::Library::Base> | ||||||||||||||||||
| 177 | |||||||||||||||||||
| 178 | =head1 AUTHOR | ||||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | Robert 'phaylon' Sedlacek C<E<lt>phaylon@dunkelheit.atE<gt>> | ||||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||
| 183 | |||||||||||||||||||
| 184 | This module is free software, you can redistribute it and/or modify it | ||||||||||||||||||
| 185 | under the same terms as perl itself. | ||||||||||||||||||
| 186 | |||||||||||||||||||
| 187 | =cut | ||||||||||||||||||
| 188 | |||||||||||||||||||
| 189 | 14 | 1; | |||||||||||||||||