EntityModel/App.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« EntityModel/Definition/XML.pm ↑ Summary EntityModel/Async.pm »
1 package EntityModel::App;
2 {
3 $EntityModel::App::VERSION = '0.102';
4 }
5 2 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 model => { type => 'EntityModel::Model' },
7 2 };
8 2 ▶use EntityModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use EntityModel::Definition::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use EntityModel::Definition::XML;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Module::Load;
12  
13 =head1 NAME
14  
15 EntityModel::App - interface to L<EntityModel> admin application
16  
17 =head1 VERSION
18  
19 version 0.102
20  
21 =head1 SYNOPSIS
22  
23 see L<EntityModel>.
24  
25 =head1 DESCRIPTION
26  
27 see L<EntityModel>.
28  
29  
30 =head1 METHODS
31  
32 =cut
33  
34 =head2 show_model
35  
36 Display information about the current model.
37  
38 =cut
39  
40 sub show_model {
41 my $self = shift;
42 my $model = $self->model;
43 $self->show(
44 model_info => {
45 model => $model,
46 name => $model->name,
47 entity => $model->entity,
48 plugin => $model->plugin,
49 }
50 );
51 }
52  
53 =head2 show_model_info
54  
55 =cut
56  
57 sub show_model_info {
58 my ($self, $info) = @_;
59 print "Model " . $info->{model}->name . " has " . $info->{entity}->count . " entities:\n";
60 printf " * %s has fields %s\n", $_->name, join ',', map $_->name, $_->field->list for $info->{entity}->list;
61 foreach my $plugin ($info->{plugin}->list) {
62 $self->show(plugin_info => { plugin => $plugin });
63 }
64 }
65  
66 =head2 show_plugin_info
67  
68 =cut
69  
70 sub show_plugin_info {
71 my ($self, $info) = @_;
72 my $plugin = $info->{plugin};
73 print "Plugin " . ref($plugin) . ":\n";
74 foreach my $site ($plugin->site->list) {
75 print " * Site " . $site->host . ":\n";
76 printf " * Template: %s\n", ($site->template // 'undef');
77 # try { printf " * Layout: %s => %s\n", $_->section, $_->wrapper for $site->layout->list; } catch { warn "$_"; };
78 printf " * Page: %s for %s\n", $_->name, $_->path for $site->page->list;
79 }
80 }
81  
82 =head2 show
83  
84 =cut
85  
86 sub show {
87 my $self = shift;
88 my $k = shift;
89 $self->${\"show_$k"}(@_);
90 return $self;
91 }
92  
93 sub create_model {
94 my $self = shift;
95 die "have a model already" if $self->model;
96  
97 # Bring in any extra plugins we might have available
98 # FIXME should be dynamic
99 my $model = EntityModel->new;
100 foreach my $type (qw(Web)) {
101 try {
102 my $class = "EntityModel::$type";
103 load($class);
104 my $instance = $class->new;
105 $model->add_plugin($instance);
106 } catch {
107 warn "Failed to load $type - $_";
108 };
109 }
110 $self->model($model);
111 $model;
112 }
113  
114 sub load_model {
115 my $self = shift;
116 my $file = shift;
117 my $model = $self->model || $self->create_model;
118  
119 if($file =~ /\.json$/) {
120 $model->load_from(
121 JSON => { file => $file }
122 );
123 } elsif($file =~ /\.xml$/) {
124 $model->load_from(
125 XML => { file => $file }
126 );
127 } else {
128 die "Unknown extension, expected .json or .xml: - $file\n";
129 }
130 $self->model($model);
131 return $self;
132 }
133  
134 {# %arg_mapping
135 my %arg_mapping = (
136 '-f' => sub {
137 my $self = shift;
138 my %args = @_;
139 my $file = shift @{$args{args}};
140 $self->load_model($file);
141 return $self;
142 },
143  
144 'list' => sub {
145 my $self = shift;
146 $self->show_model;
147 },
148  
149 'export' => sub {
150 my $self = shift;
151 my $def = EntityModel::Definition::JSON->new;
152 $def->model($self->model);
153 print $def->save(string => '');
154 },
155  
156 'merge' => sub {
157 my $self = shift;
158 my %args = @_;
159 my $input = shift @{$args{args}};
160  
161 my $name = $self->model->name;
162 if($input eq '-') {
163 $self->load_model(*STDIN);
164 } else {
165 $self->load_model($input);
166 }
167  
168 # Reset name if we had one
169 $self->model->name($name) if $name;
170 my $def = EntityModel::Definition::JSON->new;
171 $def->model($self->model);
172 print "Merged with $input\n";
173 },
174  
175 ui => sub {
176 my $self = shift;
177 print "entitymodel> ";
178 COMMAND:
179 while(my $line = <STDIN>) {
180 chomp $line;
181 my ($cmd, @args) = split ' ', $line;
182 last COMMAND if $cmd eq 'quit';
183  
184 try {
185 if($cmd eq 'read') {
186 $self->model(undef);
187 $cmd = '-f';
188 }
189 $self->from_argv($cmd, @args);
190 } catch {
191 warn "Failed to run [$cmd" . (@args ? " @args" : "") . "]: $_";
192 };
193 } continue {
194 print "entitymodel> ";
195 }
196 print "\n";
197 }
198 );
199  
200 =head2 from_argv
201  
202 =cut
203  
204 sub from_argv {
205 my $self = shift;
206 my @argv = @_;
207  
208 ARG:
209 while(@argv) {
210 my $code = $self->code_for_entry(shift @argv);
211 $self->$code(args => \@argv);
212 }
213 $self;
214 }
215  
216 sub code_for_entry {
217 my ($self, $k) = @_;
218 if(my $code = $arg_mapping{$k}) {
219 return $code;
220 } else {
221 die "Unknown parameter '$k'";
222 }
223 }
224  
225 }# %arg_mapping
226  
227 __PACKAGE__->new->from_argv(@ARGV) unless caller;
228  
229 1;
230  
231 __END__
« EntityModel/Definition/XML.pm ↑ Summary EntityModel/Async.pm »