From: Matt S Trout Date: Fri, 15 May 2009 18:12:36 +0000 (+0100) Subject: first sketch of load/save processing functionality X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=372db818621d2fe04a13d46e974c2c0221e4a9c0;p=engit%2FIron-Munger.git first sketch of load/save processing functionality --- diff --git a/Makefile.PL b/Makefile.PL index 2652e15..2b091b8 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -9,6 +9,7 @@ requires 'MooseX::Declare'; requires 'signatures'; requires 'autobox::DateTime::Duration'; requires 'MooseX::Types::DateTimeX'; +requires 'Text::CSV_XS'; test_requires 'Test::More'; test_requires 'Test::Deep'; diff --git a/lib/IronMunger.pm b/lib/IronMunger.pm new file mode 100644 index 0000000..f5d4977 --- /dev/null +++ b/lib/IronMunger.pm @@ -0,0 +1,31 @@ +use MooseX::Declare; + +class IronMunger { + + our $VERSION = '0.001000'; + + use aliased 'IronMunger::Monger'; + use aliased 'IronMunger::PlaggerLoader'; + use aliased 'IronMunger::StatsSaver'; + + use MooseX::Types::Moose qw(HashRef ClassName Str); + + has mongers => ( + is => 'ro', isa => HashRef[Monger], required => 1 + default => sub { {} }, + ); + + method load_from_plagger (ClassName $class: Str $dir) { + my $loader = PlaggerLoader->new(dir => $dir); + my $munger = $class->new; + $munger->mongers->{$_->full_name} + = $_ for @{$loader->mongers}; + } + + method save_monger_stats (Str $dir) { + my $saver = StatsSaver->new(dir => $dir); + $saver->mongers([ sort $_->full_name, values %{$self->mongers} ]); + } +} + +1; diff --git a/lib/IronMunger/PlaggerLoader.pm b/lib/IronMunger/PlaggerLoader.pm new file mode 100644 index 0000000..57174c3 --- /dev/null +++ b/lib/IronMunger/PlaggerLoader.pm @@ -0,0 +1,61 @@ +use MooseX::Declare; + +class IronMunger::PlaggerLoader { + + use MooseX::Types::Path::Class qw(Dir); + use MooseX::Types::Moose qw(HashRef); + use Moose::Util::TypeConstraints qw(class_type); + + BEGIN { class_type 'IO::All::File'; } + + use IronMunger::CSVUtils qw(:all); + + use aliased 'IronMunger::Post'; + + use IO::All; + use Text::CSV_XS; + + has dir => (is => 'ro', isa => Dir, required => 1, coerce => 1); + + method _target_files () { + grep $_->name =~ /\.csv$/, io($self->dir)->get_files; + } + + method _expand_post (HashRef $post_spec) { + Post->new($post_spec); + } + + method _expand_posts_from_file(IO::All::File $file) { + return [ + map $self->_expand_post($_), + @{$self->_expand_postspecs_from_file($file)}, + ]; + } + + method _expand_postspecs_from_file(IO::All::File $file) { + my $csv = Text::CSV_XS->new; + my $io = $file->open; + my @post_specs; + while (my $post_raw = $csv->getline($io)) { + my %post_spec; + @post_spec{qw{url at}} = @{$post_raw}[1,2]; + push(@post_specs, %post_spec); + } + return \@post_specs; + } + + method _expand_monger (IO::All::File $file) { + my ($name, $nick) = name_and_nick_from_filename($file->name); + Monger->new( + (defined $name ? (name => $name) : ()), + (defined $nick ? (nick => $nick) : ()), + posts => $self->_expand_posts_from_file($file), + ); + } + + method mongers () { + map $self->_expand_monger($_), $self->_target_files; + } +} + +1;