inflating post objects from file
[engit/Iron-Munger.git] / lib / IronMunger / PlaggerLoader.pm
CommitLineData
372db818 1use MooseX::Declare;
2
3class IronMunger::PlaggerLoader {
4
5 use MooseX::Types::Path::Class qw(Dir);
6 use MooseX::Types::Moose qw(HashRef);
7 use Moose::Util::TypeConstraints qw(class_type);
8
9 BEGIN { class_type 'IO::All::File'; }
10
11 use IronMunger::CSVUtils qw(:all);
12
13 use aliased 'IronMunger::Post';
14
15 use IO::All;
16 use Text::CSV_XS;
17
18 has dir => (is => 'ro', isa => Dir, required => 1, coerce => 1);
19
20 method _target_files () {
dd5885d7 21 grep $_->name =~ /\.csv$/, io($self->dir)->all_files;
372db818 22 }
23
24 method _expand_post (HashRef $post_spec) {
25 Post->new($post_spec);
26 }
27
28 method _expand_posts_from_file(IO::All::File $file) {
29 return [
30 map $self->_expand_post($_),
31 @{$self->_expand_postspecs_from_file($file)},
32 ];
33 }
34
7a42977e 35 method _csv_column_order () {
36 my $x;
37 return map +($_ => $x++), qw(author title url at);
38 }
39
372db818 40 method _expand_postspecs_from_file(IO::All::File $file) {
41 my $csv = Text::CSV_XS->new;
42 my $io = $file->open;
43 my @post_specs;
7a42977e 44 my %col_order = $self->_csv_column_order;
372db818 45 while (my $post_raw = $csv->getline($io)) {
46 my %post_spec;
7a42977e 47 @post_spec{qw{url at}} = @{$post_raw}[@col_order{qw{url at}}];
48 push(@post_specs, \%post_spec);
372db818 49 }
50 return \@post_specs;
51 }
52
53 method _expand_monger (IO::All::File $file) {
54 my ($name, $nick) = name_and_nick_from_filename($file->name);
55 Monger->new(
56 (defined $name ? (name => $name) : ()),
57 (defined $nick ? (nick => $nick) : ()),
58 posts => $self->_expand_posts_from_file($file),
59 );
60 }
61
62 method mongers () {
63 map $self->_expand_monger($_), $self->_target_files;
64 }
65}
66
671;