File Coverage

blib/lib/Module/Faker/Package.pm
Criterion Covered Total %
statement 26 28 92.8
branch 5 8 62.5
condition 3 6 50.0
subroutine 4 4 100.0
pod 0 1 0.0
total 38 47 80.8


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