Skip to content

Commit

Permalink
refactor some internals to get rid of lots of repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Dieter Pearcey authored and jettero committed Jul 29, 2009
1 parent fd99391 commit a6ed3c2
Showing 1 changed file with 26 additions and 38 deletions.
64 changes: 26 additions & 38 deletions CuteQueries.pm
Original file line number Diff line number Diff line change
Expand Up @@ -192,50 +192,38 @@ sub _execute_query {
return $_trimlist->( map { $_->{att}{$attr_query} } @c );
}

if( $context == KLIST ) {
if( $kar ) {
my %h;

if( $mt eq "t" ) { push @{$h{$_->gi}}, $_ for @c }
elsif( $mt eq "x" ) { push @{$h{$_->gi}}, $_->xml_string for @c }
elsif( $mt eq "r" ) { push @{$h{$_->gi}}, $_->text for @c }
else { push @{$h{$_->gi}}, $_->text_only for @c }

unless( $mt eq "t" ) {
for my $v (values %h) {
$_trimlist->( @$v );
}
}

return %h;

} elsif( $opts->{nostrict_single} ) {
return map { $_->gi => $_ } @c if $mt eq "t";
return $_trimhash->( map { $_->gi => $_->xml_string } @c ) if $mt eq "x";
return $_trimhash->( map { $_->gi => $_->text } @c ) if $mt eq "r";
return $_trimhash->( map { $_->gi => $_->text_only } @c );
my $get_value = {
t => sub { $_[0] },
x => 'xml_string',
r => 'text',
0 => 'text_only'
}->{$mt};

if ($mt eq 't') {
$_trimlist = $_trimhash = sub { @_ };
}

} else {
my %check;
my $mygi = sub {
my $g = $_[0]->gi;
return $_trimlist->( map { $_->$get_value } @c ) unless $context == KLIST;

$this->_data_error($rt, "expected exactly one match-per-tagname for \"$query\", got more")
if $check{$g}++;
my %h;

$g;
};
return map { $_->$mygi => $_ } @c if $mt eq "t";
return $_trimhash->( map { $_->$mygi => $_->xml_string } @c ) if $mt eq "x";
return $_trimhash->( map { $_->$mygi => $_->text } @c ) if $mt eq "r";
return $_trimhash->( map { $_->$mygi => $_->text_only } @c );
for (@c) {
my $arr = $h{$_->gi} ||= [];
# discard all but the last result
@$arr = () if $opts->{nostrict_single};
push @$arr, $_->$get_value;
unless ($kar || @$arr == 1) {
$this->_data_error($rt, "expected exactly one match-per-tagname for \"$query\", got more")
}
}

return @c if $mt eq "t";
return $_trimlist->( map { $_->xml_string } @c ) if $mt eq "x";
return $_trimlist->( map { $_->text } @c ) if $mt eq "r";
return $_trimlist->( map { $_->text_only } @c );
if ($kar) {
$_trimlist->( @$_ ) for values %h;
return %h;
} else {
$_ = $_->[-1] for values %h;
return $_trimhash->(%h);
}

} elsif( $rt eq "HASH" ) {
if( $context == KLIST ) {
Expand Down

0 comments on commit a6ed3c2

Please sign in to comment.