Abstract-Meta-Class

 view release on metacpan or  search on metacpan

t/meta/association.t  view on Meta::CPAN

        );
        
        my $master = Master->new(name    => 'foo', details => [@details]);
        ::is($details[$_]->master, $master, "should associate by biderectional def") for (0 .. 2);
        
        my $master2 = Master->new(name    => 'foo2');
        $details[-1]->set_master($master2);
        my @detail1 = values %{$master->details};
        my @details_ids1 = keys %{$master->details};
        
        ::is_deeply([sort @detail1], [sort @details[0 .. 1]], 'should have 2 details elements');
        ::is_deeply([sort @details_ids1], [1,2], 'should have 2 details index');
        ::is($master2->detail(3), $details[-1], "should have details");
    }



        #THE OTHER END BIDIRECTIONAL ASSOCIATION, DEASSOCIATION
    {
        package MasterA;
        use Abstract::Meta::Class ':all';
        has '$.name' => (required => 1);
        has '@.details' => (
            associated_class => 'DetailA',
            index_by         => 'id',
            item_accessor    => 'detail',
            the_other_end    => 'master',
        );
    }

    {
        package DetailA;
        use Abstract::Meta::Class ':all';
        has '$.id'     => (required => 1);
        has '$.master' => (
            associated_class => 'MasterA',
            the_other_end    => 'details'
        );
    }

    {    
        my @details  = (
            DetailA->new(id => 1),
            DetailA->new(id => 2),
            DetailA->new(id => 3),
        );
        
        my $master = MasterA->new(name    => 'foo', details => [@details]);
        ::is($details[$_]->master, $master, "should associate by biderectional def") for (0 .. 2);
        
        my $master2 = MasterA->new(name    => 'foo2');
        $details[-1]->set_master($master2);
        my @detail1 = $master->details;
        
        ::is_deeply(\@detail1, [@details[0 .. 1]], 'should have 2 details elements');
        ::is($master2->detail(0), $details[-1], "should have details");
        
        
        
        $master->cleanup;
        ::is($_->master, undef, 'should be deassociiated') for @details[0 .. 1];
        
        my $details = $master->details;
        ::is_deeply($details, [], 'should not have details association');
    }


    {    
        my @details  = (
            DetailA->new(id => 1),
            DetailA->new(id => 2),
            DetailA->new(id => 3),
        );
        
        my $master = MasterA->new(name    => 'foo', details => [@details]);
        $master->remove_details($details[1]);
        ::ok($details[1]->master, 'should deassociate the other end');
        my $details = $master->details;
        ::is_deeply($details, [$details[0], $details[-1]], 'should remove object');
    }

}



( run in 2.774 seconds using v1.01-cache-2.11-cpan-d80b1682f3f )