|
NAMEConfig::Model::Backend::Any - Virtual class for other backendsVERSIONversion 2.149SYNOPSISpackage Config::Model::Backend::Foo ; use Mouse ; extends 'Config::Model::Backend::Any'; # mandatory sub read { my $self = shift ; my %args = @_ ; # args are: # root => './my_test', # fake root directory, used for tests # config_dir => /etc/foo', # absolute path # file => 'foo.conf', # file name # file_path => Path::Tiny object for './my_test/etc/foo/foo.conf' # check => yes|no|skip return 0 unless $args{file_path}->exists ; # or die, your choice # read the file line by line # we assume the file contain lines like 'key=value' foreach ($args{file_path}->lines_utf8) { chomp ; # remove trailing \n s/#.*// ; # remove any comment next unless /\S/; # skip blank line # $data is 'foo=bar' which is compatible with load $self->node->load(steps => $_, check => $args{check} ) ; } return 1 ; } # mandatory sub write { my $self = shift ; my %args = @_ ; # args are: # root => './my_test', # fake root directory, used for tests # config_dir => /etc/foo', # absolute path # file => 'foo.conf', # file name # file_path => Path::Tiny object for './my_test/etc/foo/foo.conf' # check => yes|no|skip # read the content of the configuration tree my @lines; foreach my $elt ($self->node->children) { # read the value from element $elt my $v = $self->node->grab_value($elt) ; # write value in file push @lines,qq!$elt="$v"\n! if defined $v ; } $args{file_path}->spew_utf8(@lines); return 1; } DESCRIPTIONSome application have configuration files with a syntax which is not supported by existing "Config::Model::Backend::*" classes.In this case a new backend must be written. "Config::Model::Backend::Any" was created to facilitate this task. The new backend class must use Mouse and must extends (inherit) "Config::Model::Backend::Any". How to write your own backendDeclare the new backend in a node of the modelAs explained in "Backend specification" in Config::Model::BackendMgr, the new backend must be declared as an attribute of a Config::Model::Node specification.Let's say your new backend is "Config::Model::Backend::Foo". This new backend can be specified with: rw_config => { backend => 'Foo' , # can also be 'foo' config_dir => '/etc/cfg_dir' file => 'foo.conf', # optional } (The backend class name is constructed with "ucfirst($backend_name)") "rw_config" can also have custom parameters that are passed verbatim to "Config::Model::Backend::Foo" methods: rw_config => { backend => 'Foo' , # can also be 'foo' config_dir => '/etc/cfg_dir' file => 'foo.conf', # optional my_param => 'my_value', } "Config::Model::Backend::Foo" class must inherit (extend) Config::Model::Backend::Any and is expected to provide the following methods:
How to test your new backendUsing Config::Model::Tester, you can test your model with your backend following the instructions given in Config::Model::Tester.You can also test your backend with a minimal model (and Config::Model::Tester). In this case, you need to specify a small model to test in a "*-test-conf.pl" file. See the IniFile backend test <https://github.com/dod38fr/config-model/blob/master/t/model_tests.d/backend-ini-test-conf.pl> for an example and its examples files <https://github.com/dod38fr/config-model/tree/master/t/model_tests.d/backend-ini-examples>. CONSTRUCTORnewThe constructor should be used only by Config::Model::Node.Parameter:
Methods to overrideannotationWhether the backend supports reading and writing annotation (a.k.a comments). Default is 0. Override this method to return 1 if your backend supports annotations.readRead the configuration file. This method must be overridden.writeWrite the configuration file. This method must be overridden.MethodsnodeReturn the node (a Config::Model::Node) holding this backend.instanceReturn the instance (a Config::Model::Instance) holding this configuration.show_messageParameters: "( string )"Show a message to STDOUT (unless overridden). Delegated to "show_message" in Config::Model::Instance. read_global_commentsParameters:
Read the global comments (i.e. the first block of comments until the first blank or non comment line) and store them as root node annotation. Note that the global comment must be separated from the first data line by a blank line. Example: $self->read_global_comments( \@lines, ';'); $self->read_global_comments( \@lines, '#;'); associates_comments_with_dataParameters:
This method extracts comments from the passed lines and associate them with actual data found in the file lines. Data is associated with comments preceding or on the same line as the data. Returns a list of [ data, comment ]. Example: my @lines = ( '# Foo comments', 'foo= 1', 'Baz = 0 # Baz comments' ); my @res = $self->associates_comments_with_data( \@lines, '#') # @res is: # ( [ 'foo= 1', 'Foo comments' ] , [ 'Baz = 0' , 'Baz comments' ] ) write_global_commentsReturn a string containing global comments using data from configuration root annotation.Requires one parameter: comment_char (e.g "#" or '//' ) Example: my $str = $self->write_global_comments('#') write_data_and_commentsReturns a string containing comments (stored in annotation) and corresponding data. Comments are written before the data. If a data is undef, the comment is written on its own line.Positional parameters are "( comment_char , data1, comment1, data2, comment2 ...)" Example: print $self->write_data_and_comments('#', 'foo', 'foo comment', undef, 'lone comment','bar') # returns "# foo comment\nfoo\n#lon Use "undef" as comment char if comments are not supported by the syntax of the configuration file. Comments will then be dropped. Replacing a custom backendCustom backend are now deprecated and must be replaced with a class inheriting this module.Please:
Here's an example of such a change <https://github.com/dod38fr/config-model/commit/c3b7007ad386cb2356c5ac1499fe51bdf492b19a>. AUTHORDominique Dumont, (ddumont at cpan dot org)SEE ALSOConfig::Model, Config::Model::BackendMgr, Config::Model::Node,AUTHORDominique DumontCOPYRIGHT AND LICENSEThis software is Copyright (c) 2005-2022 by Dominique Dumont.This is free software, licensed under: The GNU Lesser General Public License, Version 2.1, February 1999
Visit the GSP FreeBSD Man Page Interface. |