File Coverage

blib/lib/Constructor/Sugar.pm
Criterion Covered Total %
statement 27 27 100.0
branch 2 2 100.0
condition 2 2 100.0
subroutine 7 7 100.0
pod n/a
total 38 38 100.0


line stmt bran cond sub pod time code
1             package Constructor::Sugar;
2              
3 4     4   42262 use strictures 2;
  4         84  
  4         154  
4              
5 4     4   3292 use String::CamelCase 'decamelize';
  4         1795  
  4         210  
6 4     4   1981 use Throwable::SugarFactory::Utils '_getglob';
  4         8  
  4         1329  
7              
8             our $VERSION = '0.152690'; # VERSION
9              
10             # ABSTRACT: export constructor syntax sugar
11              
12             #
13             # This file is part of Throwable-SugarFactory
14             #
15             #
16             # Christian Walde has dedicated the work to the Commons by waiving all of his
17             # or her rights to the work worldwide under copyright law and all related or
18             # neighboring legal rights he or she had in the work, to the extent allowable by
19             # law.
20             #
21             # Works under CC0 do not require attribution. When citing the work, you should
22             # not imply endorsement by the author.
23             #
24              
25              
26             sub _export {
27 23     23   38 my ( $pkg, $func, $code ) = @_;
28 23         26 *{ _getglob $pkg, $func } = $code;
  23         58  
29 22         65 return $func;
30             }
31              
32             sub import {
33 13     13   1940 my ( undef, @specs ) = @_;
34 13         28 my $target = caller;
35 13         18 my ( @constructors, @iders );
36              
37 13         24 for my $spec ( @specs ) {
38 13         51 my ( $class, $method ) = split /->/, $spec;
39 13   100     57 $method ||= "new";
40 13         33 my $id = ( reverse split /::/, $class )[0];
41 13         46 my $ct = decamelize $id;
42 13 100       346 die "Converting '$id' into a snake_case constructor did not result in"
43             . " a different string."
44             if $ct eq $id;
45              
46 12     10   56 push @constructors, _export $target, $ct, sub { $class->$method( @_ ) };
  10         21773  
47 11     21   42 push @iders, _export $target, $id, sub { $class };
  21         11865  
48             }
49              
50 11         1584 return ( \@constructors, \@iders );
51             }
52              
53             1;
54              
55             __END__