Crypt-HSXKPasswd

 view release on metacpan or  search on metacpan

lib/Crypt/HSXKPasswd.pm  view on Meta::CPAN

        _error('the config passed must be a hashref or a JSON string');
    }
    
    # distil the alphabets in the new config
    $_CLASS->_distil_alphabets_inplace($config);
    
    # save a clone of the passed config into the instance
    $self->{_CONFIG} = $_CLASS->clone_config($config);
    
    # update the instance's entropy cache
    $self->_update_entropystats_cache();
    
    # return a reference to self to facilitate function chaining
    return $self;
}

#####-SUB-#####################################################################
# Type       : INSTANCE
# Purpose    : Return the config of the currently running instance as a JSON
#              string.
# Returns    : A scalar.
# Arguments  : NONE
# Throws     : Croaks if invoked in an invalid way. Carps if it meets a key of a
#              type not accounted for in the code.
# Notes      : This function will carp if the JSON module is not available
# See Also   :
sub config_as_json{
    my $self = shift;
    _force_instance($self);
    
    # assemble and return the JSON string
    return $_CLASS->config_to_json($self->{_CONFIG}); # will croak without JSON
}

#####-SUB-#####################################################################
# Type       : INSTANCE
# Purpose    : Return the config of the currently running instance as a string.
# Returns    : A scalar.
# Arguments  : NONE
# Throws     : Croaks if invoked in an invalid way. Carps if it meets a key of a
#              type not accounted for in the code.
# Notes      :
# See Also   :
sub config_as_string{
    my $self = shift;
    _force_instance($self);
    
    # assemble and return the string
    return $_CLASS->config_to_string($self->{_CONFIG});
}

#####-SUB-######################################################################
# Type       : INSTANCE
# Purpose    : Alter the running config with new values.
# Returns    : A reference to the instalce itself to enable function chaining.
# Arguments  : 1. a hashref containing config keys and values.
# Throws     : Croaks on invalid invocaiton, invalid args, and, if the resulting
#              new config is in some way invalid.
# Notes      : Invalid keys in the new keys hashref will be silently ignored.
# See Also   :
sub update_config{
    my @args = @_;
    my $self = shift @args;
    _force_instance($self);
    
    # validate args
    state $args_check = compile(ConfigOverride);
    my ($new_keys) = $args_check->(@args);
    
    # clone the current config as a starting point for the new config
    my $new_config = $self->_clone_config();
    
    # get a reference to the key definitions from the types class
    my $defined_keys = $_TYPES_CLASS->_config_keys();
    
    # merge the new values into the config
    my $num_keys_updated = 0;
    CONFIG_KEY:
    foreach my $key ($_CLASS->defined_config_keys()){
        # skip the key if it's not present in the list of new keys
        next CONFIG_KEY unless defined $new_keys->{$key};
        
        # update the key in the new config
        $new_config->{$key} = $new_keys->{$key};
        $num_keys_updated++;
        _debug("updated $key to new value");
    }
    _debug("updated $num_keys_updated keys");
    
    # distil the alphabets in the merged config
    $_CLASS->_distil_alphabets_inplace($new_config);
    
    # validate the merged config
    unless(Config->check($new_config)){
        _error('the updated config is not valid: '.Config->get_message($new_config));
    }
    
    # re-calculate the dictionary cache if needed
    my @cache_all = @{$self->{_CACHE_DICTIONARY_FULL}};
    my @cache_limited = @{$self->{_CACHE_DICTIONARY_LIMITED}};
    if($new_config->{word_length_min} ne $self->{_CONFIG}->{word_length_min} || $new_config->{word_length_max} ne $self->{_CONFIG}->{word_length_max}){
        # re-build the cache of valid words - throws an error if too few words are returned
        @cache_limited = $_CLASS->_filter_word_list(\@cache_all, $new_config->{word_length_min}, $new_config->{word_length_max}, $new_config->{allow_accents});
    }
    
    # if we got here, all is well with the new config, so add it and the caches to the instance
    $self->{_CONFIG} = $new_config;
    $self->{_CACHE_DICTIONARY_LIMITED} = [@cache_limited];
    
    # update the instance's entropy cache
    $self->_update_entropystats_cache();
    
    # return a reference to self
    return $self;
}

#####-SUB-#####################################################################
# Type       : INSTANCE
# Purpose    : Get the currently loaded RNG object, or, load a new RNG
# Returns    : An instance to the loaded RNG object, or, a reference to the
#              instance (to enable function chaining) if called as a setter



( run in 0.481 second using v1.01-cache-2.11-cpan-39bf76dae61 )