lib/Badger/App.pm

filter: Statementstmt 28.0% 28 / 100 Branchbran 0.0% 0 / 20 Conditioncond 3.2% 1 / 31 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 36.8% 7 / 19 Podpod 14.2% 2 / 14 totaltotal 18.9% 38 / 201 CC 25 SCAR 35.5
CC
25
Cov
19%
args
110.0
blurb
30.0
validate
20.0
CRAP 35.0
« blib/lib/Badger/Filesystem/Universal.pm ↑ Summary blib/lib/Badger/Base.pm »
1 package Badger::App;
2  
3 1 ▶use Badger::Config::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Badger::Reporter::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Badger::Debug ':dump debugf';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Badger::Apps;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use Badger::Class
8 1 version => 0.01,
9 debug => 0,
10 base => 'Badger::Prototype',
11 import => 'class CLASS',
12 utils => 'wrap',
13 accessors => 'name author date version',
14 constants => 'DELIMITER ARRAY',
15 constant => {
16 SCHEMA => 'Badger::Config::Schema',
17 APPS => 'Badger::Apps',
18 REPORTER => 'Badger::Reporter::App',
19 },
20 alias => {
21 init => \&init_app,
22 },
23 config => [
24 'base|class:CLASS',
25 'name|class:NAME|method:CLASS',
26 'author|class:AUTHOR',
27 'version|class:VERSION',
28 'date|class:DATE',
29 'about|class:ABOUT',
30 'usage|class:USAGE',
31 'actions|class:ACTIONS',
32 # 'apps|class:APPS',
33 # 'app_path|class:APP_PATH|method:CLASS',
34 ],
35 messages => {
36 missing_arg => "No value specified for %s",
37 1 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39  
40 sub init_app {
41 1 ▶ my ($self, $config) = @_;
Subroutine init_app: called CC=2 SCAR=7.0
Pod undocumented
42  
43 1 $self->debugf(
44 "init_app(%s)",
45 $self->dump_data($config),
46 ) if DEBUG;
47  
48 1 $self->configure($config)
49 ->init_options($config);
50  
51 # shared context
52 1 ▶ $self->{ app } = $config->{ app } || { };
Condition50%1/2
$config->{'app'} || {}
l!l
01
MC/DC0%0/1
$config->{'app'} || {}
$config->{'app'}
Truth table50%1/2
A
A= $config->{'app'}
Aresult
11
00
53 1 $self->{ parent } = $config->{ parent };
54 1 $self->{ config } = $config;
55  
56 # $self->{ app }->{ $self } = 'Hello World';
57 # $self->debug("app: ", $self->dump_data($self->{ app }));
58 # my $apps = $self->{ app }->{ apps } ||= [ ];
59 # push(@$apps, $self);
60
61 1 return $self;
62 }
63  
64  
65 sub init_options {
66 1 ▶ my ($self, $config) = @_;
Subroutine init_options: called CC=1 SCAR=0.0
Pod undocumented
67 1 my $options = $self->class->list_vars( OPTIONS => $config->{ options } );
68 1 $self->{ schema } = $self->SCHEMA->new( schema => $options );
69 1 $self->debug("created schema: ", $self->{ schema }) if DEBUG;
70 1 return $self;
71 }
72  
73  
74 sub args {
75 0 ▶ my $self = shift->prototype;
Subroutine args: not called CC=10 SCAR=47.0
Pod undocumented
76 0 ▶ my $args = @_ == 1 && ref $_[0] eq ARRAY ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'ARRAY' ? : 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
77 0 ▶ my $options = $self->{ app }->{ options } ||= { };
Condition0%0/2
$self->{"app"}{"options"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"app"}{"options"} ||= {}
$self->{"app"}{"options"}
Truth table0%0/2
A
A= $self->{"app"}{"options"}
Aresult
11
00
78 0 my ($arg, $option, $app);
79
80 0 $self->debug("args(", $self->dump_data_inline($args), ")") if DEBUG;
81  
82 0 my $schema = $self->{ schema };
83 0 $self->debug("using schema: $schema") if DEBUG;
84
85 0 ▶ while (@$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args) 0 0 0
86 0 $arg = $args->[0];
87 0 $self->debug("option: $arg") if DEBUG;
88
89 0 ▶ if ($option = $schema->item($arg)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($option = $schema->item($arg)) { } 0 0 0
elsif ($arg =~ /^--(.*)/) { } 0 0 0
elsif ($arg =~ /^[\w\.]+$/ and $app = $self->app($arg)) { } 0 0 0
90 0 $self->debug("got schema option: $option") if DEBUG;
91 }
92 elsif ($arg =~ /^--(.*)/) {
93 0 $self->debug("looking for option: $1") if DEBUG;
94 # TODO: look up app stack
95 0 ▶ $option = $schema->item($1)
Condition0%0/2
$schema->item($1) || (return $self->error_msg('invalid', 'argument', $arg))
l!l
00
MC/DC0%0/1
$schema->item($1) || (return $self->error_msg('invalid', 'argument', $arg))
$schema->item($1)
Truth table0%0/2
A
A= $schema->item($1)
Aresult
11
00
96 || return $self->error_msg( invalid => argument => $arg );
97 }
98 ▶ elsif ($arg =~ /^[\w\.]+$/ && ($app = $self->app($arg))) {
Condition0%0/3
$arg =~ /^[\w\.]+$/ and $app = $self->app($arg)
!ll&&!rl&&r
000
MC/DC0%0/2
$arg =~ /^[\w\.]+$/ and $app = $self->app($arg)
$arg =~ /^[\w\.]+$/ $app = $self->app($arg)
Truth table0%0/3
A and B
A= $arg =~ /^[\w\.]+$/B= $app = $self->app($arg)
ABresult
0X0
100
111
99 0 shift @$args;
100 return $app->new(
101 parent => $self,
102 app => $self->{ app }
103 0 )->args($args);
104 }
105 else {
106 0 $self->debug("not found: $arg") if DEBUG;
107 0 return $self->error_msg( invalid => argument => $arg );
108 }
109  
110 0 shift @$args;
111 0 $option->args($args, $self->{ app }, $self);
112 }
113  
114 0 return $self;
115 # $self->debug("options schema for this app is: ", $schema);
116 # $self->not_implemented('in base class');
117 }
118  
119  
120 sub validate {
121 0 ▶ my $self = shift->prototype;
Subroutine validate: not called CC=4 SCAR=30.0
Pod undocumented
122 0 my $app = $self->{ app };
123 0 my $schema = $self->{ schema };
124 0 my ($item, $name);
125
126 0 foreach $item ($schema->items) {
127 0 ▶ next unless $item->{ required };
Branch: 1 branch
BranchTrueFalseTotal
unless $item->{"required"} 0 0 0
128 0 $name = $item->{ name };
129 return $self->error_msg( missing_arg => $name )
130 0 ▶ unless defined $app->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $app->{$name} 0 0 0
131 }
132  
133 0 return $app;
134 }
135  
136  
137 sub app {
138 0 ▶ shift->apps->app(@_);
Subroutine app: not called CC=1 SCAR=6.9
Pod undocumented
139 }
140  
141  
142 sub apps {
143 0 ▶ my $self = shift;
Subroutine apps: not called CC=3 SCAR=24.8
Pod undocumented
144  
145 0 ▶ return $self->{ apps } ||= do {
Condition0%0/3
$self->{"apps"} ||= do { my $class = $self->class; my $apps = $class->hash_vars("APPS", $self->{"config"}{"apps"}); my $path = $class->list_vars("APP_PATH", $self->{"config"}{"app_path"}); push @$path, $self->class->name unless @$path; '???'; $self->APPS->new("path", $path, "apps", $apps) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"apps"} ||= do { my $class = $self->class; my $apps = $class->hash_vars("APPS", $self->{"config"}{"apps"}); my $path = $class->list_vars("APP_PATH", $self->{"config"}{"app_path"}); push @$path, $self->class->name unless @$path; '???'; $self->APPS->new("path", $path, "apps", $apps) }
$self->{"apps"} do { my $class = $self->class; my $apps = $class->hash_vars("APPS", $self->{"config"}{"apps"}); my $path = $class->list_vars("APP_PATH", $self->{"config"}{"app_path"}); push @$path, $self->class->name unless @$path; '???'; $self->APPS->new("path", $path, "apps", $apps) }
Truth table0%0/3
A ||= B
A= $self->{"apps"}B= do { my $class = $self->class; my $apps = $class->hash_vars("APPS", $self->{"config"}{"apps"}); my $path = $class->list_vars("APP_PATH", $self->{"config"}{"app_path"}); push @$path, $self->class->name unless @$path; '???'; $self->APPS->new("path", $path, "apps", $apps) }
ABresult
1X1
011
000
146 0 my $class = $self->class;
147 my $apps = $class->hash_vars(
148 APPS => $self->{ config }->{ apps }
149 0 );
150 my $path = $class->list_vars(
151 APP_PATH => $self->{ config }->{ app_path }
152 0 );
153 0 ▶ push(@$path, $self->class->name) unless @$path;
Branch: 1 branch
BranchTrueFalseTotal
unless @$path 0 0 0
154 0 $self->debug(
155 "creating app factory with path: ", $self->dump_data_inline($path),
156 "and apps: ", $self->dump_data_inline($apps)
157 ) if DEBUG;
158 0 $self->APPS->new(
159 path => $path,
160 apps => $apps,
161 );
162 };
163 }
164  
165  
166 sub run {
167 0 ▶ my $self = shift;
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
168 0 $self->validate;
169 0 $self->not_implemented('in base class');
170 }
171  
172  
173 #-----------------------------------------------------------------------
174 # output generation
175 #-----------------------------------------------------------------------
176  
177 sub reporter {
178 0 ▶ my $self = shift->prototype;
Subroutine reporter: not called CC=3 SCAR=24.8
Pod undocumented
179 0 ▶ my $config = @_ ? params(@_) : $self->{ app };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
180 return $self->{ reporter }
181 0 ▶ ||= class($self->REPORTER)->load->instance($config);
Condition0%0/3
$self->{"reporter"} ||= Badger::App::class($self->REPORTER)->load->instance($config)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"reporter"} ||= Badger::App::class($self->REPORTER)->load->instance($config)
$self->{"reporter"} Badger::App::class($self->REPORTER)->load->instance($config)
Truth table0%0/3
A ||= B
A= $self->{"reporter"}B= Badger::App::class($self->REPORTER)->load->instance($config)
ABresult
1X1
011
000
182 }
183  
184  
185 sub help {
186 0 ▶ my $self = shift->prototype;
Subroutine help: not called CC=1 SCAR=6.9
Pod undocumented
187 0 $self->credits;
188 0 $self->about;
189 0 $self->usage;
190 0 $self->options;
191 0 exit;
192 }
193  
194  
195 sub credits {
196 0 ▶ my $self = shift;
Subroutine credits: not called CC=1 SCAR=6.9
Pod undocumented
197 0 $self->reporter->credits(
198 $self->name,
199 $self->version,
200 $self->author,
201 $self->date,
202 );
203 }
204  
205  
206 sub options {
207 0 ▶ my $self = shift->prototype;
Subroutine options: not called CC=4 SCAR=30.0
Pod undocumented
208 0 my $reporter = $self->reporter;
209 0 $reporter->section('Options');
210  
211 0 foreach my $item ($self->{ schema }->items) {
212 0 $item->summary($reporter);
213 }
214 0 return;
215  
216 my $options = join(
217 "\n ",
218 0 ▶ grep { defined && length }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 0 0 0
219 0 map { $_->summary }
220 $self->{ schema }->items
221 0 ▶ ) || return '';
Condition0%0/2
join("\n ", grep({defined $_ and length $_;} map({$_->summary;} $self->{'schema'}->items))) || (return '')
l!l
00
MC/DC0%0/1
join("\n ", grep({defined $_ and length $_;} map({$_->summary;} $self->{'schema'}->items))) || (return '')
join("\n ", grep({defined $_ and length $_;} map({$_->summary;} $self->{'schema'}->items)))
Truth table0%0/2
A
A= join("\n ", grep({defined $_ and length $_;} map({$_->summary;} $self->{'schema'}->items)))
Aresult
11
00
222
223 0 $reporter->about($options);
224 }
225  
226  
227 sub blurb {
228 0 ▶ my ($self, $type, $title) = @_;
Subroutine blurb: not called CC=5 SCAR=34.0
Pod undocumented
229 0 my $reporter = $self->reporter;
230 my $blurb = $self->{ $type }
231 0 ▶ || $self->{ config }->{ $type }
Condition0%0/8
$self->{$type} || $self->{'config'}{$type} || $self->class->any_var(uc $type) || (return '')
l!l
00
$self->{$type} or $self->{"config"}{$type} or $self->class->any_var(uc $type)
l!l&&r!l&&!r
000
$self->{$type} or $self->{"config"}{$type}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{$type} || $self->{'config'}{$type} || $self->class->any_var(uc $type) || (return '')
$self->{$type} $self->{"config"}{$type} $self->class->any_var(uc $type)
Truth table0%0/4
A or B or C
A= $self->{$type}B= $self->{"config"}{$type}C= $self->class->any_var(uc $type)
ABCresult
1XX1
01X1
0011
0000
232 || $self->class->any_var( uc $type )
233 || return '';
234 0 $blurb = wrap($blurb, 76, 2);
235 0 ▶ $title ||= ucfirst $type;
Condition0%0/3
$title ||= ucfirst $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$title ||= ucfirst $type
$title ucfirst $type
Truth table0%0/3
A ||= B
A= $titleB= ucfirst $type
ABresult
1X1
011
000
236
237 0 $reporter->section($title);
238 0 $reporter->about($blurb);
239 }
240  
241  
242 sub about {
243 0 ▶ shift->blurb('about');
Subroutine about: not called CC=1 SCAR=6.9
Pod documented
244 }
245  
246  
247 sub usage {
248 0 ▶ shift->blurb('usage');
Subroutine usage: not called CC=1 SCAR=6.9
Pod documented
249 }
250  
251  
252 1 1;
253  
254  
255  
256 =head1 NAME
257  
258 Badger::App - base class application module
259  
260 =head1 DESCRIPTION
261  
262 This module implements a base class for simple, self-contained applications.
263  
264 =head1 METHODS
265  
266 The following methods are defined in addition to those inherited from the
267 L<Badger::Prototype> and L<Badger::Base> base classes.
268  
269 =head2 about()
270  
271 This method should be re-defined in subclasses to return information about
272 the application.
273  
274 =head2 usage()
275  
276 This method should be re-defined in subclasses to return a summary of usage
277 options for the application.
278  
279 =head1 AUTHOR
280  
281 Andy Wardley L<http://wardley.org/>
282  
283 =head1 COPYRIGHT
284  
285 Copyright (C) 2008-2012 Andy Wardley. All Rights Reserved.
286  
287 This module is free software; you can redistribute it and/or
288 modify it under the same terms as Perl itself.
289  
290 =head1 SEE ALSO
291  
292 L<Badger::Factory>,
293 L<Badger::Base>.
294  
295 =cut
296  
297 # Local Variables:
298 # mode: perl
299 # perl-indent-level: 4
300 # indent-tabs-mode: nil
301 # End:
302 #
303 # vim: expandtab shiftwidth=4:
304  
« blib/lib/Badger/Filesystem/Universal.pm ↑ Summary blib/lib/Badger/Base.pm »