blib/lib/JiftyX/Fixtures/Script/Scaffold.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 97 CC 4 SCAR 14.1
CC
4
Cov
0%
run
12.0
render_scaffold
6.0
options
2.0
CRAP 4.1
1 package JiftyX::Fixtures::Script::Scaffold;
2 our $VERSION = '0.07';
3  
4 # ABSTRACT: scaffold subcommands
5  
6 use warnings;
7 use strict;
8  
9 use Jifty;
10 use Jifty::Everything;
11  
12 use IO::File;
13 use File::Spec;
14 use File::Basename;
15 use YAML qw(Dump LoadFile);
16  
17 use base qw(
18 App::CLI::Command
19 );
20  
21 my $super = 'JiftyX::Fixtures::Script';
22  
23 our $help_msg = qq{
24 Usage:
25  
26 jiftyx-fixtures scaffold [options]
27  
28 Options:
29  
30 -e, --environment: specify environment, default is development
31 -h, --help: show help
32 --man man page
33  
34 };
35  
36 sub options {
37 my ($self) = @_;
38 (
39 $super->options,
40 'e|environment=s' => "environment",
41 );
42 }
43  
44 sub before_run {
45 my ($self) = @_;
46  
47 $super->before_run($self);
48  
49 $self->{environment} ||= "development";
50  
51 return;
52 }
53  
54 sub run {
55 my ($self, $args) = @_;
56 $self->before_run();
57  
58 Jifty->new;
59  
60 for my $env (keys %{$self->{config}->{fixtures}}) {
61  
62 my $dir = File::Spec->catfile(
63 $self->{config}->{app_root},
64 $self->{config}->{fixtures}->{$env}->{dir}
65 );
66 mkdir $dir unless (-e $dir);
67  
68  
69 for my $model ($self->model_list) {
70 my $filename = $self->fixtures_filename($env ,$model, "yml");
71 my $file = IO::File->new ;
72 if (defined $file->open("> $filename") ) {
73 print $file $self->render_scaffold(Jifty->app_class("Model",$model)->columns);
74 $file->close;
75 }
76 }
77  
78 }
79  
80 }
81  
82 sub render_scaffold {
83 my ($self, @columns) = @_;
84 my $result = "-\n";
85 for (@columns) {
86 $result .= " " . $_->name . ":\n" if $_->{writable};
87 }
88 my $header = $result;
89 $header =~ s/^/#/g;
90 $header =~ s/\n/\n#/g;
91 $header =~ s/#$//g;
92  
93 $header . $result;
94 }
95  
96 sub model_list {
97 my ($self) = @_;
98 my @result = map { basename($_) } glob(
99 File::Spec->catfile(
100 $self->{config}->{app_root},
101 "lib",
102 $self->{config}->{framework}->{ApplicationClass},
103 "Model",
104 "*"
105 )
106 );
107 for (@result) {
108 $_ =~ s/\.pm//g;
109 }
110 @result;
111 }
112  
113 sub fixtures_filename {
114 my ($self, $environment, $model, $format) = @_;
115 return File::Spec->catfile(
116 $self->{config}->{app_root},
117 $self->{config}->{fixtures}->{$environment}->{dir},
118 "$model.$format"
119 );
120 }
121  
122  
123 1;
124  
125 __END__