line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::GenSource; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2015-06-12'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.06'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
18280
|
use 5.010001; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
7
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
8
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
18
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
586
|
use Data::Dumper; |
|
1
|
|
|
|
|
7417
|
|
|
1
|
|
|
|
|
957
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub _dump { |
13
|
4
|
|
|
4
|
|
3
|
local $Data::Dumper::Sortkeys = 1; |
14
|
4
|
|
|
|
|
5
|
local $Data::Dumper::Indent = 0; |
15
|
4
|
|
|
|
|
3
|
local $Data::Dumper::Terse = 1; |
16
|
4
|
|
|
|
|
4
|
local $Data::Dumper::Deparse = 1; |
17
|
4
|
|
|
|
|
10
|
Dumper($_[0]); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
require Exporter; |
21
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
22
|
|
|
|
|
|
|
our @EXPORT_OK = qw(gen_class_source_code); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our %SPEC; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $re_ident = qr/\A[A-Za-z_][A-Za-z0-9_]*(::[A-Za-z_][A-Za-z0-9_]*)*\z/; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$SPEC{gen_class_source_code} = { |
29
|
|
|
|
|
|
|
v => 1.1, |
30
|
|
|
|
|
|
|
summary => 'Generate Perl source code to declare a class', |
31
|
|
|
|
|
|
|
description => <<'_', |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
_ |
34
|
|
|
|
|
|
|
args => { |
35
|
|
|
|
|
|
|
name => { |
36
|
|
|
|
|
|
|
schema => ['str*', match=>$re_ident], |
37
|
|
|
|
|
|
|
req => 1, |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
parent => { |
40
|
|
|
|
|
|
|
schema => ['str*', match=>$re_ident], |
41
|
|
|
|
|
|
|
}, |
42
|
|
|
|
|
|
|
attributes => { |
43
|
|
|
|
|
|
|
schema => ['hash*', match=>$re_ident], |
44
|
|
|
|
|
|
|
default => {}, |
45
|
|
|
|
|
|
|
}, |
46
|
|
|
|
|
|
|
variant => { |
47
|
|
|
|
|
|
|
schema => ['str*', in=>[qw/classic Mo Moo Moose Mouse Mojo::Base/]], |
48
|
|
|
|
|
|
|
default => 'classic', |
49
|
|
|
|
|
|
|
}, |
50
|
|
|
|
|
|
|
}, |
51
|
|
|
|
|
|
|
result_naked => 1, |
52
|
|
|
|
|
|
|
}; |
53
|
|
|
|
|
|
|
sub gen_class_source_code { |
54
|
2
|
|
|
2
|
1
|
4483
|
my %args = @_; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# XXX schema |
57
|
2
|
|
100
|
|
|
10
|
my $variant = $args{variant} // 'classic'; |
58
|
2
|
|
50
|
|
|
6
|
my $attrs = $args{attributes} // {}; |
59
|
|
|
|
|
|
|
|
60
|
2
|
|
|
|
|
1
|
my @res; |
61
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
5
|
push @res, "package $args{name};\n"; |
63
|
2
|
50
|
|
|
|
6
|
if ($variant eq 'Mojo::Base') { |
64
|
0
|
0
|
|
|
|
0
|
push @res, "use $variant ", |
65
|
|
|
|
|
|
|
($args{parent} ? "'$args{parent}'" : "-base"), ";\n"; |
66
|
2
|
50
|
|
|
|
10
|
} if ($variant eq 'Mo') { |
|
|
100
|
|
|
|
|
|
67
|
0
|
|
|
|
|
0
|
push @res, "use Mo qw(default);\n"; |
68
|
|
|
|
|
|
|
} elsif ($variant =~ /^(Moo|Moose|Mouse)$/) { |
69
|
1
|
|
|
|
|
3
|
push @res, "use $variant;\n"; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
2
|
50
|
|
|
|
5
|
if ($args{parent}) { |
73
|
0
|
0
|
|
|
|
0
|
if ($variant =~ /^(Mo|Moo|Moose|Mouse)$/) { |
|
|
0
|
|
|
|
|
|
74
|
0
|
|
|
|
|
0
|
push @res, "extends '$args{parent}';\n"; |
75
|
|
|
|
|
|
|
} elsif ($variant eq 'classic') { |
76
|
0
|
|
|
|
|
0
|
push @res, "use parent qw(", $args{parent}, ");\n"; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
2
|
100
|
|
|
|
4
|
if ($variant eq 'classic') { |
81
|
1
|
|
|
|
|
2
|
push @res, q[sub new { my $class = shift; my $self = bless {@_}, $class]; |
82
|
1
|
|
|
|
|
5
|
for my $name (sort keys %$attrs) { |
83
|
3
|
|
|
|
|
128
|
my $spec = $attrs->{$name}; |
84
|
3
|
100
|
|
|
|
7
|
if (exists $spec->{default}) { |
85
|
2
|
|
|
|
|
6
|
push @res, "; \$self->{'$name'} = ", _dump($spec->{default}), |
86
|
|
|
|
|
|
|
" unless exists \$self->{'$name'}"; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
1
|
|
|
|
|
68
|
push @res, q[; $self }], "\n"; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
8
|
for my $name (sort keys %$attrs) { |
93
|
6
|
|
|
|
|
85
|
my $spec = $attrs->{$name}; |
94
|
6
|
50
|
|
|
|
17
|
if ($variant =~ /^(Mojo::Base)$/) { |
|
|
100
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
0
|
push @res, ( |
96
|
|
|
|
|
|
|
"has '$name'", |
97
|
|
|
|
|
|
|
(exists($spec->{default}) ? " => "._dump($spec->{default}) : ''), |
98
|
|
|
|
|
|
|
";\n", |
99
|
|
|
|
|
|
|
); |
100
|
|
|
|
|
|
|
} elsif ($variant =~ /^(Mo|Moo|Moose|Mouse)$/) { |
101
|
3
|
100
|
|
|
|
12
|
push @res, ( |
102
|
|
|
|
|
|
|
"has $name => (is=>'rw'", |
103
|
|
|
|
|
|
|
(exists($spec->{default}) ? ", default=>"._dump($spec->{default}) : ''), |
104
|
|
|
|
|
|
|
");\n", |
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
} else { |
107
|
3
|
|
|
|
|
7
|
push @res, "sub $name { my \$self = shift; \$self->{'$name'} = \$_[0] if \@_; \$self->{'$name'} }\n"; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
103
|
join("", @res); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
# ABSTRACT: Generate Perl source code to declare a class |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |