| 1 | package WWW::MenuGrinder::Plugin::FileReloader; | ||
| 2 | our $VERSION = '0.01_01'; | ||
| 3 | |||
| 4 | |||
| 5 | # ABSTRACT: WWW::MenuGrinder plugin to reload the menu when a file changes. | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | |||
| 9 | with 'WWW::MenuGrinder::Role::BeforePreMogrify'; | ||
| 10 | with 'WWW::MenuGrinder::Role::BeforeMogrify'; | ||
| 11 | |||
| 12 | # New versions of Time::HiRes give us subsecond times on stat(). Use it if we | ||
| 13 | # can just in case we find ourselves racing against two crazy-fast updates. | ||
| 14 | BEGIN { | ||
| 15 | eval { | ||
| 16 | require Time::HiRes; | ||
| 17 | Time::HiRes->import(qw(stat)); | ||
| 18 | }; | ||
| 19 | } | ||
| 20 | |||
| 21 | has filename => ( | ||
| 22 | is => 'rw', | ||
| 23 | ); | ||
| 24 | |||
| 25 | has timestamp => ( | ||
| 26 | is => 'rw' | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub before_pre_mogrify { | ||
| 30 | my ($self) = @_; | ||
| 31 | my $time = (stat $self->filename)[9]; | ||
| 32 | $self->timestamp( $time ) if defined $time; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub before_mogrify { | ||
| 36 | my ($self) = @_; | ||
| 37 | |||
| 38 | my $time = (stat $self->filename)[9]; | ||
| 39 | |||
| 40 | # It seems odd that we're not setting $self->timestamp here but our | ||
| 41 | # before_pre_mogrify is about to get called anyway... | ||
| 42 | if (defined $time and $time > $self->timestamp) { | ||
| 43 | $self->grinder->init_menu; | ||
| 44 | } | ||
| 45 | |||
| 46 | return $self->grinder->menu; | ||
| 47 | |||
| 48 | } | ||
| 49 | |||
| 50 | sub BUILD { | ||
| 51 | my ($self) = @_; | ||
| 52 | |||
| 53 | my $filename = $self->grinder->config->{filename}; | ||
| 54 | die "config->{filename} is required" unless defined $filename; | ||
| 55 | |||
| 56 | $self->filename($filename); | ||
| 57 | } | ||
| 58 | |||
| 59 | no Moose; | ||
| 60 | 1; | ||
| 61 | |||
| 62 | |||
| 63 | __END__ | ||
| 64 | =head1 NAME | ||
| 65 | |||
| 66 | WWW::MenuGrinder::Plugin::FileReloader - WWW::MenuGrinder plugin to reload the menu when a file changes. | ||
| 67 | |||
| 68 | =head1 VERSION | ||
| 69 | |||
| 70 | version 0.01_01 | ||
| 71 | |||
| 72 | =head1 DESCRIPTION | ||
| 73 | |||
| 74 | C<WWW::MenuGrinder::Plugin::FileReloader> is a plugin for C<WWW::MenuGrinder>. | ||
| 75 | You should not use it directly, but include it in the C<plugins> section of a | ||
| 76 | C<WWW::MenuGrinder> config. | ||
| 77 | |||
| 78 | When loaded, this plugin will automatically reload the menu file whenever its | ||
| 79 | modification time changes. | ||
| 80 | |||
| 81 | =head2 Configuration | ||
| 82 | |||
| 83 | C<FileReloader> reads the key C<filename> in the global configuration. | ||
| 84 | |||
| 85 | =head2 Other Considerations | ||
| 86 | |||
| 87 | C<FileReloader> should be loaded before all other plugins, except for the | ||
| 88 | C<Loader> plugin, to avoid surprises. | ||
| 89 | |||
| 90 | =head1 AUTHOR | ||
| 91 | |||
| 92 | Andrew Rodland <andrew@hbslabs.com> | ||
| 93 | |||
| 94 | =head1 COPYRIGHT AND LICENSE | ||
| 95 | |||
| 96 | This software is copyright (c) 2009 by HBS Labs, LLC.. | ||
| 97 | |||
| 98 | This is free software; you can redistribute it and/or modify it under | ||
| 99 | the same terms as perl itself. | ||
| 100 |