File Coverage

lib/SweetPea/Cli/Mvc.pm
Criterion Covered Total %
statement 21 66 31.8
branch 0 12 0.0
condition 0 3 0.0
subroutine 7 11 63.6
pod 0 2 0.0
total 28 94 29.7


line stmt bran cond sub pod time code
1             package SweetPea::Cli::Mvc;
2              
3 1     1   11 use warnings;
  1         2  
  1         44  
4 1     1   6 use strict;
  1         2  
  1         39  
5              
6 1     1   6 use SweetPea;
  1         3  
  1         62  
7 1     1   58 use SweetPea::Cli::Util;
  1         3  
  1         24  
8 1     1   6 use SweetPea::Cli::Flash;
  1         2  
  1         28  
9 1     1   6 use SweetPea::Cli::Error;
  1         2  
  1         22  
10 1     1   6 use SweetPea::Cli::Help;
  1         2  
  1         957  
11              
12             # SweetPea::Cli::Mvc - Model, View, Controller builder for use with SweetPea-Cli
13              
14             sub new {
15 0     0 0   my $class = shift;
16 0           my $self = {};
17 0           bless $self, $class;
18            
19 0           my $c = $self->{c} = shift;
20 0           my $f = SweetPea::Cli::Flash->new;
21 0           my $e = SweetPea::Cli::Error->new;
22            
23             $self->{commands} = [
24             {
25             name => 'mvc',
26             code => sub {
27 0     0     $self->mvc(@_)
28             },
29 0           args => {
30             'model' => {
31             aliases => ['m']
32             },
33             'view' => {
34             aliases => ['v']
35             },
36             'controller' => {
37             aliases => ['c']
38             }
39             },
40             help => 'generate code for models, views and/or controllers.'
41             }
42             ];
43            
44 0           return $self;
45             }
46              
47             sub mvc {
48 0     0 0   my $self = shift;
49 0           my $c = shift;
50 0           my $f = SweetPea::Cli::Flash->new;
51 0           my $e = SweetPea::Cli::Error->new;
52 0           my $u = SweetPea::Cli::Util->new;
53 0           my $h = SweetPea::Cli::Help->new;
54            
55 0           my $doc = $c->argv->[0];
56 0 0         my $type = 'Model'
57             if $c->options->{model};
58 0 0         $type = 'View'
59             if $c->options->{view};
60 0 0         $type = 'Controller'
61             if $c->options->{controller};
62            
63 0 0 0       unless ($type && $doc) {
64 0           return $h->display('mvc', $c);
65             }
66            
67 0 0         if ($type) {
68 0 0         if ($doc) {
69 0           $doc =~ s/^[\\\/]+//;
70 0           $doc =~ s/\.pm$//;
71 0           my $package = $doc;
72 0           $package =~ s/([\\\/]|::)+/::/g;
73 0           $doc =~ s/([\\\/]|::)+/\//g;
74 0           $doc .= '.pm';
75            
76             # make files
77 0           $self->_write(
78             0754,
79             "sweet/application/$type/$doc",
80             "generated/". lc($type) ."/new.tt",
81             $package
82             );
83            
84 0           my @return = (
85             "$type created successfully as ...",
86             "... file $doc"
87             );
88 0           return $f->flash(@return)->report($c);
89             }
90             else {
91 0           my @return = (
92             'No data profile specified. Use `help make;` for instructions.'
93             );
94 0           return $e->error(@return)->report($c);
95             }
96             }
97             else {
98 0           my @return = (
99             'No MVC specified. Use `help mvc;` for instructions.'
100             );
101 0           return $e->error(@return)->report($c);
102             }
103             }
104              
105             sub _write {
106 0     0     my $self = shift;
107 0           my ($mask, $to, $from, $package) = @_;
108 0           my $f = SweetPea::Cli::Flash->new;
109 0           my $e = SweetPea::Cli::Error->new;
110 0           my $u = SweetPea::Cli::Util->new;
111            
112 0           my $stash = {
113             shebang => $^X,
114             version => $SweetPea::VERSION,
115             head => '---',
116             fruitful => 'Now go be fruitful and multiply.',
117             package => $package
118             };
119            
120 0           my $content = '';
121 0           $content = $u->template($from, $stash);
122            
123 0           $u->makefile(
124             'file' => $to,
125             'content' => $content,
126             'bitmask' => $mask
127             );
128             }
129              
130             1; # End of SweetPea::Cli::Mvc