[Catalyst-commits] r14383 - in CatalystX-CRUD/CatalystX-CRUD/trunk: . lib/CatalystX/CRUD lib/CatalystX/CRUD/Model lib/CatalystX/CRUD/Object

karpet at dev.catalyst.perl.org karpet at dev.catalyst.perl.org
Mon Nov 5 03:06:19 GMT 2012


Author: karpet
Date: 2012-11-05 03:06:19 +0000 (Mon, 05 Nov 2012)
New Revision: 14383

Modified:
   CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
   CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/File.pm
   CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object.pm
   CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object/File.pm
Log:
add is_new() method to Object class

Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/Changes	2012-11-05 03:05:32 UTC (rev 14382)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/Changes	2012-11-05 03:06:19 UTC (rev 14383)
@@ -258,4 +258,5 @@
         * add Results->serialize() method
         * refactor some Controller methods into a Role class
         * Model::File->fetch() now returns undef if file does not exist. Add Model::File->prep_new_object().
+        * add new required method to Object class: is_new()
 

Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/File.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/File.pm	2012-11-05 03:05:32 UTC (rev 14382)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/File.pm	2012-11-05 03:06:19 UTC (rev 14383)
@@ -286,7 +286,7 @@
 =cut
 
 sub add_related {
-    my ( $self, $file, $rel_name, $other_file_name ) = @_;
+    my ( $self, $file, $rel_name, $other_file_name, $overwrite ) = @_;
 
     if ( !$SYMLINK_SUPPORTED ) {
         $self->context->log->error(
@@ -304,6 +304,9 @@
 
         # if in the same dir, already related.
         if ( $other_file->dir eq $file->dir ) {
+            if ($overwrite) {
+                return 1;    # nothing to do
+            }
             $self->throw_error("relationship already exists");
         }
 
@@ -318,8 +321,15 @@
     else {
         $self->throw_error("unsupported relationship name: $rel_name");
     }
+
+    return $other_file;
 }
 
+sub put_related {
+    my $self = shift;
+    return $self->add_related( @_, 1 );    # overwrite
+}
+
 =head2 rm_related( I<file>, I<rel_name>, I<other_file_name> )
 
 For I<rel_name> of "dir" will create a symlink for I<other_file_name>'s

Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object/File.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object/File.pm	2012-11-05 03:05:32 UTC (rev 14382)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object/File.pm	2012-11-05 03:06:19 UTC (rev 14383)
@@ -119,6 +119,17 @@
     return $self->delegate->remove;
 }
 
+=head2 is_new
+
+Returns true if the file does not yet exist.
+
+=cut
+
+sub is_new {
+    my $self = shift;
+    return defined -s $self->delegate ? 0 : 1;
+}
+
 sub _write {
     my $self = shift;
     my $dir  = $self->delegate->dir;
@@ -134,13 +145,13 @@
 
 =head2 serialize
 
-Returns the File object as a hashref with 2 keys: path and content.
+Returns the File object as a hashref with 2 keys: file and content.
 
 =cut
 
 sub serialize {
     my $self = shift;
-    return { path => $self->delegate . "", content => $self->content };
+    return { file => $self->file, content => $self->content };
 }
 
 1;

Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object.pm	2012-11-05 03:05:32 UTC (rev 14382)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object.pm	2012-11-05 03:06:19 UTC (rev 14383)
@@ -9,11 +9,6 @@
 use MRO::Compat;
 use mro 'c3';
 
-#use overload
-#    '""'     => sub { return dump( $_[0]->serialize ) . "" },
-#    'bool'   => sub {1},
-#    fallback => 1;
-
 __PACKAGE__->mk_ro_accessors(qw( delegate ));
 __PACKAGE__->mk_classdata('delegate_class');
 
@@ -108,6 +103,22 @@
 sub update { shift->throw_error("must implement update") }
 sub delete { shift->throw_error("must implement delete") }
 
+=head2 is_new
+
+Return results should be boolean indicating whether the object
+already exists or not. Expectation is code like:
+
+ if ($object->is_new) {
+     $object->create;
+ }
+ else {
+     $object->update;
+ }
+
+=cut
+
+sub is_new { shift->throw_error("must implement is_new") }
+
 =head2 serialize
 
 Stringify the object. This class overloads the string operators




More information about the Catalyst-commits mailing list