[Rt-commit] r5595 - in Object-Declare: lib/Object t

audreyt at bestpractical.com audreyt at bestpractical.com
Mon Jul 17 21:37:59 EDT 2006


Author: audreyt
Date: Mon Jul 17 21:37:57 2006
New Revision: 5595

Modified:
   Object-Declare/Changes
   Object-Declare/lib/Object/Declare.pm
   Object-Declare/t/01-basic.t

Log:
* This be 0.05.
* Support for ordered declarations, via list-context return of 
  "declare".  In scalar context, it still returns a hash reference.

Modified: Object-Declare/Changes
==============================================================================
--- Object-Declare/Changes	(original)
+++ Object-Declare/Changes	Mon Jul 17 21:37:57 2006
@@ -1,3 +1,8 @@
+[Changes for 0.05 - 2006-07-17]
+
+* Support for ordered declarations, via list-context return of 
+  "declare".  In scalar context, it still returns a hash reference.
+
 [Changes for 0.04 - 2006-07-17]
 
 * Support for plural values via "are":

Modified: Object-Declare/lib/Object/Declare.pm
==============================================================================
--- Object-Declare/lib/Object/Declare.pm	(original)
+++ Object-Declare/lib/Object/Declare.pm	Mon Jul 17 21:37:57 2006
@@ -1,5 +1,5 @@
 package Object::Declare;
-$Object::Declare::VERSION = '0.04';
+$Object::Declare::VERSION = '0.05';
 
 use 5.006;
 use strict;
@@ -51,7 +51,7 @@
     my $from    = caller;
 
     # Table of collected objects.
-    my $objects = {};
+    my @objects;
 
     no strict 'refs';
     no warnings 'redefine';
@@ -62,13 +62,13 @@
     $override->replace("UNIVERSAL::$_" => \&_universal) for @$copula;
     $override->replace("$_\::AUTOLOAD" => \&_autoload) for @$copula;
     $override->replace(
-        "$from\::$_" => _make_object($mapping->{$_} => $objects)
+        "$from\::$_" => _make_object($mapping->{$_} => \@objects)
     ) for keys %$mapping;
 
     # Let's play katamari!
     $code->();
 
-    return $objects;
+    return(wantarray ? @objects : { @objects });
 }
 
 sub _universal {
@@ -90,7 +90,7 @@
 
     return sub {
         my ($name, $katamari) = @_;
-        $schema->{$name} = $class->new($katamari ? $katamari->unroll : ());
+        push @$schema, $name => scalar $class->new($katamari ? $katamari->unroll : ());
     };
 }
 
@@ -126,7 +126,7 @@
 
     use Object::Declare ['MyApp::Column', 'MyApp::Param'];
 
-    my $objects = declare {
+    my %objects = declare {
 
     param foo =>
         is immutable,
@@ -147,6 +147,10 @@
 objects with a declarative syntax, similar to how L<Jifty::DBI::Schema>
 defines its columns.
 
+In list context, C<declare> returns a list of name/object pairs in the
+order of declaration (allowing duplicates), suitable for putting into a hash.
+In scalar context, C<declare> returns a hash reference.
+
 Using a flexible C<import> interface, one can change exported helper
 functions names (I<declarator>), words to link labels and values together
 (I<copula>), and the table of named classes to declare (I<mapping>):

Modified: Object-Declare/t/01-basic.t
==============================================================================
--- Object-Declare/t/01-basic.t	(original)
+++ Object-Declare/t/01-basic.t	Mon Jul 17 21:37:57 2006
@@ -1,21 +1,35 @@
 use strict;
-use Test::More tests => 2, import => ['is_deeply'];
+use Test::More tests => 3, import => ['is_deeply'];
 use ok 'Object::Declare' => ['MyApp::Column'];
 
 sub MyApp::Column::new { shift; return { @_ } }
 
-my $objects = declare {
+sub do_declare { declare {
+    column x =>
+        field1 is 'xxx',
+        field2 are 'XXX', 'XXX',
+        is field3;
+
+    column y =>
+        field1 is 'yyy',
+        field2 is 'YYY';
+} }
 
-column x =>
-    field1 is 'xxx',
-    field2 are 'XXX', 'XXX',
-    is field3;
-
-column y =>
-    field1 is 'yyy',
-    field2 is 'YYY';
+my @objects = do_declare;
 
-};
+is_deeply(\@objects => [
+    x => {
+            'field1' => 'xxx',
+            'field2' => ['XXX', 'XXX'],
+            'field3' => 1
+            },
+    y => {
+            'field1' => 'yyy',
+            'field2' => 'YYY'
+            },
+], 'object declared correctly (list context)');
+
+my $objects = do_declare;
 
 is_deeply($objects => {
     x => {
@@ -27,5 +41,5 @@
             'field1' => 'yyy',
             'field2' => 'YYY'
             },
-}, 'object declared correctly');
+}, 'object declared correctly (scalar context)');
 


More information about the Rt-commit mailing list