1 package Reaction::InterfaceModel::Collection;
4 use Scalar::Util qw/refaddr blessed/;
5 use aliased 'Reaction::Meta::InterfaceModel::Object::DomainModelAttribute';
7 # WARNING - DANGER: this is just an RFC, please DO NOT USE YET
9 class Collection is "Reaction::InterfaceModel::Object", which {
11 # consider supporting slice, first, iterator, last etc.
12 # pager functionality should probably be a role
14 # IM objects don't have write methods because those are handled through actions,
15 # no support for write actions either unless someone makes a good case for it
16 # many models may not even be writable, so we cant make that assumption...
18 # I feel like we should hasa result_class or object_class ?
19 # having this here would remove a lot of PITA complexity from
20 # ObjectClass and SchemaClass when it comes to munging with internals
22 #Answer: No, because collections should be able to hold more than one type of object
24 # ALL IMPLEMENTATIONS ARE TO ILLUSTRATE POSSIBLE BEHAVIOR ONLY. DON'T CONSIDER
25 # THEM CORRECT, OR FINAL. JUST A ROUGH DRAFT.
27 #domain_models are 'ro' unless otherwise specified
28 has _collection_store => (
32 clearer => "_clear_collection_store",
33 metaclass => DomainModelAttribute,
36 has 'member_type' => (is => 'ro', isa => 'ClassName');
38 implements _build__collection_store => as { [] };
40 implements members => as {
42 return @{ $self->_collection_store };
45 #return new member or it's index # ?
46 implements add_member => as {
49 confess "Argument passed is not an object" unless blessed $new;
50 confess "Object Passed does not meet constraint isa Reaction::InterfaceModel::Object"
51 unless $new->isa('Reaction::InterfaceModel::Object');
52 my $store = $self->_collection_store;
54 return $#$store; #return index # of inserted item
57 implements remove_member => as {
60 confess "Argument passed is not an object" unless blessed $rem;
61 confess "Object Passed does not meet constraint isa Reaction::InterfaceModel::Object"
62 unless $rem->isa('Reaction::InterfaceModel::Object');
64 my $addr = refaddr $rem;
65 @{ $self->_collection_store } = grep {$addr ne refaddr $_ } @{ $self->_store };
69 implements count_members => sub{
71 return scalar @{ $self->_collection_store };
80 Reaction::InterfaceModel::Collection - Generic collections of
81 L<Reaction::InterfaceModel::Object>s
85 The base class for C<InterfaceModel::Collection>s. The functionality implemented here
86 is minimal and it is expected that specialized collections be built by sublclassing
87 this and exploiting the roles system.
93 Returns a list containing all known members of the collection
95 =head2 add_member $object
97 Will add the object passed to the collection
99 =head2 remove_member $object
101 Removed the object passed from the collection, if present
105 Returns the number of objects in the collection.
109 =head2 _collection_store
111 Read-write & lazy_build. Holds the arrayref where the collection of objects is
112 presently stored. Has a clearer of C<_clear_collection_store> and a predicate of
113 C<_has_collection_store>.
115 =head1 PRIVATE METHODS
117 _build__collection_store
119 Builder method for attribute_collection_store, returns an empty arrayref
123 See L<Reaction::Class> for authors.
127 See L<Reaction::Class> for the license.