Config::Model::AnyId - Base class for hash or list element
version 1.242
use Config::Model; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($WARN); # define configuration tree object my $model = Config::Model->new; $model->create_config_class( name => "Foo", element => [ [qw/foo bar/] => { type => 'leaf', value_type => 'string' }, ] ); $model->create_config_class( name => "MyClass", element => [ plain_hash => { type => 'hash', index_type => 'string', cargo => { type => 'leaf', value_type => 'string', }, }, bounded_hash => { type => 'hash', # hash id index_type => 'integer', # hash boundaries min_index => 1, max_index => 123, max_nb => 2, # specify cargo held by hash cargo => { type => 'leaf', value_type => 'string' }, }, bounded_list => { type => 'list', # list id max_index => 123, cargo => { type => 'leaf', value_type => 'string' }, }, hash_of_nodes => { type => 'hash', # hash id index_type => 'string', cargo => { type => 'node', config_class_name => 'Foo' }, }, ], ); my $inst = $model->instance( root_class_name => 'MyClass' ); my $root = $inst->config_root; # put data my $step = 'plain_hash:foo=boo bounded_list=foo,bar,baz bounded_hash:3=foo bounded_hash:30=baz hash_of_nodes:"foo node" foo="in foo node" - hash_of_nodes:"bar node" bar="in bar node" '; $root->load( step => $step ); # dump resulting tree print $root->dump_tree;
This class provides hash or list elements for a Config::Model::Node.
The hash index can either be en enumerated type, a boolean, an integer or a string.
AnyId object should not be created directly.
A hash or list element must be declared with the following parameters:
Mandatory element type. Must be hash or list to have a collection element. The actual element type must be specified by cargo = type> (See "CAVEATS").
hash
list
cargo =
Either integer or string. Mandatory for hash.
integer
string
Whether to keep the order of the hash keys (default no). (a bit like Tie::IxHash). The hash keys are ordered along their creation. The order can be modified with swap, move_up or move_down.
Hash ref specifying the cargo held by the hash of list. This has must contain:
Can be node or leaf (default).
node
leaf
Specifies the type of configuration object held in the hash. Only valid when cargo type is node.
cargo
type
Constructor arguments passed to the cargo object. See Config::Model::Node when cargo->type is node. See Config::Model::Value when cargo->type is leaf.
cargo->type
Specify the minimum value (optional, only for hash and for integer index)
Specify the maximum value (optional, only for integer index)
Specify the maximum number of indexes. (hash only, optional, may also be used with string index type)
When set, the default parameter (or set of parameters) are used as default keys hashes and created automatically when the keys or exists functions are used on an empty hash..
keys
exists
You can use default_keys => 'foo', or default_keys => ['foo', 'bar'].
default_keys => 'foo'
default_keys => ['foo', 'bar']
To perform special set-up on children nodes you can also use
default_with_init => { 'foo' => 'X=Av Y=Bv' , 'bar' => 'Y=Av Z=Cv' }
When the hash contains leaves, you can also use:
default_with_init => { 'def_1' => 'def_1 stuff' , 'def_2' => 'def_2 stuff' }
Specifies that the keys of the hash or list are copied from another hash or list in the configuration tree only when the hash is created.
migrate_keys_from => '- another_hash_or_list'
Specifies that the keys of the hash follow the keys of another hash in the configuration tree. In other words, the hash you're creating will always have the same keys as the other hash.
follow_keys_from => '- another_hash'
Specifies authorized keys:
allow_keys => ['foo','bar','baz']
A bit like the follow_keys_from parameters. Except that the hash pointed to by allow_keys_from specified the authorized keys for this hash.
follow_keys_from
allow_keys_from
allow_keys_from => '- another_hash'
Keys must match the specified regular expression. For instance:
allow_keys_matching => '^foo\d\d$'
When set, the default parameter (or set of parameters) are used as keys hashes and created automatically. (valid only for hash elements)
Called with auto_create_keys => ['foo'], or auto_create_keys => ['foo', 'bar'].
auto_create_keys => ['foo']
auto_create_keys => ['foo', 'bar']
Issue a warning if the key matches the specified regular expression
Issue a warning unless the key matches the specified regular expression
Specifies the number of elements to create automatically. E.g. auto_create_ids => 4 will initialize the list with 4 undef elements. (valid only for list elements)
auto_create_ids => 4
See "Warp: dynamic value configuration" below.
The Warp functionality enables an HashId or ListId object to change its default settings (e.g. min_index, max_index or max_nb parameters) dynamically according to the value of another Value object. (See Config::Model::WarpedThing for explanation on warp mechanism)
min_index
max_index
max_nb
Value
For instance, with this model:
$model ->create_config_class ( name => 'Root', 'element' => [ macro => { type => 'leaf', value_type => 'enum', name => 'macro', choice => [qw/A B C/], }, warped_hash => { type => 'hash', index_type => 'integer', max_nb => 3, warp => { follow => '- macro', rules => { A => { max_nb => 1 }, B => { max_nb => 2 } } }, cargo => { type => 'node', config_class_name => 'Dummy' } }, ] );
Setting macro to A will mean that warped_hash can only accept one instance of Dummy.
macro
A
warped_hash
Dummy
Setting macro to B will mean that warped_hash will accept two instances of Dummy.
B
Like other warped class, a HashId or ListId can have multiple warp masters (See "Warp follow argument" in Config::Model::WarpedThing:
warp => { follow => { m1 => '- macro1', m2 => '- macro2' }, rules => [ '$m1 eq "A" and $m2 eq "A2"' => { max_nb => 1}, '$m1 eq "A" and $m2 eq "B2"' => { max_nb => 2} ], }
When a warp is applied with auto_create_keys or auto_create_ids parameter, the auto_created items are created if they are not already present. But this warp will never remove items that were previously auto created.
auto_create_keys
auto_create_ids
For instance, if a tied hash is created with auto_create => [a,b,c], the hash contains (a,b,c).
auto_create => [a,b,c]
(a,b,c)
Then if a warp is applied with auto_create_keys => [c,d,e], the hash will contain (a,b,c,d,e). The items created by the first auto_create_keys are not removed.
auto_create_keys => [c,d,e]
(a,b,c,d,e)
When a warp is applied, the items that do not fit the constraint (e.g. min_index, max_index) are removed.
For the max_nb constraint, an exception will be raised if a warp leads to a number of items greater than the max_nb constraint.
The following methods returns the current value stored in the Id object (as declared in the model unless they were warped):
Returns the object type contained by the hash or list (i.e. returns cargo -> type).
cargo -> type
Returns more info on the cargo contained by the hash or list. what may be value_type or any other cargo info stored in the model. Will return undef if the requested info was not provided in the model.
what
value_type
Returns a list (or a list ref) of the current default keys. These keys can be set by the default_keys or default_with_init parameters or by the other hash pointed by follow_keys_from parameter.
default_keys
default_with_init
Returns the object name. The name finishes with ' id'.
Returns the config_class_name of collected elements. Valid only for collection of nodes.
This method will return undef if cargo type is not node.
Fetch the collected element held by the hash or list. Index check is 'yes' by default. Can be called with one parameter which will be used as index.
get( path =
Get a value from a directory like path.
Set a value with a directory like path.
Deep copy an element within the hash or list. If the element contained by the hash or list is a node, all configuration information is copied from one node to another.
Returns an array containing all elements held by the hash or list.
Returns an array containing all defined values held by the hash or list. (undefined values are simply discarded). This method is only valid for hash or list containing leaves.
With mode parameter, this method will return either:
mode
The value entered by the user
The value entered in preset mode
The value entered in preset mode or checked by default.
The default value (defined by the configuration model)
Returns an array containing all indexes of the hash or list. Hash keys are sorted alphabetically, except for ordered hashed.
Like get_all_indexes. This method is polymorphic for all non-leaf objects of the configuration tree.
Returns true if the value held at index is defined.
index
Returns true if the value held at index exists (i.e the key exists but the value may be undefined). This method may not make sense for list element.
Delete the indexed value
Delete all values (also delete underlying value or node objects).
Delete all values (without deleting underlying value objects).
Returns warnings concerning indexes of this hash. Without parameter, returns a hash ref or undef. With an index, return the warnings concerning this index or undef.
Returns the error messages of this object (if any)
Dominique Dumont, ddumont [AT] cpan [DOT] org
Config::Model, Config::Model::Instance, Config::Model::Node, Config::Model::WarpedNode, Config::Model::HashId, Config::Model::ListId, Config::Model::CheckList, Config::Model::Value
To install Config::Model, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Config::Model
CPAN shell
perl -MCPAN -e shell install Config::Model
For more information on module installation, please visit the detailed CPAN module installation guide.