You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2011/02/24 22:11:38 UTC

[lucy-commits] svn commit: r1074290 - in /incubator/lucy/trunk/clownfish: lib/Clownfish/Class.pm lib/Clownfish/Parser.pm t/400-class.t

Author: marvin
Date: Thu Feb 24 21:11:38 2011
New Revision: 1074290

URL: http://svn.apache.org/viewvc?rev=1074290&view=rev
Log:
Simplify the Clownfish::Class constructor by offloading the addition of vars,
functions, and methods to later add_xxxx() calls.

Modified:
    incubator/lucy/trunk/clownfish/lib/Clownfish/Class.pm
    incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm
    incubator/lucy/trunk/clownfish/t/400-class.t

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Class.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Class.pm?rev=1074290&r1=1074289&r2=1074290&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Class.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Class.pm Thu Feb 24 21:11:38 2011
@@ -45,10 +45,6 @@ our %create_PARAMS = (
     class_name        => undef,
     cnick             => undef,
     parent_class_name => undef,
-    methods           => undef,
-    functions         => undef,
-    member_vars       => undef,
-    inert_vars        => undef,
     docucomment       => undef,
     inert             => undef,
     final             => undef,
@@ -102,42 +98,11 @@ sub create {
     $args{inert} ||= 0;
     $args{final} ||= 0;
 
-    # Verify that members of supplied arrays meet "is a" requirements.
-    my $functions   = delete $args{functions}   || [];
-    my $methods     = delete $args{methods}     || [];
-    my $member_vars = delete $args{member_vars} || [];
-    my $inert_vars  = delete $args{inert_vars}  || [];
-    for (qw( functions methods member_vars inert_vars )) {
-        next unless defined $args{$_};
-        next if reftype( $args{$_} ) eq 'ARRAY';
-        confess("Supplied parameter '$_' is not an arrayref");
-    }
-    for (@$functions) {
-        confess("Not a Clownfish::Function")
-            unless a_isa_b( $_, 'Clownfish::Function' );
-    }
-    for (@$methods) {
-        confess("Not a Clownfish::Method")
-            unless a_isa_b( $_, 'Clownfish::Method' );
-    }
-    for ( @$member_vars, @$inert_vars ) {
-        confess("Not a Clownfish::Variable")
-            unless a_isa_b( $_, 'Clownfish::Variable' );
-    }
-
-    # Make it possible to look up methods and functions by name.
-    my %methods_by_name   = map { ( $_->micro_sym => $_ ) } @$methods;
-    my %functions_by_name = map { ( $_->micro_sym => $_ ) } @$functions;
-
     # Validate attributes.
     my $attributes = delete $args{attributes} || {};
     confess("Param 'attributes' not a hashref")
         unless reftype($attributes) eq 'HASH';
 
-    # Validate inert param.
-    confess("Inert classes can't have methods")
-        if ( $args{inert} and scalar @$methods );
-
     my $package = ref($either) || $either;
     $args{parcel} = Clownfish::Parcel->acquire( $args{parcel} );
     $args{exposure}  ||= 'parcel';
@@ -147,12 +112,12 @@ sub create {
         docucomment source_class parent_class_name final inert )} );
 
     $attributes{$self}        = $attributes;
-    $meth_by_name{$self}      = \%methods_by_name;
-    $func_by_name{$self}      = \%functions_by_name;
-    $functions{$self}         = $functions;
-    $methods{$self}           = $methods;
-    $member_vars{$self}       = $member_vars;
-    $inert_vars{$self}        = $inert_vars;
+    $meth_by_name{$self}      = {};
+    $func_by_name{$self}      = {};
+    $functions{$self}         = [];
+    $methods{$self}           = [];
+    $member_vars{$self}       = [];
+    $inert_vars{$self}        = [];
     $overridden{$self}        = {};
 
     # Store in registry.
@@ -241,6 +206,28 @@ sub add_method {
     $self->_meth_by_name->{ $method->micro_sym } = $method;
 }
 
+sub add_function {
+    my ( $self, $function ) = @_;
+    confess("Not a Function") unless a_isa_b( $function, "Clownfish::Function" );
+    confess("Can't call add_function after grow_tree") if $self->_tree_grown;
+    push @{ $self->functions }, $function;
+    $self->_func_by_name->{ $function->micro_sym } = $function;
+}
+
+sub add_member_var {
+    my ( $self, $var ) = @_;
+    confess("Not a Variable") unless a_isa_b( $var, "Clownfish::Variable" );
+    confess("Can't call add_member_var after grow_tree") if $self->_tree_grown;
+    push @{ $self->member_vars }, $var;
+}
+
+sub add_inert_var {
+    my ( $self, $var ) = @_;
+    confess("Not a Variable") unless a_isa_b( $var, "Clownfish::Variable" );
+    confess("Can't call add_inert_var after grow_tree") if $self->_tree_grown;
+    push @{ $self->inert_vars }, $var;
+}
+
 # Create dumpable functions unless hand coded versions were supplied.
 sub _create_dumpables {
     my $self = shift;
@@ -369,10 +356,6 @@ Retrieve a Class, if one has already bee
         source_class      => undef,              # default: same as class_name
         parent_class_name => 'Crustacean::Claw', # default: undef
         inert             => undef,              # default: undef
-        methods           => \@methods,          # default: []
-        functions         => \@funcs,            # default: []
-        member_vars       => \@members,          # default: []
-        inert_vars        => \@inert_vars,       # default: []
         docucomment       => $documcom,          # default: undef,
         attributes        => \%attributes,       # default: {}
     );
@@ -395,17 +378,6 @@ in order to establish the class hierarch
 =item * B<inert> - Should be true if the class is inert, i.e. cannot be
 instantiated.
 
-=item * B<methods> - An array where each element is a Clownfish::Method.
-
-=item * B<functions> - An array where each element is a Clownfish::Method.
-
-=item * B<member_vars> - An array where each element is a
-Clownfish::Variable and should be a member variable in each instantiated
-object.
-
-=item * B<inert_vars> - An array where each element is a
-Clownfish::Variable and should be a shared (class) variable.
-
 =item * B<docucomment> - A Clownfish::DocuComment describing this Class.
 
 =item * B<attributes> - An arbitrary hash of attributes.
@@ -437,6 +409,25 @@ Add a child class. 
 
 Add a Method to the class.  Valid only before grow_tree() is called.
 
+=head2 add_function
+
+    $class->add_function($function);
+
+Add a Function to the class.  Valid only before grow_tree() is called.
+
+=head2 add_member_var
+
+    $class->add_member_var($var);
+
+Add a member variable to the class.  Valid only before grow_tree() is called.
+
+=head2 add_inert_var
+
+    $class->add_inert_var($var);
+
+Add an inert (class) variable to the class.  Valid only before grow_tree() is
+called.
+
 =head2 function 
 
     my $do_stuff_function = $class->function("do_stuff");

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm?rev=1074290&r1=1074289&r2=1074290&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm Thu Feb 24 21:11:38 2011
@@ -487,21 +487,22 @@ sub new_class {
         }
     }
 
-    return Clownfish::Class->create(
+    my $class = Clownfish::Class->create(
         parcel            => $parcel,
         class_name        => $item->{class_name},
         cnick             => $item->{'cnick(?)'}[0],
         parent_class_name => $item->{'class_inheritance(?)'}[0],
-        member_vars       => \@member_vars,
-        functions         => \@functions,
-        methods           => \@methods,
-        inert_vars        => \@inert_vars,
         docucomment       => $item->{'docucomment(?)'}[0],
         source_class      => $source_class,
         inert             => $class_modifiers{inert},
         final             => $class_modifiers{final},
         attributes        => \%class_attributes,
     );
+    $class->add_method($_)     for @methods;
+    $class->add_function($_)   for @functions;
+    $class->add_member_var($_) for @member_vars;
+    $class->add_inert_var($_)  for @inert_vars;
+    return $class;
 }
 
 sub new_file {

Modified: incubator/lucy/trunk/clownfish/t/400-class.t
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/t/400-class.t?rev=1074290&r1=1074289&r2=1074290&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/t/400-class.t (original)
+++ incubator/lucy/trunk/clownfish/t/400-class.t Thu Feb 24 21:11:38 2011
@@ -41,14 +41,14 @@ my $tread_water = Clownfish::Function->n
     param_list  => $parser->param_list('()'),
 );
 my %foo_create_args = (
-    parcel      => 'Neato',
-    class_name  => 'Foo',
-    member_vars => [$thing],
-    inert_vars  => [$widget],
-    functions   => [$tread_water],
+    parcel     => 'Neato',
+    class_name => 'Foo',
 );
 
 my $foo = Clownfish::Class->create(%foo_create_args);
+$foo->add_function($tread_water);
+$foo->add_member_var($thing);
+$foo->add_inert_var($widget);
 eval { Clownfish::Class->create(%foo_create_args) };
 like( $@, qr/conflict/i,
     "Can't call create for the same class more than once" );
@@ -99,7 +99,8 @@ my %inert_args = (
     inert      => 1,
 );
 eval {
-    Clownfish::Class->create( %inert_args, methods => [$inert_do_stuff] );
+    my $class = Clownfish::Class->create(%inert_args);
+    $class->add_method($inert_do_stuff);
 };
 like(
     $@,