File Coverage

blib/lib/Data/Compare/Plugins/Set/Object.pm
Criterion Covered Total %
statement 31 31 100.0
branch 3 6 50.0
condition n/a
subroutine 10 10 100.0
pod n/a
total 44 47 93.6


line stmt bran cond sub pod time code
1             package Data::Compare::Plugins::Set::Object;
2              
3             # ABSTRACT: plugin for Data::Compare to handle Set::Object objects
4              
5             #pod =head1 DESCRIPTION
6             #pod
7             #pod Enables L<Data::Compare|Data::Compare> to Do The Right Thing for
8             #pod L<Set::Object|Set::Object> objects. Set::Object already has an
9             #pod C<equals()> method, but it only returns true if objects within two sets
10             #pod are exactly equal (i.e. have the same references, referring to the same
11             #pod object instance). When using Data::Compare in conjunction with this
12             #pod plugin, objects in sets are considered the same if their B<contents> are
13             #pod the same. This extends down to sets that contain arrays, hashes, or
14             #pod other objects supported by Data::Compare plugins.
15             #pod
16             #pod =cut
17              
18 1     1   98077 use 5.010;
  1         11  
19 1     1   657 use utf8;
  1         15  
  1         6  
20 1     1   33 use strict;
  1         2  
  1         21  
21 1     1   5 use warnings;
  1         2  
  1         64  
22              
23             our $VERSION = '1.002'; # VERSION
24 1     1   475 use English '-no_match_vars';
  1         3948  
  1         6  
25 1     1   401 use Data::Compare 0.06;
  1         20  
  1         9  
26 1     1   6090 use List::Util 'first';
  1         3  
  1         341  
27              
28             sub _register {
29 1     1   14 return [ 'Set::Object', \&_so_set_compare ];
30             }
31              
32             ## no critic (Subroutines::RequireArgUnpacking)
33             sub _so_set_compare {
34 1     1   1496 my @sets = splice @_, 0, 2;
35              
36             # quick optimizations if sets aren't of equal size or are directly equal
37 1 50       9 return 0 if $sets[0]->size() != $sets[1]->size();
38 1 50       5 return 1 if $sets[0] == $sets[1];
39              
40             # loop over each of the first set's elements
41             # looking for a match in the second set
42 1         19 for my $element ( $sets[0]->elements() ) {
43 5     5   218 my $matched_element = first { Data::Compare::Compare( $element, $_ ) }
44 3         22 grep { ref eq ref $element } $sets[1]->elements();
  6         34  
45              
46             # return false if not found
47 3 50       259 return 0 if not defined $matched_element;
48              
49             # otherwise remove from copy of second set and keep going
50 3         12 $sets[1]->remove($matched_element);
51             }
52              
53             # sets are equal only if we've exhausted the second set
54 1         6 return $sets[1]->is_null();
55             }
56              
57             # Data::Compare::Plugins interface requires modules to return an arrayref
58             ## no critic (RequireEndWithOne, Lax::RequireEndWithTrueConst)
59             _register();
60              
61             __END__
62              
63             =pod
64              
65             =encoding utf8
66              
67             =for :stopwords Mark Gardner cpan testmatrix url bugtracker rt cpants kwalitee diff irc
68             mailto metadata placeholders metacpan
69              
70             =head1 NAME
71              
72             Data::Compare::Plugins::Set::Object - plugin for Data::Compare to handle Set::Object objects
73              
74             =head1 VERSION
75              
76             version 1.002
77              
78             =head1 SYNOPSIS
79              
80             use 5.010;
81             use Set::Object 'set';
82             use Data::Compare;
83              
84             my $foo = {
85             list => [ qw(one two three) ],
86             set => set( [1], [2], [3] ),
87             };
88             my $bar = {
89             list => [ qw(one two three) ],
90             set => set( [1], [2], [3] ),
91             };
92              
93             say 'Sets in $foo and $bar are ',
94             $foo->{set} == $bar->{set} ? '' : 'NOT ', 'identical.';
95             say 'Data within $foo and $bar are ',
96             Compare($foo, $bar) ? '' : 'NOT ', 'equal.';
97              
98             =head1 DESCRIPTION
99              
100             Enables L<Data::Compare|Data::Compare> to Do The Right Thing for
101             L<Set::Object|Set::Object> objects. Set::Object already has an
102             C<equals()> method, but it only returns true if objects within two sets
103             are exactly equal (i.e. have the same references, referring to the same
104             object instance). When using Data::Compare in conjunction with this
105             plugin, objects in sets are considered the same if their B<contents> are
106             the same. This extends down to sets that contain arrays, hashes, or
107             other objects supported by Data::Compare plugins.
108              
109             =head1 SUBROUTINES/METHODS
110              
111             As a plugin to Data::Compare, the interface is the same as Data::Compare
112             itself: pass the reference to two data structures to the C<Compare>
113             function, which for historical reasons is exported by default.
114              
115             Set::Object also can export certain functions, and overloads comparison
116             operators pertaining to sets. Consult the
117             L<Set::Object documentation|Set::Object> for more information.
118              
119             =head1 SUPPORT
120              
121             =head2 Perldoc
122              
123             You can find documentation for this module with the perldoc command.
124              
125             perldoc Data::Compare::Plugins::Set::Object
126              
127             =head2 Websites
128              
129             The following websites have more information about this module, and may be of help to you. As always,
130             in addition to those websites please use your favorite search engine to discover more resources.
131              
132             =over 4
133              
134             =item *
135              
136             CPANTS
137              
138             The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
139              
140             L<http://cpants.cpanauthors.org/dist/Data-Compare-Plugins-Set-Object>
141              
142             =item *
143              
144             CPAN Testers
145              
146             The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
147              
148             L<http://www.cpantesters.org/distro/D/Data-Compare-Plugins-Set-Object>
149              
150             =item *
151              
152             CPAN Testers Matrix
153              
154             The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
155              
156             L<http://matrix.cpantesters.org/?dist=Data-Compare-Plugins-Set-Object>
157              
158             =item *
159              
160             CPAN Testers Dependencies
161              
162             The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
163              
164             L<http://deps.cpantesters.org/?module=Data::Compare::Plugins::Set::Object>
165              
166             =back
167              
168             =head2 Bugs / Feature Requests
169              
170             Please report any bugs or feature requests through the web
171             interface at L<https://github.com/mjgardner/Data-Compare-Plugins-Set-Object/issues>. You will be automatically notified of any
172             progress on the request by the system.
173              
174             =head2 Source Code
175              
176             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
177             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
178             from your repository :)
179              
180             L<https://github.com/mjgardner/Data-Compare-Plugins-Set-Object>
181              
182             git clone git://github.com/mjgardner/Data-Compare-Plugins-Set-Object.git
183              
184             =head1 AUTHOR
185              
186             Mark Gardner <mjgardner@cpan.org>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is copyright (c) 2017 by Mark Gardner.
191              
192             This is free software; you can redistribute it and/or modify it under
193             the same terms as the Perl 5 programming language system itself.
194              
195             =cut