[Jifty-commit] r2054 - in jifty/trunk/lib/Jifty: Action

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sun Oct 22 20:36:38 EDT 2006


Author: audreyt
Date: Sun Oct 22 20:36:37 2006
New Revision: 2054

Modified:
   jifty/trunk/lib/Jifty/Action/Record.pm
   jifty/trunk/lib/Jifty/Param/Schema.pm

Log:
* "DWIM" Hash::Merge algorithm for params:
    - Empty fields in subclasses don't override nonempty fields in superclass anymore.
    - Arrays don't merge; e.g. if parent class's valid_values is [1,2,3,4], and
      subclass's valid_values() is [1,2], they don't somehow become [1,2,3,4,1,2].


Modified: jifty/trunk/lib/Jifty/Action/Record.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Action/Record.pm	(original)
+++ jifty/trunk/lib/Jifty/Action/Record.pm	Sun Oct 22 20:36:37 2006
@@ -282,8 +282,6 @@
         }
 
     if ($self->can('PARAMS')) {
-        use Hash::Merge qw( merge );
-
         # User-defined declarative schema fields can override default ones here
         my $params = $self->PARAMS;
 
@@ -298,7 +296,8 @@
             }
         }
 
-        $self->_cached_arguments(merge($field_info, $params));
+        use Jifty::Param::Schema ();
+        $self->_cached_arguments(Jifty::Param::Schema::merge_params($field_info, $params));
     }
     else {
         $self->_cached_arguments($field_info);

Modified: jifty/trunk/lib/Jifty/Param/Schema.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Param/Schema.pm	(original)
+++ jifty/trunk/lib/Jifty/Param/Schema.pm	Sun Oct 22 20:36:37 2006
@@ -120,8 +120,7 @@
     }
 
     if (my $super_params = $from->can('SUPER::PARAMS')) {
-        use Hash::Merge qw( merge );
-        $from->PARAMS(merge( $super_params->(), { @params } ));
+        $from->PARAMS(merge_params( $super_params->(), { @params } ));
     }
     else {
         $from->PARAMS({ @params });
@@ -132,4 +131,30 @@
     return;
 }
 
+use Hash::Merge ();
+
+no warnings 'uninitialized';
+use constant MERGE_PARAM_BEHAVIOUR => {
+    SCALAR => {
+            SCALAR => sub { length($_[1]) ? $_[1] : $_[0] },
+            ARRAY  => sub { [ @{$_[1]} ] },
+            HASH   => sub { $_[1] } },
+    ARRAY => {
+            SCALAR => sub { length($_[1]) ? $_[1] : $_[0] },
+            ARRAY  => sub { [ @{$_[1]} ] },
+            HASH   => sub { $_[1] } },
+    HASH => {
+            SCALAR => sub { length($_[1]) ? $_[1] : $_[0] },
+            ARRAY  => sub { [ @{$_[1]} ] },
+            HASH   => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) } }
+};
+
+sub merge_params {
+    my $prev_behaviour = Hash::Merge::get_behavior();
+    Hash::Merge::specify_behavior( MERGE_PARAM_BEHAVIOUR, "merge_params" );
+    my $rv = Hash::Merge::merge(@_);
+    Hash::Merge::set_behavior( $prev_behaviour );
+    return $rv;
+}
+
 1;


More information about the Jifty-commit mailing list