Skip to content
Browse files

Refactor common code from get and get_all

  • Loading branch information...
1 parent dd31398 commit 609204e0a655803d2d1225870439b1576d6d8c52 @alexmv alexmv committed
Showing with 25 additions and 28 deletions.
  1. +25 −28 lib/Config/GitLike.pm
View
53 lib/Config/GitLike.pm
@@ -495,12 +495,10 @@ sub cast {
}
}
-sub get {
+sub _get {
my $self = shift;
my %args = (
- key => undef,
- as => undef,
- human => undef,
+ key => undef,
filter => '',
@_,
);
@@ -511,7 +509,7 @@ sub get {
grep { defined } (lc $section, $subsection, lc $name),
);
- return undef unless exists $self->data->{$args{key}};
+ return () unless exists $self->data->{$args{key}};
my $v = $self->data->{$args{key}};
my @values = ref $v ? @{$v} : ($v);
if (defined $args{filter} and length $args{filter}) {
@@ -522,10 +520,7 @@ sub get {
@values = grep { m/$args{filter}/i } @values;
}
}
- die "Multiple values" unless @values <= 1;
-
- return $self->cast( value => $values[0], as => $args{as},
- human => $args{human} );
+ return @values;
}
# I'm pretty sure that someone can come up with an edge case where stripping
@@ -541,32 +536,34 @@ sub _remove_balanced_quotes {
return $key;
}
-sub get_all {
+sub get {
my $self = shift;
my %args = (
- key => undef,
- as => undef,
+ key => undef,
+ as => undef,
+ human => undef,
+ filter => '',
@_,
);
- $self->load unless $self->is_loaded;
- my ($section, $subsection, $name) = _split_key($args{key});
- $args{key} = join('.',
- grep { defined } (lc $section, $subsection, lc $name),
- );
- return undef unless exists $self->data->{$args{key}};
- my $v = $self->data->{$args{key}};
- my @v = ref $v ? @{$v} : ($v);
+ my @v = $self->_get( %args );
+ return undef unless @v;
+ die "Multiple values" if @v > 1;
- if (defined $args{filter} and length $args{filter}) {
- if ($args{filter} =~ s/^!//) {
- @v = grep { !/$args{filter}/i } @v;
- }
- else {
- @v = grep { m/$args{filter}/i } @v;
- }
- }
+ return $self->cast( value => $v[0], as => $args{as},
+ human => $args{human} );
+}
+
+sub get_all {
+ my $self = shift;
+ my %args = (
+ key => undef,
+ as => undef,
+ filter => '',
+ @_,
+ );
+ my @v = $self->_get( %args );
@v = map {$self->cast( value => $_, as => $args{as} )} @v;
return wantarray ? @v : \@v;
}

0 comments on commit 609204e

Please sign in to comment.
Something went wrong with that request. Please try again.