| 1 | package CSAF::Util::Options; | ||
| 2 | |||
| 3 | use 5.010001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use utf8; | ||
| 7 | |||
| 8 | use YAML::XS 'LoadFile'; | ||
| 9 | |||
| 10 | use Moo::Role; | ||
| 11 | |||
| 12 | $YAML::XS::LoadBlessed = 0; | ||
| 13 | |||
| 14 | has config_file => | ||
| 15 | (is => 'rw', isa => sub { Carp::croak "Unable to open configuration file" unless -e $_[0] }, trigger => 1); | ||
| 16 | |||
| 17 | sub _trigger_config_file { | ||
| 18 | |||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | my $config_data = LoadFile($self->config_file); | ||
| 22 | |||
| 23 | foreach my $config_name (keys %{$config_data}) { | ||
| 24 | |||
| 25 | my $config_value = $config_data->{$config_name}; | ||
| 26 | |||
| 27 | $config_name =~ s/\-/_/; | ||
| 28 | $self->$config_name($config_value) if $self->can($config_name); | ||
| 29 | |||
| 30 | } | ||
| 31 | |||
| 32 | } | ||
| 33 | |||
| 34 | sub configure { | ||
| 35 | |||
| 36 | my ($self, %args) = @_; | ||
| 37 | |||
| 38 | foreach my $method (keys %args) { | ||
| 39 | my $value = $args{$method}; | ||
| 40 | $self->$method($value) if $self->can($method); | ||
| 41 | } | ||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | sub clone { | ||
| 46 | |||
| 47 | my $self = shift; | ||
| 48 | my $clone = {%$self}; | ||
| 49 | |||
| 50 | bless $clone, ref $self; | ||
| 51 | return $clone; | ||
| 52 | |||
| 53 | } | ||
| 54 | |||
| 55 | 1; | ||
| 56 | |||
| 57 | __END__ |