Commit cfa5206f authored by Bernard Quatermass's avatar Bernard Quatermass

Expose defaults merge routine as public method

parent 015b2271
......@@ -4,7 +4,7 @@
# VERSION
version 2.0
version 2.2
# SYNOPSIS
......@@ -59,6 +59,16 @@ initialising data in normal hash list format (or even a mix).
> my $ifile = InterestingFile->new( 'filename' => 'something.real', $inherited_hash_ref );
# Methods
There is only one helper method apart from new
## merge\_stacked\_options
This is the routine that takes the set of stacked initialisation options and merges
them into one hash. It is provided as a helper method for objects that want to perform
an equivalent merge.
# COMPATIBILITY
- **Class::Accessorize** requires **Class::Accessor** (quelle surprise).
......@@ -69,7 +79,7 @@ Bernard Quatermass <toolsmith@quatermass.co.uk>
# COPYRIGHT AND LICENSE
This software is copyright (c) 2017, 2018 by Bernard Quatermass.
This software is copyright (c) 2017, 2018, 2020 by Bernard Quatermass.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
Name: perl-Class-Accessorize
Version: 2.0
Version: 2.2
Release: 1%{?dist}
Summary: defaults wrapper around Class::Accessor
......@@ -52,7 +52,10 @@ make test
%changelog
* Mon Jan 27 2020 Bernard Quatermass <toolsmith@quatermass.co.uk>> - 2.0-1
* Tue Jan 28 2020 Bernard Quatermass <toolsmith@quatermass.co.uk>> - 2.2-1
- Expose the defaults merge as specific method
* Mon Jan 27 2020 Bernard Quatermass <toolsmith@quatermass.co.uk>> - 2.1-1
- Allow for non-hashref defaults
* Tue Oct 2 2018 Bernard Quatermass <toolsmith@quatermass.co.uk>> - 1.1-1
......
......@@ -5,7 +5,7 @@ use strict;
package Class::Accessorize;
$Class::Accessorize::VERSION='2.0';
$Class::Accessorize::VERSION='2.2';
use Class::Accessor;
use base qw(Class::Accessor);
......@@ -24,14 +24,26 @@ new
{
my $protoverse = shift;
my $class = ref($protoverse) || $protoverse;
my $self = shift || {};
my $self = Class::Accessorize->merge_stacked_options( @_, {} );
return bless $self, $class;
}
# Overlay all undefined keys
# V2.0 - get flexible and allow for non-hash-ref defaults.
# on the observation that if the first item is not a hash ref
# then it must be a hash key(scalar) followed by the value for the key
# So we either transfer the hash contents, or take a key/value pair
# off the list and set accordingly.
sub
merge_stacked_options
{
my $self = shift; # and discard
# Overlay all undefined keys
# V2.0 - get flexible and allow for non-hash-ref defaults.
# on the observation that if the first item is not a hash ref
# then it must be a hash key(scalar) followed by the value for the key
# So we either transfer the hash contents, or take a key/value pair
# off the list and set accordingly.
my ( $a, $k, $v );
$a = {};
while( $k = shift @_ )
{
......@@ -42,17 +54,21 @@ new
{
foreach( keys %$k )
{
$self->{$_} = $k->{$_}
if ! exists $self->{$_};
$a->{$_} = $k->{$_}
if ! exists $a->{$_};
}
}
else
{
$self->{$k} = shift @_
if @_;
if (@_)
{
$v = shift @_;
$a->{$k} = $v
if ! exists $a->{$k};
}
}
};
return bless $self, $class;
return $a;
}
1;
......@@ -65,7 +81,7 @@ B<Class::Accessorize> - Wrapper for B<Class::Accessor> to allow defaults
=head1 VERSION
version 2.0
version 2.2
=head1 SYNOPSIS
......@@ -124,6 +140,16 @@ initialising data in normal hash list format (or even a mix).
=back
=head1 Methods
There is only one helper method apart from new
=head2 merge_stacked_options
This is the routine that takes the set of stacked initialisation options and merges
them into one hash. It is provided as a helper method for objects that want to perform
an equivalent merge.
=head1 COMPATIBILITY
=over 4
......@@ -150,4 +176,4 @@ the same terms as the Perl 5 programming language system itself.
# vi: aw ai sw=4 et
###############################################################
## End of File
###############################################################
\ No newline at end of file
###############################################################
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment