blib/lib/Badger/App.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 34 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 202 CC 18 SCAR 37.9
CC
18
Cov
0%
args
132.0
validate
12.0
apps
12.0
CRAP 44.1
1 package Badger::App;
2  
3 use Badger::Config::Schema;
4 use Badger::Reporter::App;
5 use Badger::Debug ':dump debugf';
6 use Badger::Apps;
7 use Badger::Class
8 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 };
38  
39  
40 sub init_app {
41 my ($self, $config) = @_;
42  
43 $self->debugf(
44 "init_app(%s)",
45 $self->dump_data($config),
46 ) if DEBUG;
47  
48 $self->configure($config)
49 ->init_options($config);
50  
51 # shared context
52 $self->{ app } = $config->{ app } || { };
53 $self->{ parent } = $config->{ parent };
54 $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 return $self;
62 }
63  
64  
65 sub init_options {
66 my ($self, $config) = @_;
67 my $options = $self->class->list_vars( OPTIONS => $config->{ options } );
68 $self->{ schema } = $self->SCHEMA->new( schema => $options );
69 $self->debug("created schema: ", $self->{ schema }) if DEBUG;
70 return $self;
71 }
72  
73  
74 sub args {
75 my $self = shift->prototype;
76 my $args = @_ == 1 && ref $_[0] eq ARRAY ? shift : [ @_ ];
77 my $options = $self->{ app }->{ options } ||= { };
78 my ($arg, $option, $app);
79
80 $self->debug("args(", $self->dump_data_inline($args), ")") if DEBUG;
81  
82 my $schema = $self->{ schema };
83 $self->debug("using schema: $schema") if DEBUG;
84
85 while (@$args) {
86 $arg = $args->[0];
87 $self->debug("option: $arg") if DEBUG;
88
89 if ($option = $schema->item($arg)) {
90 $self->debug("got schema option: $option") if DEBUG;
91 }
92 elsif ($arg =~ /^--(.*)/) {
93 $self->debug("looking for option: $1") if DEBUG;
94 # TODO: look up app stack
95 $option = $schema->item($1)
96 || return $self->error_msg( invalid => argument => $arg );
97 }
98 elsif ($arg =~ /^[\w\.]+$/ && ($app = $self->app($arg))) {
99 shift @$args;
100 return $app->new(
101 parent => $self,
102 app => $self->{ app }
103 )->args($args);
104 }
105 else {
106 $self->debug("not found: $arg") if DEBUG;
107 return $self->error_msg( invalid => argument => $arg );
108 }
109  
110 shift @$args;
111 $option->args($args, $self->{ app }, $self);
112 }
113  
114 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 my $self = shift->prototype;
122 my $app = $self->{ app };
123 my $schema = $self->{ schema };
124 my ($item, $name);
125
126 foreach $item ($schema->items) {
127 next unless $item->{ required };
128 $name = $item->{ name };
129 return $self->error_msg( missing_arg => $name )
130 unless defined $app->{ $name };
131 }
132  
133 return $app;
134 }
135  
136  
137 sub app {
138 shift->apps->app(@_);
139 }
140  
141  
142 sub apps {
143 my $self = shift;
144  
145 return $self->{ apps } ||= do {
146 my $class = $self->class;
147 my $apps = $class->hash_vars(
148 APPS => $self->{ config }->{ apps }
149 );
150 my $path = $class->list_vars(
151 APP_PATH => $self->{ config }->{ app_path }
152 );
153 push(@$path, $self->class->name) unless @$path;
154 $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 $self->APPS->new(
159 path => $path,
160 apps => $apps,
161 );
162 };
163 }
164  
165  
166 sub run {
167 my $self = shift;
168 $self->validate;
169 $self->not_implemented('in base class');
170 }
171  
172  
173 #-----------------------------------------------------------------------
174 # output generation
175 #-----------------------------------------------------------------------
176  
177 sub reporter {
178 my $self = shift->prototype;
179 my $config = @_ ? params(@_) : $self->{ app };
180 return $self->{ reporter }
181 ||= class($self->REPORTER)->load->instance($config);
182 }
183  
184  
185 sub help {
186 my $self = shift->prototype;
187 $self->credits;
188 $self->about;
189 $self->usage;
190 $self->options;
191 exit;
192 }
193  
194  
195 sub credits {
196 my $self = shift;
197 $self->reporter->credits(
198 $self->name,
199 $self->version,
200 $self->author,
201 $self->date,
202 );
203 }
204  
205  
206 sub options {
207 my $self = shift->prototype;
208 my $reporter = $self->reporter;
209 $reporter->section('Options');
210  
211 foreach my $item ($self->{ schema }->items) {
212 $item->summary($reporter);
213 }
214 return;
215  
216 my $options = join(
217 "\n ",
218 grep { defined && length }
219 map { $_->summary }
220 $self->{ schema }->items
221 ) || return '';
222
223 $reporter->about($options);
224 }
225  
226  
227 sub blurb {
228 my ($self, $type, $title) = @_;
229 my $reporter = $self->reporter;
230 my $blurb = $self->{ $type }
231 || $self->{ config }->{ $type }
232 || $self->class->any_var( uc $type )
233 || return '';
234 $blurb = wrap($blurb, 76, 2);
235 $title ||= ucfirst $type;
236
237 $reporter->section($title);
238 $reporter->about($blurb);
239 }
240  
241  
242 sub about {
243 shift->blurb('about');
244 }
245  
246  
247 sub usage {
248 shift->blurb('usage');
249 }
250  
251  
252 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