| 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 | 2 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 2 | our $VERSION = '1.002'; # VERSION | |||||||||
| 24 | 2 | ▶ | use English '-no_match_vars'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | ▶ | use Data::Compare 0.06; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 2 | ▶ | use List::Util 'first'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 2 | ▶ | sub _register { | ||||||||
|
Subroutine
_register: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 2 | return [ 'Set::Object', \&_so_set_compare ]; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | ## no critic (Subroutines::RequireArgUnpacking) | ||||||||||
| 33 | 1 | ▶ | sub _so_set_compare { | ||||||||
|
Subroutine
_so_set_compare: called CC=5 SCAR=16.4
| |||||||||||
| 34 | 1 | my @sets = splice @_, 0, 2; | |||||||||
| 35 | |||||||||||
| 36 | # quick optimizations if sets aren't of equal size or are directly equal | ||||||||||
| 37 | 1 | ▶ | return 0 if $sets[0]->size() != $sets[1]->size(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 1 | ▶ | return 1 if $sets[0] == $sets[1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | # loop over each of the first set's elements | ||||||||||
| 41 | # looking for a match in the second set | ||||||||||
| 42 | 1 | for my $element ( $sets[0]->elements() ) { | |||||||||
| 43 | 5 | ▶ | my $matched_element = first { Data::Compare::Compare( $element, $_ ) } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 3 | grep { ref eq ref $element } $sets[1]->elements(); | |||||||||
| 45 | |||||||||||
| 46 | # return false if not found | ||||||||||
| 47 | 3 | ▶ | return 0 if not defined $matched_element; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | |||||||||||
| 49 | # otherwise remove from copy of second set and keep going | ||||||||||
| 50 | 3 | $sets[1]->remove($matched_element); | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | # sets are equal only if we've exhausted the second set | ||||||||||
| 54 | 1 | 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 | 2 | _register(); | |||||||||
| 60 | |||||||||||
| 61 | __END__ | ||||||||||