File: Package.pm

package info (click to toggle)
libmodule-faker-perl 0.027-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 340 kB
  • sloc: perl: 1,231; makefile: 2
file content (148 lines) | stat: -rw-r--r-- 4,416 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package Module::Faker::Package 0.027;
# ABSTRACT: a faked package in a faked module

use v5.20.0;
use Moose;

use Moose::Util::TypeConstraints;

has name     => (is => 'ro', isa => 'Str', required => 1);
has version  => (is => 'ro', isa => 'Maybe[Str]');
has abstract => (is => 'ro', isa => 'Maybe[Str]');

has in_file  => (
  is       => 'ro',
  isa      => 'Str',
  lazy     => 1,
  default  => sub {
    my ($self) = @_;
    my $name = $self->name;
    $name =~ s{::}{/}g;
    return "lib/$name";
  },
);

# PKGWORD = package | class | role
# VERSION = our | our-literal | inline
# STYLE   = statement | block
#
#                           STYLE         VERSION
# PW N;  our $V = '...';    statement     our
# PW N;  our $V =  ... ;    statement     our-literal
# PW N V;                   statement     inline
# PW N { our $V = '...' }   block         our
# PW N { our $V =  ...  }   block         our-literal
# PW N V { ... }            block         inline

has layout => (
  reader  => '_layout',
  default => sub {
    return { pkgword => 'package', version => 'our', style => 'statement' };
  },
);

my %STYLE_TEMPLATE = (
  'statement_our'         =>  [ "PKGWORD PKGNAME;\nour \$VERSION = 'VERSIONSTR';",
                                "PKGWORD PKGNAME;",
                              ],
  'statement_our-literal' =>  [ "PKGWORD PKGNAME;\nour \$VERSION = VERSIONSTR;",
                                "PKGWORD PKGNAME;",
                              ],
  'statement_inline'      =>  [ "PKGWORD PKGNAME VERSIONSTR;",
                                "PKGWORD PKGNAME;",
                              ],
  'block_our'             =>  [ "PKGWORD PKGNAME {\n  our \$VERSION = 'VERSIONSTR';\n  # code!\n}",
                                "PKGWORD PKGNAME {\n  #code!\n}",
                              ],
  'block_our-literal'     =>  [ "PKGWORD PKGNAME {\n  our \$VERSION = VERSIONSTR;\n  # code!\n}",
                                "PKGWORD PKGNAME {\n  #code!\n}",
                              ],
  'block_inline'          =>  [ "PKGWORD PKGNAME VERSIONSTR {\n  # code!\n}",
                                "PKGWORD PKGNAME {\n  #code!\n}",
                              ],
);

my %KNOWN_KEY     = map {; $_ => 1 } qw(pkgword version style);
my %KNOWN_PKGWORD = map {; $_ => 1 } qw(package class role);

sub as_string {
  my ($self) = @_;

  my $layout = $self->_layout;

  my (@unknown) = grep {; ! $KNOWN_KEY{$_} } keys %$layout;
  if (@unknown) {
    Carp::confess("Unknown entries in package layout: @unknown");
  }

  my $layout_pkgword = $layout->{pkgword} // 'package';
  my $layout_version = $layout->{version} // 'our';
  my $layout_style   = $layout->{style}   // 'statement';

  unless ($KNOWN_PKGWORD{$layout_pkgword}) {
    Carp::confess("Invalid value for package layout's pkgword");
  }

  my $version = $self->version;
  my $name    = $self->name;

  my $key = join q{_}, $layout_style, $layout_version;

  my $template_pair = $STYLE_TEMPLATE{$key};
  confess("Can't handle style/version combination in package layout")
    unless $template_pair;

  my $template = $template_pair->[ defined $version ? 0 : 1 ];

  my $body = $template  =~ s/PKGWORD/$layout_pkgword/r
                        =~ s/PKGNAME/$name/r
                        =~ s/VERSIONSTR/$version/r;

  return $body;
}

subtype 'Module::Faker::Type::Packages'
  => as 'ArrayRef[Module::Faker::Package]';

no Moose;
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Module::Faker::Package - a faked package in a faked module

=head1 VERSION

version 0.027

=head1 PERL VERSION

This module should work on any version of perl still receiving updates from
the Perl 5 Porters.  This means it should work on any version of perl
released in the last two to three years.  (That is, if the most recently
released version is v5.40, then this module should work on both v5.40 and
v5.38.)

Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased.  The version may be increased
for any reason, and there is no promise that patches will be accepted to
lower the minimum required perl.

=head1 AUTHOR

Ricardo Signes <cpan@semiotic.systems>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2008 by Ricardo Signes.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut