first swipe at this
[gitmo/moose-website.git] / lib / Moose / Website.pm
1 package Moose::Website;
2 use Moose;
3 use MooseX::Types::Path::Class;
4
5 use Template;
6 use YAML::XS 'LoadFile';
7 use Moose::Website::I18N;
8
9 our $VERSION   = '0.01';
10 our $AUTHORITY = 'cpan:STEVAN';
11
12 with 'MooseX::Getopt';
13
14 has 'page_file' => (
15     is       => 'ro',
16     isa      => 'Path::Class::File',
17     coerce   => 1,
18     required => 1,
19 );
20
21 has 'outdir' => (
22     is       => 'ro',
23     isa      => 'Path::Class::Dir',
24     coerce   => 1,
25     required => 1,
26 );
27
28 has 'template_root' => (
29     is       => 'ro',
30     isa      => 'Path::Class::Dir',
31     coerce   => 1,
32     required => 1,
33 );
34
35 has 'template_config' => (
36     is      => 'ro',
37     isa     => 'HashRef',
38     lazy    => 1,
39     default => sub { +{} },
40 );
41
42 has 'locale' => (
43     is      => 'ro',
44     isa     => 'Str',
45     default => sub { 'en' },
46 );
47
48 # ....
49
50 has 'i18n' => (
51     is      => 'ro',
52     isa     => 'Object',
53     lazy    => 1,
54     default => sub {
55         my $self = shift;
56         Moose::Website::I18N->get_handle( $self->locale )
57     }
58 );
59
60 has 'pages' => (
61     traits  => [ 'NoGetopt' ],
62     is      => 'ro',
63     isa     => 'ArrayRef[HashRef]',
64     lazy    => 1,
65     default => sub {
66         my $self = shift;
67         LoadFile( $self->page_file->stringify );
68     }
69 );
70
71 has 'tt' => (
72     traits  => [ 'NoGetopt' ],
73     is      => 'ro',
74     isa     => 'Template',
75     lazy    => 1,
76     default => sub {
77         my $self = shift;
78         Template->new(
79             INCLUDE_PATH => $self->template_root,
80             %{ $self->template_config }
81         )
82     }
83 );
84
85 sub log { shift; warn @_, "\n" }
86
87 sub run {
88     my $self = shift;
89
90     foreach my $page ( @{ $self->pages } ) {
91         $self->tt->process(
92             $page->{template},
93             $self->build_template_params( current_page => $page ),
94             $self->outdir->file( $page->{outfile} )->stringify
95         ) || confess $self->tt->error;
96     }
97 }
98
99 sub build_template_params {
100     my ($self, %params) = @_;
101
102     $params{ pages } = $self->pages;
103     $params{ loc }   = sub { $self->i18n->loc( @_ ) };
104
105     \%params;
106 }
107
108
109 __PACKAGE__->meta->make_immutable;
110
111 no Moose; 1;
112
113 __END__
114
115 =pod
116
117 =head1 NAME
118
119 Moose::Website - A Moosey solution to this problem
120
121 =head1 SYNOPSIS
122
123   use Moose::Website;
124
125 =head1 DESCRIPTION
126
127 =head1 METHODS
128
129 =over 4
130
131 =item B<>
132
133 =back
134
135 =head1 BUGS
136
137 All complex software has bugs lurking in it, and this module is no
138 exception. If you find a bug please either email me, or add the bug
139 to cpan-RT.
140
141 =head1 AUTHOR
142
143 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
144
145 =head1 COPYRIGHT AND LICENSE
146
147 Copyright 2010 Infinity Interactive, Inc.
148
149 L<http://www.iinteractive.com>
150
151 This library is free software; you can redistribute it and/or modify
152 it under the same terms as Perl itself.
153
154 =cut