[Jifty-commit] r5703 - in jifty/trunk: lib/Jifty lib/Jifty/Event lib/Jifty/Logger lib/Jifty/Subs

Jifty commits jifty-commit at lists.jifty.org
Tue Aug 12 14:27:05 EDT 2008


Author: alexmv
Date: Tue Aug 12 14:27:05 2008
New Revision: 5703

Added:
   jifty/trunk/lib/Jifty/Event/Log.pm
   jifty/trunk/lib/Jifty/Logger/
   jifty/trunk/lib/Jifty/Logger/EventAppender.pm
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/META.yml
   jifty/trunk/Makefile.PL
   jifty/trunk/lib/Jifty/Subs.pm
   jifty/trunk/lib/Jifty/Subs/Render.pm

Log:
 r35844 at kohr-ah:  chmrr | 2008-08-12 14:26:49 -0400
  * Support for Jifty->subs->update_on( class => 'SomeEvent' ) as a shortcut
  * Don't always assume event classes are under AppName::Event, as we
 can have generic Jifty::Events, too
  * Add an Jifty::Event::Log, and a Jifty::Logger::EventAppender, so
 you can cause arbitrary log messages to send events, based on your
 log4perl config


Modified: jifty/trunk/META.yml
==============================================================================
--- jifty/trunk/META.yml	(original)
+++ jifty/trunk/META.yml	Tue Aug 12 14:27:05 2008
@@ -150,5 +150,5 @@
   YAML::Syck: 0.71
   perl: 5.8.3
   version: 0
-tests: t/*.t t/*/*.t t/*/*/*.t t/*/*/*/*.t
+tests: t/*.t t/*/*.t t/*/*/*.t t/*/*/*/*.t plugins/TabView/t/*.t
 version: 0.80408

Modified: jifty/trunk/Makefile.PL
==============================================================================
--- jifty/trunk/Makefile.PL	(original)
+++ jifty/trunk/Makefile.PL	Tue Aug 12 14:27:05 2008
@@ -54,7 +54,7 @@
 requires('Jifty::DBI' => '0.49' );            # Jifty::DBI::Collection Jifty::DBI::Handle Jifty::DBI::Record::Cachable Jifty::DBI::SchemaGenerator
 requires('Locale::Maketext::Extract' => '0.20');
 requires('Locale::Maketext::Lexicon' => '0.60');
-requires('Log::Log4perl' => '1.04');
+requires('Log::Log4perl' => '1.04'); # Log::Log4perl::Appender
 requires('LWP::UserAgent'); # Net::HTTP
 requires('MIME::Types');
 requires('Module::Pluggable' => '3.5'); # Module::Pluggable::Object

Added: jifty/trunk/lib/Jifty/Event/Log.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Event/Log.pm	Tue Aug 12 14:27:05 2008
@@ -0,0 +1,45 @@
+package Jifty::Event::Log;
+use strict;
+use warnings;
+use base qw/Jifty::Event/;
+
+=head1 NAME
+
+Jifty::Event::Log - An event that L<Jifty::Logger::EventAppender> creates
+
+=head1 DESCRIPTION
+
+This L<Jifty::Event> is created when a log message happens.
+
+=head1 METHODS
+
+=head2 match QUERY
+
+Matches only if all of the keys in the query exist in the data, and
+the values of the keys match the respective values in the data.
+
+=cut
+
+sub match {
+    my $self    = shift;
+    my $query   = shift;
+
+    for my $key (keys %{$query}) {
+        return unless defined $self->data->{$key} and $self->data->{$key} eq $query->{$key};
+    }
+
+    return 1;
+}
+
+=head2 render_arguments
+
+All of the data is dumped into the redered arguments, verbatim.
+
+=cut
+
+sub render_arguments {
+    my $self = shift;
+    return ( %{ $self->data } );
+}
+
+1;

Added: jifty/trunk/lib/Jifty/Logger/EventAppender.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Logger/EventAppender.pm	Tue Aug 12 14:27:05 2008
@@ -0,0 +1,67 @@
+package Jifty::Logger::EventAppender;
+use strict;
+use warnings;
+use base qw/Log::Log4perl::Appender/;
+
+# We need to pull these in explicitly because this appender class
+# could be used from programs which aren't Jifty
+use Jifty::Util;
+use Jifty;
+BEGIN {Jifty->new}
+
+=head1 NAME
+
+Jifty::Logger::EventAppender - Create Jifty events from log directives
+
+=head1 SYNOPSIS
+
+In a log4perl config file:
+
+    log4perl.appender.Event=Jifty::Logger::EventAppender
+    log4perl.appender.Event.class=YourApp::Event::Log
+    log4perl.appender.Event.arbitraryData=42
+    log4perl.appender.Event.layout=SimpleLayout
+
+=head1 DESCRIPTION
+
+This class is a L<Log::Log4perl>-compatible appender which creates
+L<Jifty::Event::Log> objects when a logging instruction is received.
+
+=head1 METHODS
+
+=head2 new PARAMHASH
+
+The C<class> configuration parameter controls the class of the event
+to create.  It defaults to L<Jifty::Event::Log>.  All other parameters
+are passed through to the event when it is created.
+
+=cut
+
+sub new {
+    my $class = shift;
+    my %params = (
+        class => "Jifty::Event::Log",
+        @_,
+    );
+
+    my $event_class = delete $params{class};
+    Jifty::Util->require($event_class) or die "Can't find event class $event_class";
+
+    return bless {params => \%params, class => $event_class}, $class;
+}
+
+=head2 log PARAMHASH
+
+Creates an instance of the event with all of the configuration
+parameters set in the log4perl config file, as well as all of the
+contents of the C<PARAMHASH> -- see L<Log::Log4perl::Appender> for
+details of the arguments therein.
+
+=cut
+
+sub log {
+    my $self = shift;
+    $self->{class}->new( { %{$self->{params}}, @_ } )->publish;
+}
+
+1;

Modified: jifty/trunk/lib/Jifty/Subs.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Subs.pm	(original)
+++ jifty/trunk/lib/Jifty/Subs.pm	Tue Aug 12 14:27:05 2008
@@ -94,7 +94,8 @@
     $args->{coalesce} = 1 if not exists $args->{coalesce} and $args->{mode} eq "Replace";
 
     my $id          = ($args->{window_id} || Jifty->web->session->id);
-    my $event_class = Jifty->app_class("Event", $args->{class});
+    my $event_class = $args->{class};
+    $event_class = Jifty->app_class("Event", $args->{class}) unless $event_class =~ /^Jifty::Event::/;
 
     my $queries = $args->{queries} || [];
     my $region  = $args->{region};
@@ -200,4 +201,36 @@
     return $subscribe->channels;
 }
 
+=head2 update_on PARAMHASH
+
+Like L</add>, but provides a sane set of defaults for updating the
+current region, based on inspection of the current region's
+properties.  C<queries> is set to the region's arguments, and C<class>
+is left unspecified.
+
+=cut
+
+sub update_on {
+    my $self = shift;
+
+    my $region = Jifty->web->current_region;
+    unless ($region) {
+        warn "Jifty->subs->update_on called when not in a region";
+        return;
+    }
+
+    my %args = %{ $region->arguments };
+    delete $args{region};
+    delete $args{event};
+
+    $self->add(
+        queries     => [ \%args ],
+        arguments   => \%args,
+        mode        => 'Replace',
+        region      => $region->qualified_name,
+        render_with => $region->path,
+        @_,
+    );
+}
+
 1;

Modified: jifty/trunk/lib/Jifty/Subs/Render.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Subs/Render.pm	(original)
+++ jifty/trunk/lib/Jifty/Subs/Render.pm	Tue Aug 12 14:27:05 2008
@@ -80,6 +80,12 @@
     return ($sent);
 }
 
+=head2 render_single CLASS, MESSAGE, INFO, CALLBACK
+
+Renders a single region, based on the region information in C<INFO>.
+
+=cut
+
 sub render_single {
     my ($class, $msg, $render_info, $callback) = @_;
 
@@ -87,21 +93,21 @@
         name => $render_info->{region},
         path => $render_info->{render_with},
     );
-
     # So we don't warn about "duplicate region"s
     delete Jifty->web->{'regions'}{ $region->qualified_name };
 
-    # Finally render the region.  In addition to the user-supplied arguments
-    # in $render_info, we always pass the target $region and the event object
-    # into its %ARGS.
-    my $region_content = '';
     my $event_object   = $class->new($msg);
+    # Region's arguments come from explicit arguments only
+    $region->arguments( $render_info->{arguments} );
+
+    my $region_content = '';
     $region->enter;
-    $region->render_as_subrequest( \$region_content,
-        {   %{ $render_info->{arguments} || {} },
-            event => $event_object,
-            $event_object->render_arguments,
-        }
+    # Also provide an 'event' argument, and fill in the render
+    # arguments.  These don't show up in the region's arguments if
+    # inspected, but do show up in the request.
+    $region->render_as_subrequest(
+        \$region_content,
+        { %{$region->arguments}, event => $event_object, $event_object->render_arguments },
     );
     $callback->( $render_info->{mode}, $region->qualified_name, $region_content, $render_info->{attrs} );
     $region->exit;


More information about the Jifty-commit mailing list