Skip to content
Browse files

Add clone() method to the Net::Whois::Object

  • Loading branch information...
1 parent 750d67d commit 52d1694244ced3cd12fc1815645136b11956925b @arhuman committed Nov 22, 2012
Showing with 49 additions and 1 deletion.
  1. +41 −0 lib/Net/Whois/Object.pm
  2. +7 −0 t/03-objects.t
  3. +1 −1 t/common.pl
View
41 lib/Net/Whois/Object.pm
@@ -261,6 +261,47 @@ sub new {
return grep {defined} @results;
}
+=head2 B<clone( [\%options] )>
+
+Return a clone from a Net::Whois::RIPE object
+
+Current allowed option is remove => [attribute1, ..., attributen] where the specified
+attribute AREN'T copied to the clone object (for example to ignore the 'changed' values)
+
+=cut
+
+sub clone {
+ my ( $self, $rh_options ) = @_;
+
+ my $clone;
+ my %filtered;
+
+ for my $option (keys %$rh_options) {
+ if ( $option =~ /remove/i ) {
+ for my $att ( @{ $rh_options->{$option} } ) {
+ $filtered{ lc $att } = 1;
+ }
+ } else {
+ croak "Unknown option $option used while cloning a ", ref $self;
+ }
+ }
+
+ my @lines;
+ my @tofilter = split /\n/, $self->dump;
+ for my $line (@tofilter) {
+ if ( $line =~ /^(.+?):/ and $filtered{ lc $1 } ) {
+ next;
+ }
+ push @lines, $line;
+
+ }
+
+ eval { ($clone) = Net::Whois::Object->new( @lines, $/ ); };
+ croak $@ if $@;
+
+ return $clone;
+}
+
=head2 B<attributes( [$type [, \@attributes]] )>
Accessor to the attributes of the object.
View
7 t/03-objects.t
@@ -42,6 +42,13 @@ is_deeply( [ $o[0]->attributes() ], [ 'comment', 'opt1', 'opt2', 'opt3' ] )
is( $o[2]->dump, "% Information related to 'AS30720 - AS30895'\n" );
is( $o[2]->dump( { align => 8 } ), "% Information related to 'AS30720 - AS30895'\n" );
+my $clone = $o[3]->clone;
+isa_ok($clone, ref $o[3], "Clone object has the same type of source");
+is_deeply($clone, $o[3], "Clone object deeply similar to source");
+
+$clone = $o[3]->clone({remove => ['source','remarks','org', 'admin-c', 'tech-c', 'mnt-by','mnt-lower']});
+is_deeply($clone, { class => 'AsBlock', order => ['as_block', 'descr'], as_block => 'AS30720 - AS30895', descr => ['RIPE NCC ASN block'] }, "Clone object similar with removed attribute");
+
my @objects;
eval { @objects = Net::Whois::Object->query('AS30781', {attribute => 'remarks'}) };
View
2 t/common.pl
@@ -6,7 +6,7 @@
qw( new ),
# OO Support
- qw( attributes attribute_is filtered_attributes displayed_attributes dump
+ qw( attributes attribute_is filtered_attributes clone displayed_attributes dump
syncupdates_update syncupdates_delete syncupdates_create _object_factory
_single_attribute_setget _multiple_attribute_setget _syncupdates_submit );

0 comments on commit 52d1694

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