Config-Model-Itself
view release on metacpan or search on metacpan
lib/App/Cme/Command/meta.pm view on Meta::CPAN
[ "plugin-file=s" => "create a model plugin in this file" ],
[ "load-yaml=s" => "load model from YAML file. Use '-' to load from STDIN" ],
[ "load=s" => "load model from cds file (Config::Model serialisation file). "
."Use '-' to load from STDIN"],
[ "system!" => "read model from system files" ],
[ "test-and-quit=s" => "Used for tests" ],
$class->cme_global_options()
);
}
sub usage_desc {
my ($self) = @_;
my $desc = $self->SUPER::usage_desc; # "%c COMMAND %o"
return "$desc [ ".join(' | ', sort keys %meta_cmd)." ] your_model_class ";
}
sub description {
my ($self) = @_;
return $self->get_documentation;
}
sub read_data {
my $load_file = shift ;
my @data ;
if ( $load_file eq '-' ) {
## no critic (InputOutput::ProhibitExplicitStdin)
# user called cme with -load - or -load-yaml -, cannot use ARGV
@data = <STDIN> ;
}
else {
open my $load, '<', $load_file || die "cannot open load file $load_file:$!";
@data = <$load> ;
close $load;
}
return wantarray ? @data : join('',@data);
}
sub load_optional_data {
my ($self, $args, $opt, $root_model, $meta_root) = @_;
if (defined $opt->{load}) {
my $data = read_data($opt->{load}) ;
$data = qq(class:"$root_model" ).$data unless $data =~ /^\s*class:/ ;
$meta_root->load($data);
}
if (defined $opt->{'load-yaml'}) {
my $yaml = read_data($opt->{'load-yaml'}) ;
my $pdata = Load($yaml) ;
$meta_root->load_data($pdata) ;
}
return;
}
sub load_meta_model {
my ($self, $opt, $args) = @_;
my $root_model = $opt->{_root_model};
my $cm_lib_dir = path(split m!/!, $opt->{dir}) ; # replace with cm_lib_dir ???
if (! $cm_lib_dir->is_dir) {
$cm_lib_dir->mkdir();
}
my $meta_model = $self->{meta_model} = Config::Model -> new();
my $meta_inst = $meta_model->instance(
root_class_name => 'Itself::Model',
instance_name => 'meta',
check => $opt->{'force-load'} ? 'no' : 'yes',
);
my $meta_root = $meta_inst -> config_root ;
my $system_cm_lib_dir = $INC{'Config/Model.pm'} ;
$system_cm_lib_dir =~ s/\.pm//;
return ($meta_inst, $meta_root, $cm_lib_dir, path($system_cm_lib_dir));
}
sub load_meta_root {
my ($self, $opt, $args) = @_;
my ($meta_inst, $meta_root, $cm_lib_dir, $system_cm_lib_dir) = $self->load_meta_model($opt,$args);
my $root_model = $opt->{_root_model};
say "Reading model from $system_cm_lib_dir" if $opt->system();
# now load model
my $rw_obj = Config::Model::Itself -> new(
model_object => $meta_root,
cm_lib_dir => $cm_lib_dir->canonpath
);
$meta_inst->initial_load_start ;
my @read_args = (
force_load => $opt->{'force-load'},
root_model => $root_model,
# legacy => 'ignore',
);
if ($opt->system()) {
push @read_args,
application => $opt->{_application},
read_from => $system_cm_lib_dir ;
}
$rw_obj->read_all(@read_args);
$meta_inst->initial_load_stop ;
$self->load_optional_data($args, $opt, $root_model, $meta_root) ;
my $write_sub = sub {
my $wr_dir = shift || $cm_lib_dir ;
$rw_obj->write_all(factorize => $opt->{factorize} // 'none');
return;
} ;
return ($rw_obj, $cm_lib_dir, $meta_root, $write_sub);
( run in 0.337 second using v1.01-cache-2.11-cpan-71847e10f99 )