[Jifty-commit] r1250 - jifty/trunk/lib/Jifty/Web/Form

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sat Jun 10 23:56:24 EDT 2006


Author: clkao
Date: Sat Jun 10 23:56:12 2006
New Revision: 1250

Modified:
   jifty/trunk/lib/Jifty/Web/Form/Clickable.pm
   jifty/trunk/lib/Jifty/Web/Form/Field.pm
   jifty/trunk/lib/Jifty/Web/Form/Link.pm

Log:
Misc refactoring to drop Clickable->generator from 16% to 12%.


Modified: jifty/trunk/lib/Jifty/Web/Form/Clickable.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Form/Clickable.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/Form/Clickable.pm	Sat Jun 10 23:56:12 2006
@@ -12,7 +12,7 @@
 
 =cut
 
-use base qw/Jifty::Web::Form::Element Class::Accessor::Fast/;
+use base 'Jifty::Web::Form::Element';
 
 =head2 accessors
 
@@ -423,6 +423,12 @@
     return $url;
 }
 
+sub _defined_accessor_values {
+    my $self = shift;
+    return { map { my $val = $self->$_; defined $val ? ($_ => $val) : () } 
+        $self->SUPER::accessors };
+}
+
 =head2 as_link
 
 Returns the clickable as a L<Jifty::Web::Form::Link>, if possible.
@@ -434,14 +440,12 @@
 sub as_link {
     my $self = shift;
 
-    my %args;
-    $args{$_} = $self->$_
-        for grep { defined $self->$_ } $self->SUPER::accessors;
+    my $args = $self->_defined_accessor_values;
     my $link = Jifty::Web::Form::Link->new(
-        %args,
-        escape_label => $self->escape_label,
-        url          => $self->complete_url,
-        @_
+        { %$args,
+          escape_label => $self->escape_label,
+          url          => $self->complete_url,
+          @_ }
     );
     return $link;
 }
@@ -458,13 +462,11 @@
 sub as_button {
     my $self = shift;
 
-    my %args;
-    $args{$_} = $self->$_
-        for grep { defined $self->$_ } $self->SUPER::accessors;
+    my $args = $self->_defined_accessor_values;
     my $field = Jifty::Web::Form::Field->new(
-        %args,
-        type => 'InlineButton',
-        @_
+        { %$args,
+          type => 'InlineButton',
+          @_ }
     );
     my %parameters = $self->post_parameters;
 
@@ -473,7 +475,7 @@
         map      { $_ . "=" . $parameters{$_} }
             grep { defined $parameters{$_} } keys %parameters
     );
-    $field->name( join '|', keys %{ $args{parameters} } );
+    $field->name( join '|', keys %{ $args->{parameters} } );
     $field->button_as_link($self->render_as_link);
 
     return $field;

Modified: jifty/trunk/lib/Jifty/Web/Form/Field.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Form/Field.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/Form/Field.pm	Sat Jun 10 23:56:12 2006
@@ -41,7 +41,7 @@
 
 =cut
 
-use base qw/Jifty::Web::Form::Element Class::Accessor::Fast/;
+use base 'Jifty::Web::Form::Element';
 
 use Scalar::Util;
 use HTML::Entities;
@@ -56,23 +56,20 @@
 
 sub new {
     my $class = shift;
-    my $self = bless {}, $class;
-
-    my %args = (
-        type          => 'text',
+    my $self = $class->SUPER::new(
+      { type          => 'text',
         class         => '',
         input_name    => '',
         default_value => '',
         sticky_value  => '',
-        render_mode   => 'update',
-        @_,
-    );
+        render_mode   => 'update' });
+    my $args = ref($_[0]) ? $_[0] : {@_};
 
     my $subclass;
-    if ($args{render_as}) {
-        $subclass = ucfirst($args{render_as});
-    } elsif ($args{'type'}) {
-        $subclass = ucfirst($args{'type'});
+    if ($args->{render_as}) {
+        $subclass = ucfirst($args->{render_as});
+    } elsif ($args->{'type'}) {
+        $subclass = ucfirst($args->{'type'});
     }
     if ($subclass) { 
         $subclass = 'Jifty::Web::Form::Field::' . $subclass unless $subclass =~ /::/;
@@ -80,7 +77,7 @@
     }
 
     for my $field ( $self->accessors() ) {
-        $self->$field( $args{$field} ) if exists $args{$field};
+        $self->$field( $args->{$field} ) if exists $args->{$field};
     }
 
     # If they key and/or value imply that this argument is going to be
@@ -173,12 +170,12 @@
 # Otherwise, we should ask our action, how to turn our "name"
 # into a form input name.
 
-    $self->_input_name(@_)
-      || (
-          $self->action
-        ? $self->action->form_field_name( $self->name )
-        : ''
-      );
+    my $ret = $self->_input_name(@_);
+    return $ret if $ret;
+
+    my $action = $self->action;
+    return $action ? $self->action->form_field_name( $self->name )
+                   : '';
 }
 
 

Modified: jifty/trunk/lib/Jifty/Web/Form/Link.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Form/Link.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/Form/Link.pm	Sat Jun 10 23:56:12 2006
@@ -15,7 +15,7 @@
 
 =cut
 
-use base qw/Jifty::Web::Form::Element Class::Accessor::Fast/;
+use base 'Jifty::Web::Form::Element';
 
 # Since we don't inherit from Form::Field, we don't otherwise stringify
 use overload '""' => sub { shift->render};
@@ -65,20 +65,18 @@
 
 sub new {
     my $class = shift;
-    my $self  = bless {}, $class;
-
-    my %args = (
-        url          => $ENV{PATH_INFO},
+    my $self  = $class->SUPER::new(
+      { url          => $ENV{PATH_INFO},
         label        => "Click me!",
         tooltip      => undef,
         escape_label => 1,
         class        => '',
-        target       => '',
-        @_
+        target       => '' }
     );
+    my $args = ref($_[0]) ? $_[0] : {@_};
 
-    for my $field ( $self->accessors() ) {
-        $self->$field( $args{$field} ) if exists $args{$field};
+    for my $field ( keys %$args ) {
+        $self->$field( $args->{$field} );
     }
 
     return $self;


More information about the Jifty-commit mailing list