added AUTOLOAD and pass it down to readers
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 6 Aug 2010 15:21:20 +0000 (17:21 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 6 Aug 2010 15:21:20 +0000 (17:21 +0200)
lib/RFID/Biblio/Readers.pm
t/50-Readers.t [new file with mode: 0755]

index 1573134..f813845 100644 (file)
@@ -6,11 +6,21 @@ RFID::Biblio::Readers - autodetect supported readers
 
 =head1 FUNCTIONS
 
 
 =head1 FUNCTIONS
 
-=head2 available
+=head2 _available
 
 Probe each RFID reader supported and returns succefull ones
 
 
 Probe each RFID reader supported and returns succefull ones
 
-  my @rfid = RFID::Biblio::Readers->available( $regex_filter );
+  my $rfid_readers = RFID::Biblio::Readers->_available( $regex_filter );
+
+=head1 SEE ALSO
+
+=head2 RFID reader implementations
+
+L<RFID::Biblio::3M810>
+
+L<RFID::Biblio::CPRM02>
+
+L<RFID::Biblio::librfid>
 
 =head1 SEE ALSO
 
 
 =head1 SEE ALSO
 
@@ -31,9 +41,13 @@ use lib 'lib';
 
 my @readers = ( '3M810', 'CPRM02', 'librfid' );
 
 
 my @readers = ( '3M810', 'CPRM02', 'librfid' );
 
-sub available {
+sub _available {
        my ( $self, $filter ) = @_;
 
        my ( $self, $filter ) = @_;
 
+       $filter = 'all' unless defined $filter;
+
+       return $self->{_available}->{$filter} if defined $self->{_available}->{$filter};
+
        my @rfid;
 
        foreach my $reader ( @readers ) {
        my @rfid;
 
        foreach my $reader ( @readers ) {
@@ -51,6 +65,28 @@ sub available {
 
        die "no readers found" unless @rfid;
 
 
        die "no readers found" unless @rfid;
 
-       return @rfid;
+       $self->{_available}->{$filter} = [ @rfid ];
+}
+
+sub new {
+       my $class = shift;
+       my $self = {};
+       bless $self, $class;
+       return $self;
+}
+
+# we don't want DESTROY to fallback into AUTOLOAD
+sub DESTROY {}
+
+our $AUTOLOAD;
+sub AUTOLOAD {
+       my $self = shift;
+       my $command = $AUTOLOAD;
+       $command =~ s/.*://;
+
+       foreach my $r ( @{ $self->_available } ) {
+               $r->$command(@_);
+       }
 }
 
 }
 
+1
diff --git a/t/50-Readers.t b/t/50-Readers.t
new file mode 100755 (executable)
index 0000000..38a5971
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+use Test::More; # tests => 3;
+use Data::Dump qw(dump);
+
+use lib 'lib';
+
+BEGIN {
+       use_ok( 'RFID::Biblio::Readers' );
+}
+
+ok( my $o = RFID::Biblio::Readers->new, 'new' );
+
+ok( my @tags = $o->inventory, 'inventory' );
+diag dump @tags;
+
+my $old_afi;
+
+foreach my $tag ( @tags ) {
+
+       ok( my $blocks = $o->read_blocks( $tag ), "read_blocks $tag" );
+
+       ok( my $afi = $o->read_afi( $tag ), "read_afi $tag" );
+
+       ok( $o->write_blocks( $tag, $blocks->{$tag} ), "write_blocks $tag" );
+
+       my $new_afi = "\x42";
+
+       ok( $o->write_afi( $tag, $new_afi ), sprintf( "write_afi %s %x", $tag, $new_afi ) );
+
+       cmp_ok( $o->read_afi( $tag ), 'eq', $new_afi, 'AFI check' );
+
+       ok( $o->write_afi( $tag, $afi ), sprintf( "write_afi %s %x", $tag, $afi ) );
+
+}
+
+ok( my $visible = $o->scan, 'scan' );
+diag dump $visible;
+
+done_testing();