File Coverage

lib/Google/Ads/Common/MapUtils.pm
Criterion Covered Total %
statement 15 27 55.5
branch n/a
condition n/a
subroutine 5 7 71.4
pod 2 2 100.0
total 22 36 61.1


line stmt bran cond sub pod time code
1             # Copyright 2011, Google Inc. All Rights Reserved.
2             #
3             # Licensed under the Apache License, Version 2.0 (the "License");
4             # you may not use this file except in compliance with the License.
5             # You may obtain a copy of the License at
6             #
7             # http://www.apache.org/licenses/LICENSE-2.0
8             #
9             # Unless required by applicable law or agreed to in writing, software
10             # distributed under the License is distributed on an "AS IS" BASIS,
11             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12             # See the License for the specific language governing permissions and
13             # limitations under the License.
14              
15             package Google::Ads::Common::MapUtils;
16              
17 1     1   668 use strict;
  1         2  
  1         27  
18 1     1   5 use warnings;
  1         1  
  1         29  
19 1     1   5 use version;
  1         3  
  1         9  
20              
21             # The following needs to be on one line because CPAN uses a particularly hacky
22             # eval() to determine module versions.
23 1     1   112 use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION};
  1         3  
  1         67  
24              
25 1     1   9 use Exporter 'import';
  1         4  
  1         226  
26              
27             our @EXPORT_OK = qw(get_map build_api_map);
28              
29             # Gets a map (associative array) from an array of map entries. A map entry is
30             # any object that has a key and value field.
31             sub get_map($) {
32 0     0 1   my $map_entries = $_[0];
33 0           my %result = ();
34 0           foreach my $map_entry (@{$map_entries}) {
  0            
35 0           $result{$map_entry->get_key()} = $map_entry->get_value();
36             }
37 0           return \%result;
38             }
39              
40             sub build_api_map {
41 0     0 1   my $map = shift;
42              
43 0           my @result = ();
44 0           foreach my $key (keys %{$map}) {
  0            
45 0           push @result, {key => $key, value => $map->{$key}};
46             }
47              
48 0           return \@result;
49             }
50              
51             return 1;
52              
53             =pod
54              
55             =head1 NAME
56              
57             Google::Ads::Common::MapUtils
58              
59             =head1 SYNOPSIS
60              
61             use Google::Ads::Common::MapUtils;
62              
63             my $map_entries = Google::Ads::Common::MapUtils::get_map($map_xml);
64             # Make use of $map_entries.
65              
66             =head1 DESCRIPTION
67              
68             A collection of utility methods for working with maps (associative arrays).
69              
70             =head1 SUBROUTINES
71              
72             =head2 get_map
73              
74             Gets a map (associative array) from an array of map entries. A map entry is any
75             object that has a key and value field.
76              
77             =head3 Parameters
78              
79             An array of map entries.
80              
81             =head3 Returns
82              
83             A map built from the keys and values of the map entries.
84              
85             =head2 build_api_map
86              
87             Builds an API map as defined in the WSDLs, which is really a list of key-value
88             maps from a given native Perl map.
89              
90             =head3 Parameters
91              
92             A native Perl map to be translated to "map" as expected by the API.
93              
94             =head3 Returns
95              
96             A list of maps in which each map represents an entry of a key and value from the
97             given map.
98              
99             =head1 LICENSE AND COPYRIGHT
100              
101             Copyright 2011 Google Inc.
102              
103             Licensed under the Apache License, Version 2.0 (the "License");
104             you may not use this file except in compliance with the License.
105             You may obtain a copy of the License at
106              
107             http://www.apache.org/licenses/LICENSE-2.0
108              
109             Unless required by applicable law or agreed to in writing, software
110             distributed under the License is distributed on an "AS IS" BASIS,
111             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
112             See the License for the specific language governing permissions and
113             limitations under the License.
114              
115             =head1 REPOSITORY INFORMATION
116              
117             $Rev: $
118             $LastChangedBy: $
119             $Id: $
120              
121             =cut