OSDN Git Service

add Epoch Plugin
authorhylom <hylom@users.sourceforge.jp>
Fri, 19 Jan 2018 14:04:14 +0000 (23:04 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 19 Jan 2018 14:04:14 +0000 (23:04 +0900)
src/newslash_web/lib/Newslash/Plugin/Epoch.pm [new file with mode: 0644]
src/newslash_web/lib/Newslash/Web.pm

diff --git a/src/newslash_web/lib/Newslash/Plugin/Epoch.pm b/src/newslash_web/lib/Newslash/Plugin/Epoch.pm
new file mode 100644 (file)
index 0000000..bffa54e
--- /dev/null
@@ -0,0 +1,69 @@
+package Newslash::Plugin::Epoch;
+use Mojo::Base 'Mojolicious::Plugin';
+use Time::HiRes qw(time);
+
+sub register {
+    my ($self, $app, $conf) = @_;
+
+    $self->update;
+    $app->helper(epoch => sub { state $epoch = $self; });
+
+    $app->renderer_helper->add_function("Newslash", epoch => sub { return $self->get });
+}
+
+sub get {
+    return shift->{epoch};
+}
+
+sub update {
+    my $raw_epoch = time;
+    $raw_epoch =~ s/\.//;
+    return shift->{epoch} = $raw_epoch;
+}
+
+1;
+
+=encoding utf8
+
+=head1 NAME
+
+Newslash::Plugin::Epoch - Epoch counter plugin
+
+=head1 SYNOPSIS
+
+  # Mojolicious
+  $app->plugin('Newslash::Plugin::Epoch');
+
+=head1 DESCRIPTION
+
+L<Newslash::Plugin::Epoch> porvides global Epoch counter.
+
+Epoch is similar to version or revision, but updatable while Newslash
+is under operating.
+
+
+=head1 METHODS
+
+=head2 register
+
+  $plugin->register(Mojolicious->new);
+
+Register epoch in L<Mojolicious> application.
+
+=head2 get
+
+  $app->epoch->get;
+
+Return epoch value.
+
+=head2 update
+
+  $app->epoch->update;
+
+Update epoch value.
+
+=head1 SEE ALSO
+
+L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
+
+=cut
index 5afee88..560e7de 100644 (file)
@@ -86,6 +86,9 @@ sub startup {
     # stash for plugins
     #$app->config->{_Plugins} = {};
 
+    # use Epoch
+    $app->plugin('Newslash::Plugin::Epoch');
+
     # use BasicAuth?
     if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
         $app->plugin('Newslash::Plugin::BasicAuth');