[Bast-commits] r8085 - in ironman/plagger: lib/IronMan
lib/IronMan/Schema/Result lib/IronMan/Schema/ResultSet t
castaway at dev.catalyst.perl.org
castaway at dev.catalyst.perl.org
Sat Dec 12 20:08:50 GMT 2009
Author: castaway
Date: 2009-12-12 20:08:49 +0000 (Sat, 12 Dec 2009)
New Revision: 8085
Modified:
ironman/plagger/lib/IronMan/Schema.pm
ironman/plagger/lib/IronMan/Schema/Result/Feed.pm
ironman/plagger/lib/IronMan/Schema/ResultSet/Feed.pm
ironman/plagger/t/feed_rs.t
Log:
added filter_unseen method to pass test
Modified: ironman/plagger/lib/IronMan/Schema/Result/Feed.pm
===================================================================
--- ironman/plagger/lib/IronMan/Schema/Result/Feed.pm 2009-12-12 18:38:26 UTC (rev 8084)
+++ ironman/plagger/lib/IronMan/Schema/Result/Feed.pm 2009-12-12 20:08:49 UTC (rev 8085)
@@ -1,10 +1,8 @@
package IronMan::Schema::Result::Feed;
use strict;
use warnings;
-use base qw/DBIx::Class/;
+use base qw/DBIx::Class::Core/;
-__PACKAGE__->load_components(qw/Core/);
-
__PACKAGE__->table('feed');
__PACKAGE__->add_columns(
id => {
@@ -34,4 +32,6 @@
__PACKAGE__->set_primary_key(qw/id/);
__PACKAGE__->add_unique_constraint(url => ['url']);
+__PACKAGE__->has_many('posts', 'IronMan::Schema::Result::Post', 'feed_id');
+
1;
Modified: ironman/plagger/lib/IronMan/Schema/ResultSet/Feed.pm
===================================================================
--- ironman/plagger/lib/IronMan/Schema/ResultSet/Feed.pm 2009-12-12 18:38:26 UTC (rev 8084)
+++ ironman/plagger/lib/IronMan/Schema/ResultSet/Feed.pm 2009-12-12 20:08:49 UTC (rev 8085)
@@ -6,6 +6,7 @@
use base 'DBIx::Class::ResultSet';
use Data::UUID;
+use Scalar::Util 'blessed';
## Need to check if url is duplicated by normalising it as well as just uniquing what is passed in!
sub add_new_blog {
@@ -31,4 +32,30 @@
return (1, $fdb);
}
+## Given a feed url that should exist as a unique feed in the db,
+## and an arrayref of entry hashes/objects, return ones we dont
+## have yet.
+sub filter_unseen {
+ my ($self, $feed_url, $entries) = @_;
+
+ die "filter_unseen not passed a feed_url" if(!$feed_url);
+
+ return [] if(!$entries || ref($entries) ne 'ARRAY' || !@$entries);
+
+ my $feed = $self->find({ url => $feed_url}, { key => 'url' });
+ die "No such feed: $feed_url" if(!$feed);
+
+ my %entry_urls = map {
+ blessed($_) ?
+ ( $_->can('url') && $_->url )
+ ||
+ ( $_->can('link') && $_->link )
+ : $_->{url} => $_ } @$entries;
+
+ my @got_these = $feed->posts->search({ url => { -in => [ keys %entry_urls ] } });
+ delete $entry_urls{$_->url} for @got_these;
+
+ return [ values(%entry_urls) ];
+}
+
1;
Modified: ironman/plagger/lib/IronMan/Schema.pm
===================================================================
--- ironman/plagger/lib/IronMan/Schema.pm 2009-12-12 18:38:26 UTC (rev 8084)
+++ ironman/plagger/lib/IronMan/Schema.pm 2009-12-12 20:08:49 UTC (rev 8085)
@@ -3,10 +3,11 @@
use strict;
use warnings;
-our $VERSION = '0.01';
+our $VERSION = '0.02';
use base 'DBIx::Class::Schema';
+__PACKAGE__->load_components('Schema::Versioned');
__PACKAGE__->load_namespaces();
1;
Modified: ironman/plagger/t/feed_rs.t
===================================================================
--- ironman/plagger/t/feed_rs.t 2009-12-12 18:38:26 UTC (rev 8084)
+++ ironman/plagger/t/feed_rs.t 2009-12-12 20:08:49 UTC (rev 8085)
@@ -4,6 +4,8 @@
use warnings;
use DateTime;
+use XML::Feed;
+use XML::Feed::Entry;
use Test::More 'no_plan';
use_ok('IronMan::Schema');
@@ -28,17 +30,19 @@
## given three entries, one of which is the above, return the two unknown onws
-my $input = [ 'http://my.blog.example/foo.atom',
+my $input = [ 'http://my.blog.example/foo.atom', [
{ url => 'http://my.blog.example/entry2.html' },
{ url => 'http://my.blog.example/entry1.html' },
{ url => 'http://my.blog.example/entry3.html' },
- ];
+ ] ];
-is_deeply($schema->resultset('Feed')->filter_unseen(@$input),
- [
+my $filtered = $schema->resultset('Feed')->filter_unseen(@$input);
+is_deeply([ sort { $a->{url} cmp $b->{url} } @$filtered ],
+ [ sort { $a->{url} cmp $b->{url} } (
{ url => 'http://my.blog.example/entry2.html' },
{ url => 'http://my.blog.example/entry3.html' },
- ]);
+ )],
+ 'Plain hashes return correct values');
my $feed = XML::Feed->new('Atom');
$feed->link('http://my.blog.example/foo.atom');
@@ -52,7 +56,10 @@
$e3->link('http://my.blog.example/entry3.html');
$feed->add_entry($e3);
-is_deeply($schema->resultset('Feed')->filter_unseen($feed->link,
- $feed->entries),
- [ $e1, $e3 ]);
+my $filtered2 = $schema->resultset('Feed')->filter_unseen($feed->link,
+ [ $feed->entries] );
+is_deeply( [ sort { $a cmp $b } map {$_->link } @$filtered2 ],
+ [ sort { $a cmp $b } map {$_->link } ($e1, $e3) ],
+ 'Entry objects return correct values');
+
More information about the Bast-commits
mailing list