load and construct
[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 () {
21 grep $_->name =~ /\.csv$/, io($self->dir)->get_files;
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
35 method _expand_postspecs_from_file(IO::All::File $file) {
36 my $csv = Text::CSV_XS->new;
37 my $io = $file->open;
38 my @post_specs;
39 while (my $post_raw = $csv->getline($io)) {
40 my %post_spec;
41 @post_spec{qw{url at}} = @{$post_raw}[1,2];
42 push(@post_specs, %post_spec);
43 }
44 return \@post_specs;
45 }
46
47 method _expand_monger (IO::All::File $file) {
48 my ($name, $nick) = name_and_nick_from_filename($file->name);
49 Monger->new(
50 (defined $name ? (name => $name) : ()),
51 (defined $nick ? (nick => $nick) : ()),
52 posts => $self->_expand_posts_from_file($file),
53 );
54 }
55
56 method mongers () {
57 map $self->_expand_monger($_), $self->_target_files;
58 }
59}
60
611;