File Coverage

lib/Module/New/Context.pm
Criterion Covered Total %
statement 78 87 89.6
branch 16 22 72.7
condition 5 13 38.4
subroutine 17 19 89.4
pod 12 12 100.0
total 128 153 83.6


line stmt bran cond sub pod time code
1             package Module::New::Context;
2              
3 6     6   2067 use strict;
  6         8  
  6         188  
4 6     6   21 use warnings;
  6         10  
  6         120  
5 6     6   23 use Carp;
  6         22  
  6         374  
6              
7 6     6   25 use Module::New::Loader;
  6         7  
  6         174  
8 6     6   2004 use Module::New::Log;
  6         13  
  6         31  
9 6     6   357 use Sub::Install 'reinstall_sub';
  6         10  
  6         22  
10              
11             foreach my $accessor (qw( template date path loader files )) {
12             reinstall_sub({
13             as => $accessor,
14 358     358   5629 code => sub { shift->{$accessor} },
15             });
16             }
17              
18             sub new {
19 14     14 1 92 my $class = shift;
20              
21 14         83 my $loader = Module::New::Loader->new(@_);
22 14         56 my $self = bless { loader => $loader }, $class;
23              
24 14         36 foreach my $name (qw( License Template )) {
25 28         113 $self->{lc $name} = $loader->load_class($name);
26             }
27              
28 14         43 foreach my $name (qw( Config Path Date Files )) {
29 56         1438 $self->{lc $name} = $loader->load($name);
30             }
31              
32 14         68 $self;
33             }
34              
35             sub config {
36 327     327 1 1609 my $self = shift;
37 327 100       887 return $self->{config} unless @_;
38 296 100       1397 return $self->{config}->get(@_) if @_ == 1;
39 4         17 $self->{config}->set(@_);
40             }
41              
42             sub license {
43 20     20 1 3296 my ($self, $type, $args) = @_;
44 20   50     124 $type ||= $self->config('license') || 'perl';
      33        
45 20   50     108 $args ||= {};
46 20   33     149 $args->{holder} ||= $self->config('author');
47 20   33     116 $args->{year} ||= $self->date->year;
48 20         297 $self->{license}->object( $type, $args );
49             }
50              
51             sub distname {
52 23     23 1 13737 my $self = shift;
53              
54 23 100       76 if ( @_ ) {
55 6         14 my $module = my $dist = shift;
56 6         15 $dist =~ s/::/\-/g;
57 6         14 $module =~ s/\-/::/g;
58              
59 6 50       21 croak "$dist looks weird" if $dist =~ tr/A-Za-z0-9_\-//cd;
60              
61 6         18 my $distid = lc $dist;
62 6         12 $distid =~ s/\-/_/g;
63              
64 6         30 $self->{distname} = $dist;
65 6         14 $self->{distid} = $distid;
66 6         22 $self->module( $module );
67             }
68              
69 23         338 $self->{distname};
70             }
71              
72             sub module {
73 38     38 1 29813 my $self = shift;
74              
75 38 100       97 if ( @_ ) {
76 8         17 $self->{module} = shift;
77 8         16 my $path = $self->{module};
78 8         51 $path =~ s|::|\/|g;
79 8         16 my $id = lc $path;
80 8         16 $id =~ s|/|_|g;
81 8         36 $self->mainfile("lib/$path.pm");
82 8         28 $self->modulepath($path);
83 8         25 $self->moduleid($id);
84             }
85 38         668 $self->{module};
86             }
87              
88             sub mainfile {
89 31     31 1 123 my $self = shift;
90 31 100       75 if ( @_ ) {
91 10         18 $self->{mainfile} = shift;
92             }
93 31         276 $self->{mainfile};
94             }
95              
96             sub maindir {
97 0     0 1 0 my $self = shift;
98 0         0 my $dir = $self->{mainfile};
99 0         0 $dir =~ s/\.pm$//;
100 0         0 return $dir;
101             }
102              
103             sub modulepath {
104 8     8 1 14 my $self = shift;
105 8 50       25 if ( @_ ) {
106 8         18 $self->{modulepath} = shift;
107             }
108 8         12 $self->{modulepath};
109             }
110              
111             sub moduleid {
112 11     11 1 825 my $self = shift;
113 11 100       30 if ( @_ ) {
114 8         25 $self->{moduleid} = shift;
115             }
116 11         97 $self->{moduleid};
117             }
118              
119             sub modulebase {
120 3     3 1 1443 my $self = shift;
121 3         20 my ($name) = $self->{module} =~ /(\w+)$/;
122 3         39 return $name;
123             }
124              
125             sub distid {
126 0     0 1 0 my $self = shift;
127 0 0       0 if ( @_ ) {
128 0         0 $self->{distid} = shift;
129             }
130 0         0 $self->{distid};
131             }
132              
133             sub repository {
134 6     6 1 68 my $self = shift;
135 6 50       26 if ( @_ ) {
136 0         0 $self->{repository} = shift;
137             }
138 6 50       155 $self->{repository} || '';
139             }
140              
141             *dist_name = \&distname;
142             *dist_id = \&distid;
143             *main_file = \&mainfile;
144             *main_dir = \&maindir;
145             *module_path = \&modulepath;
146             *module_id = \&moduleid;
147             *module_base = \&modulebase;
148              
149             1;
150              
151             __END__