Changeset 6792

Show
Ignore:
Timestamp:
09/06/05 13:05:40 (3 years ago)
Author:
Stevan
Message:

Perl6::MetaModel? 2.0 -
* added $::Module into the hierarchy, now:

$::Class->isa($::Module) && $::Class->isa($::Object)

* moved name, version, authority and identifier methods

to $::Module

  • adjusted tests and files appropriately
Location:
perl5/Perl6-MetaModel2.0
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • perl5/Perl6-MetaModel2.0/lib/genesis.pl

    r6557 r6792  
    66BEGIN { do "lib/pneuma.pl" }; 
    77 
     8## Now create some of the other things we need ... 
     9 
     10## ---------------------------------------------------------------------------- 
     11## Module 
     12 
     13$::Module = $::Class->new('$:name' => 'Module'); 
     14 
     15$::Module->superclasses([ $::Object ]); 
     16 
     17$::Module->add_attribute('$:name'       => ::make_attribute('$:name')); 
     18$::Module->add_attribute('$:version'    => ::make_attribute('$:version')); 
     19$::Module->add_attribute('$:authority'  => ::make_attribute('$:authority')); 
     20 
     21$::Module->add_method('name' => ::make_method(sub { 
     22    my $self = shift; 
     23    ::opaque_instance_attrs($self)->{'$:name'} = shift if @_;         
     24    ::opaque_instance_attrs($self)->{'$:name'}; 
     25}, $::Module)); 
     26 
     27$::Module->add_method('version' => ::make_method(sub { 
     28    my ($self, $version) = @_; 
     29    if (defined $version) { 
     30        ($version =~ /^\d+\.\d+\.\d+$/) 
     31            || confess "The version ($version) is not in the correct format '0.0.0'"; 
     32        ::opaque_instance_attrs($self)->{'$:version'} = $version; 
     33    } 
     34    ::opaque_instance_attrs($self)->{'$:version'};     
     35}, $::Module)); 
     36 
     37$::Module->add_method('authority' => ::make_method(sub { 
     38    my $self = shift; 
     39    ::opaque_instance_attrs($self)->{'$:authority'} = shift if @_;         
     40    ::opaque_instance_attrs($self)->{'$:authority'}; 
     41}, $::Module)); 
     42 
     43$::Module->add_method('identifier' => ::make_method(sub { 
     44    return join '-' => ($::SELF->name, $::SELF->version, ($::SELF->authority || ())); 
     45}, $::Module)); 
     46 
    847# ... this makes ::Class a subclass of ::Object 
    948# the result of this is (Theos) 
    1049 
    1150# < Class is a subclass of Object > 
    12 ::opaque_instance_attrs($::Class)->{'@:superclasses'} = [ $::Object ]; 
     51::opaque_instance_attrs($::Class)->{'@:superclasses'} = [ $::Module, $::Object ]; 
    1352 
    14 ::opaque_instance_attrs($::Class)->{'@:MRO'} = [ $::Class, $::Object ]; 
     53# NOTE: 
     54# this is to avoid recursion 
     55::opaque_instance_attrs($::Class)->{'@:MRO'} = [ $::Class, $::Module, $::Object ]; 
    1556::opaque_instance_attrs($::Object)->{'@:MRO'} = [ $::Object ]; 
    1657 
     58## ---------------------------------------------------------------------------- 
     59## Role 
     60 
     61$::Role = $::Class->new('$:name' => 'Role'); 
     62 
     63$::Role->superclasses([ $::Module ]); 
     64 
     65$::Role->add_attribute('@:subroles'   => ::make_attribute('@:subroles')); 
     66$::Role->add_attribute('%:methods'    => ::make_attribute('%:methods')); 
     67$::Role->add_attribute('%:attributes' => ::make_attribute('%:attributes')); 
     68 
     69$::Role->add_method('add_method' => ::make_method(sub { 
     70    my ($self, $label, $method) = @_; 
     71    # NOTE:  
     72    # the $method here can be undefined, this is to allow 
     73    # for methods which do not have an implementation. These 
     74    # methods are then required to be implemented by the  
     75    # composing class. 
     76    # (see A12/Class Composition with Roles/Declaration of Roles/Interfaces) 
     77    ::opaque_instance_attrs($self)->{'%:methods'}->{$label} = $method; 
     78}, $::Role)); 
     79 
     80$::Role->add_method('get_method_list' => ::make_method(sub { 
     81    keys %{::opaque_instance_attrs($::SELF)->{'%:methods'}}; 
     82}, $::Role)); 
     83 
     84$::Role->add_method('add_attribute' => ::make_method(sub { 
     85    my ($self, $label, $attribute) = @_; 
     86    ::opaque_instance_attrs($self)->{'%:attributes'}->{$label} = $attribute; 
     87}, $::Role)); 
     88 
     89$::Role->add_method('get_attribute_list' => ::make_method(sub { 
     90    keys %{::opaque_instance_attrs($::SELF)->{'%:attributes'}}; 
     91}, $::Role)); 
     92 
     93$::Role->add_method('subroles' => ::make_method(sub { 
     94    my $self = shift; 
     95    ::opaque_instance_attrs($self)->{'@:subroles'} = shift if @_; 
     96    ::opaque_instance_attrs($self)->{'@:subroles'}; 
     97}, $::Role)); 
     98 
     99$::Role->add_method('does' => ::make_method(sub { 
     100    my $self = shift; 
     101    if (my $role_name = shift) { 
     102        foreach (@{::opaque_instance_attrs($self)->{'@:subroles'}}) { 
     103            return 1 if $_->name eq $role_name;             
     104        } 
     105        return 0; 
     106    } 
     107    return map { $_->name } @{::opaque_instance_attrs($self)->{'@:subroles'}}; 
     108}, $::Role)); 
     109 
     110 
    171111; 
  • perl5/Perl6-MetaModel2.0/lib/gnosis.pl

    r6669 r6792  
    1616        ( 
    1717            # meta-information 
    18             '$:name'             => $attrs{'$:name'}      || undef, 
    19             '$:version'          => $attrs{'$:version'}   || '0.0.0', 
    20             '$:authority'        => $attrs{'$:authority'} || undef, 
     18            '$:name'             => $attrs{'$:name'} || undef, 
    2119            # the guts 
    2220            '@:MRO'              => [], 
  • perl5/Perl6-MetaModel2.0/lib/metamorph.pl

    r6766 r6792  
    9999    ::opaque_instance_attrs($self)->{'$:name'} = shift if @_;         
    100100    ::opaque_instance_attrs($self)->{'$:name'}; 
    101 }, $::Class)); 
    102  
    103 $::Class->add_method('version' => ::make_method(sub { 
    104     my ($self, $version) = @_; 
    105     if (defined $version) { 
    106         ($version =~ /^\d+\.\d+\.\d+$/) 
    107             || confess "The version ($version) is not in the correct format '0.0.0'"; 
    108         ::opaque_instance_attrs($self)->{'$:version'} = $version; 
    109     } 
    110     ::opaque_instance_attrs($self)->{'$:version'};     
    111 }, $::Class)); 
    112  
    113 $::Class->add_method('authority' => ::make_method(sub { 
    114     my $self = shift; 
    115     ::opaque_instance_attrs($self)->{'$:authority'} = shift if @_;         
    116     ::opaque_instance_attrs($self)->{'$:authority'}; 
    117 }, $::Class)); 
    118  
    119 $::Class->add_method('identifier' => ::make_method(sub { 
    120     return join '-' => ($::SELF->name, $::SELF->version, ($::SELF->authority || ())); 
    121101}, $::Class)); 
    122102 
     
    389369 
    390370$::Class->add_attribute('$:name'             => ::make_attribute('$:name')); 
    391 $::Class->add_attribute('$:version'          => ::make_attribute('$:version')); 
    392 $::Class->add_attribute('$:authority'        => ::make_attribute('$:authority')); 
    393371$::Class->add_attribute('@:MRO'              => ::make_attribute('@:MRO')); 
    394372$::Class->add_attribute('@:superclasses'     => ::make_attribute('@:superclasses')); 
  • perl5/Perl6-MetaModel2.0/t/03_metamorph.t

    r6557 r6792  
    44use warnings; 
    55 
    6 use Test::More tests => 88; 
     6use Test::More tests => 72; 
    77use Test::Exception; 
    88 
     
    2828    $::Class->name; 
    2929} 'Class', '... got the name we expected'; 
    30  
    31 lives_ok_and_is { 
    32     $::Class->version; 
    33 } '0.0.0', '... got the version we expected'; 
    34  
    35 lives_ok_and_is { 
    36     $::Class->authority; 
    37 } undef, '... got the authority we expected'; 
    38  
    39 lives_ok_and_is { 
    40     $::Class->identifier; 
    41 } 'Class-0.0.0', '... got the identifier we expected'; 
    4230 
    4331lives_ok_and_is { 
     
    8674# check public methods 
    8775foreach my $method_name (qw(name 
    88                             version 
    89                             authority 
    90                             identifier 
    9176                            superclasses 
    9277                            MRO 
     
    120105} 
    121106 
    122 my @attribute_name_list = ('$:name', 
    123                            '$:version',       
    124                            '$:authority',         
     107my @attribute_name_list = ('$:name',        
    125108                           '@:MRO',               
    126109                           '@:superclasses',     
  • perl5/Perl6-MetaModel2.0/t/05_genesis.t

    r6692 r6792  
    44use warnings; 
    55 
    6 use Test::More tests => 28; 
     6use Test::More tests => 47; 
    77use Test::Exception;  
    88 
     
    1111is_deeply( 
    1212    $::Class->superclasses,  
    13     [ $::Object ],  
    14     '... $::Class->superclasses() is $::Object'); 
     13    [ $::Module, $::Object ],  
     14    '... $::Class->superclasses() is [ $::Module, $::Object ]');     
    1515 
    1616is_deeply( 
     
    2121is_deeply( 
    2222    [ $::Class->MRO() ],  
    23     [ $::Class, $::Object ],  
    24     '... $::Class->MRO() is ($::Class, $::Object)');     
     23    [ $::Class, $::Module, $::Object ],  
     24    '... $::Class->MRO() is ($::Class, $::Module, $::Object)');     
     25 
     26ok($::Class->is_a('Module'), '... $::Class->is_a(Module)'); 
     27ok($::Class->isa('Module'), '... $::Class->isa(Module)'); 
    2528 
    2629ok($::Class->is_a('Object'), '... $::Class->is_a(Object)'); 
    2730ok($::Class->isa('Object'), '... $::Class->isa(Object)'); 
    2831 
    29 # can call all of Object's class methods ... 
    30 foreach my $method_name (qw(new bless CREATE isa can)) { 
     32ok($::Object->is_a('Object'), '... $::Object->is_a(Object)'); 
     33ok($::Object->isa('Object'), '... $::Object->isa(Object)'); 
     34 
     35ok($::Module->is_a('Object'), '... $::Module->is_a(Object)'); 
     36ok($::Module->isa('Object'), '... $::Module->isa(Object)'); 
     37 
     38# Module can call all of Modules's methods and all of Object's ... 
     39foreach my $method_name (qw(name version authority identifier 
     40                            BUILD BUILDALL DESTROYALL isa can)) { 
     41    ok($::Module->can($method_name), '... Module->can(' . $method_name . ')'); 
     42} 
     43 
     44# Class can call all of Modules's methods ... 
     45foreach my $method_name (qw(name version authority identifier)) { 
    3146    ok($::Class->can($method_name), '... Class->can(' . $method_name . ')'); 
    3247} 
    3348 
    34 # can call all of Object's instance methods as well ... 
     49# Object can call all of Modules's methods ... 
     50foreach my $method_name (qw(name version authority identifier)) { 
     51    ok($::Object->can($method_name), '... Object->can(' . $method_name . ')'); 
     52} 
     53 
     54# can call all of Object's methods as well ... 
    3555foreach my $method_name (qw(BUILD BUILDALL DESTROYALL isa can)) { 
    3656    ok($::Class->can($method_name), '... Class->can(' . $method_name . ')'); 
    3757} 
    3858 
    39 # now call some Object methods 
    40  
    41 ok($::Object->isa('Object'), '... Object->isa(Object)'); 
    42  
    43 foreach my $method_name (qw(new bless CREATE isa can)) { 
    44     ok($::Object->can($method_name), '... Object->can(' . $method_name . ')'); 
    45 } 
     59# now create an Object 
    4660 
    4761my $iObject = $::Object->new(); 
    4862ok($iObject->isa('Object'), '... iObject->isa(Object)'); 
    4963 
    50 is($iObject->id, 3, '... $iObject is the third object in the system'); 
     64cmp_ok($iObject->id, '>', 3, '... $iObject is at least the third object in the system'); 
    5165 
    5266foreach my $method_name (qw(BUILD BUILDALL DESTROYALL isa can)) { 
    5367    ok($iObject->can($method_name), '... iObject->can(' . $method_name . ')'); 
    5468} 
     69 
     70# now create a Module 
     71 
     72# now create an Object 
     73 
     74my $MyModule = $::Module->new(); 
     75ok($MyModule->isa('Module'), '... MyModule->isa(Module)'); 
     76ok($MyModule->isa('Object'), '... MyModule->isa(Object)'); 
     77 
     78cmp_ok($MyModule->id, '>', 3, '... $MyModule is the at least the third object in the system'); 
     79 
     80# Module can call all of Modules's methods and all of Object's ... 
     81foreach my $method_name (qw(name version authority identifier)) { 
     82    ok($MyModule->can($method_name), '... MyModule->can(' . $method_name . ')'); 
     83} 
     84 
     85 
     86 
  • perl5/Perl6-MetaModel2.0/t/06_bootstrapped.t

    r6692 r6792  
    44use warnings; 
    55 
    6 use Test::More tests => 14; 
     6use Test::More tests => 18; 
    77use Test::Exception;  
    88 
     
    1414is($Foo->name, 'Foo', '... Foo->name == Foo'); 
    1515is($Foo->version, '0.0.1', '... Foo->version == 0.0.1'); 
     16is($Foo->authority, undef, '... Foo->authority == undef'); 
     17 
     18is($Foo->identifier, 'Foo-0.0.1', '... Foo->identifier == Foo-0.0.1'); 
    1619 
    1720lives_ok { 
     
    3336# Now try to create a new class .... 
    3437 
    35 my $Bar = $::Class->new('$:name' => 'Bar', '$:version' => '0.0.1'); 
     38my $Bar = $::Class->new('$:name' => 'Bar', '$:version' => '0.0.1', '$:authority' => 'cpan:JRANDOM'); 
    3639is($Bar->name, 'Bar', '... Bar->name == Bar'); 
    3740is($Bar->version, '0.0.1', '... Bar->version == 0.0.1'); 
     41is($Bar->authority, 'cpan:JRANDOM', '... Bar->authority == cpan:JRANDOM'); 
     42 
     43is($Bar->identifier, 'Bar-0.0.1-cpan:JRANDOM', '... Bar->identifier == Bar-0.0.1-cpan:JRANDOM'); 
    3844 
    3945lives_ok { 
  • perl5/Perl6-MetaModel2.0/t/07_Perl6_MetaModel.t

    r6610 r6792  
    44use warnings; 
    55 
    6 use Test::More tests => 26; 
     6use Test::More tests => 27; 
    77use Test::Exception;  
    88 
     
    1212ok($::Object->isa('Object'), '... genesis was loaded ok'); 
    1313ok($::Class->isa('Class'), '... genesis was loaded ok'); 
     14ok($::Class->isa('Module'), '... genesis was loaded ok'); 
    1415ok($::Class->isa('Object'), '... genesis was loaded ok'); 
    1516is_deeply( 
    1617    $::Class->superclasses, 
    17     [ $::Object ], 
     18    [ $::Module, $::Object ], 
    1819    '... genesis was loaded ok'); 
    1920