broken classdata version
Guillermo Roditi [Mon, 23 Jun 2008 21:20:47 +0000 (21:20 +0000)]
r18425@martha (orig r7903):  groditi | 2008-06-09 14:46:29 -0400

lib/Catalyst/ClassData.pm

index 66e9b62..23fd110 100644 (file)
@@ -4,26 +4,51 @@ use Moose::Role;
 use Scalar::Util 'blessed';
 
 sub mk_classdata {
-  my ($declaredclass, $attribute, $data) = @_;
+  my ($class, $attribute) = @_;
   confess("mk_classdata() is a class method, not an object method")
-    if ref $declaredclass;
-
-  my $accessor = sub {
-    my $wantclass = blessed($_[0]) || $_[0];
-
-    return $wantclass->mk_classdata($attribute)->(@_)
-      if @_>1 && $wantclass ne $declaredclass;
-
-    $data = $_[1] if @_>1;
-    return $data;
+    if blessed $class;
+
+  my $slot = '$'.$attribute;
+  my $accessor =  sub {
+    if(@_ > 1){
+      $_[0]->meta->add_package_symbol($slot, \  $_[1]);
+      return $_[1];
+    }
+    foreach my $super ( $_[0], $_[0]->meta->linearized_isa ) {
+      my $meta = $super->meta;
+      if( $meta->has_package_symbol($slot) ){
+        return $meta->get_package_symbol($slot);
+      }
+    }
+    return;
   };
+  my $accessor = eval $code;
+  confess("Failed to create accessor: $@ \n $code \n")
+    unless ref $accessor eq 'CODE';
 
+  my $meta = $class->meta;
   my $alias = "_${attribute}_accessor";
-  $declaredclass->meta->add_method($alias, $accessor);
-  $declaredclass->meta->add_method($attribute, $accessor);
+  $meta->add_method($alias, $accessor);
+  $meta->add_method($attribute, $accessor);
+  $class->$attribute($_[2]) if(@_ > 2);
   return $accessor;
 }
 
 1;
 
 __END__
+
+#   my $code = ' sub {
+#     if(@_ > 1){
+#       $_[0]->meta->add_package_symbol(\''.$slot.'\', \  $_[1]);
+#       return $_[1];
+#     }
+#     foreach my $super ( $_[0], $_[0]->meta->linearized_isa ) {
+#       my $meta = $super->meta;
+#       if( $meta->has_package_symbol(\''.$slot.'\') ){
+#         return $meta->get_package_symbol(\''.$slot.'\');
+#       }
+#     }
+#     return;
+#   }';
+#   my $accessor = eval $code;