File Coverage

blib/lib/Module/Faker/Package.pm
Criterion Covered Total %
statement 28 30 93.3
branch 5 8 62.5
condition 3 6 50.0
subroutine 5 5 100.0
pod 0 1 0.0
total 41 50 82.0


line stmt bran cond sub pod time code
1             package Module::Faker::Package 0.027;
2             # ABSTRACT: a faked package in a faked module
3              
4 8     8   99 use v5.20.0;
  8         29  
5 8     8   65 use Moose;
  8         23  
  8         70  
6              
7 8     8   60863 use Moose::Util::TypeConstraints;
  8         18  
  8         84  
8              
9             has name => (is => 'ro', isa => 'Str', required => 1);
10             has version => (is => 'ro', isa => 'Maybe[Str]');
11             has abstract => (is => 'ro', isa => 'Maybe[Str]');
12              
13             has in_file => (
14             is => 'ro',
15             isa => 'Str',
16             lazy => 1,
17             default => sub {
18             my ($self) = @_;
19             my $name = $self->name;
20             $name =~ s{::}{/}g;
21             return "lib/$name";
22             },
23             );
24              
25             # PKGWORD = package | class | role
26             # VERSION = our | our-literal | inline
27             # STYLE = statement | block
28             #
29             # STYLE VERSION
30             # PW N; our $V = '...'; statement our
31             # PW N; our $V = ... ; statement our-literal
32             # PW N V; statement inline
33             # PW N { our $V = '...' } block our
34             # PW N { our $V = ... } block our-literal
35             # PW N V { ... } block inline
36              
37             has layout => (
38             reader => '_layout',
39             default => sub {
40             return { pkgword => 'package', version => 'our', style => 'statement' };
41             },
42             );
43              
44             my %STYLE_TEMPLATE = (
45             'statement_our' => [ "PKGWORD PKGNAME;\nour \$VERSION = 'VERSIONSTR';",
46             "PKGWORD PKGNAME;",
47             ],
48             'statement_our-literal' => [ "PKGWORD PKGNAME;\nour \$VERSION = VERSIONSTR;",
49             "PKGWORD PKGNAME;",
50             ],
51             'statement_inline' => [ "PKGWORD PKGNAME VERSIONSTR;",
52             "PKGWORD PKGNAME;",
53             ],
54             'block_our' => [ "PKGWORD PKGNAME {\n our \$VERSION = 'VERSIONSTR';\n # code!\n}",
55             "PKGWORD PKGNAME {\n #code!\n}",
56             ],
57             'block_our-literal' => [ "PKGWORD PKGNAME {\n our \$VERSION = VERSIONSTR;\n # code!\n}",
58             "PKGWORD PKGNAME {\n #code!\n}",
59             ],
60             'block_inline' => [ "PKGWORD PKGNAME VERSIONSTR {\n # code!\n}",
61             "PKGWORD PKGNAME {\n #code!\n}",
62             ],
63             );
64              
65             my %KNOWN_KEY = map {; $_ => 1 } qw(pkgword version style);
66             my %KNOWN_PKGWORD = map {; $_ => 1 } qw(package class role);
67              
68             sub as_string {
69 41     41 0 90 my ($self) = @_;
70              
71 41         1605 my $layout = $self->_layout;
72              
73 41         218 my (@unknown) = grep {; ! $KNOWN_KEY{$_} } keys %$layout;
  123         292  
74 41 50       136 if (@unknown) {
75 0         0 Carp::confess("Unknown entries in package layout: @unknown");
76             }
77              
78 41   50     188 my $layout_pkgword = $layout->{pkgword} // 'package';
79 41   50     132 my $layout_version = $layout->{version} // 'our';
80 41   50     127 my $layout_style = $layout->{style} // 'statement';
81              
82 41 50       161 unless ($KNOWN_PKGWORD{$layout_pkgword}) {
83 0         0 Carp::confess("Invalid value for package layout's pkgword");
84             }
85              
86 41         1529 my $version = $self->version;
87 41         1345 my $name = $self->name;
88              
89 41         132 my $key = join q{_}, $layout_style, $layout_version;
90              
91 41         98 my $template_pair = $STYLE_TEMPLATE{$key};
92 41 50       103 confess("Can't handle style/version combination in package layout")
93             unless $template_pair;
94              
95 41 100       139 my $template = $template_pair->[ defined $version ? 0 : 1 ];
96              
97 41         394 my $body = $template =~ s/PKGWORD/$layout_pkgword/r
98             =~ s/PKGNAME/$name/r
99             =~ s/VERSIONSTR/$version/r;
100              
101 41         212 return $body;
102             }
103              
104             subtype 'Module::Faker::Type::Packages'
105             => as 'ArrayRef[Module::Faker::Package]';
106              
107 8     8   22717 no Moose;
  8         20  
  8         58  
108             1;
109              
110             __END__
111              
112             =pod
113              
114             =encoding UTF-8
115              
116             =head1 NAME
117              
118             Module::Faker::Package - a faked package in a faked module
119              
120             =head1 VERSION
121              
122             version 0.027
123              
124             =head1 PERL VERSION
125              
126             This module should work on any version of perl still receiving updates from
127             the Perl 5 Porters. This means it should work on any version of perl
128             released in the last two to three years. (That is, if the most recently
129             released version is v5.40, then this module should work on both v5.40 and
130             v5.38.)
131              
132             Although it may work on older versions of perl, no guarantee is made that the
133             minimum required version will not be increased. The version may be increased
134             for any reason, and there is no promise that patches will be accepted to
135             lower the minimum required perl.
136              
137             =head1 AUTHOR
138              
139             Ricardo Signes <cpan@semiotic.systems>
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This software is copyright (c) 2008 by Ricardo Signes.
144              
145             This is free software; you can redistribute it and/or modify it under
146             the same terms as the Perl 5 programming language system itself.
147              
148             =cut