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 use namespace::clean -except => [ qw(meta) ];
10 extends "Reaction::InterfaceModel::Object";
14 # consider supporting slice, first, iterator, last etc.
15 # pager functionality should probably be a role
17 # IM objects don't have write methods because those are handled through actions,
18 # no support for write actions either unless someone makes a good case for it
19 # many models may not even be writable, so we cant make that assumption...
21 # I feel like we should hasa result_class or object_class ?
22 # having this here would remove a lot of PITA complexity from
23 # ObjectClass and SchemaClass when it comes to munging with internals
25 #Answer: No, because collections should be able to hold more than one type of object
27 # ALL IMPLEMENTATIONS ARE TO ILLUSTRATE POSSIBLE BEHAVIOR ONLY. DON'T CONSIDER
28 # THEM CORRECT, OR FINAL. JUST A ROUGH DRAFT.
30 #domain_models are 'ro' unless otherwise specified
31 has _collection_store => (
35 clearer => "_clear_collection_store",
36 metaclass => DomainModelAttribute,
39 has 'member_type' => (is => 'ro', isa => 'ClassName');
40 sub _build__collection_store { [] };
43 return @{ $self->_collection_store };
46 #return new member or it's index # ?
50 confess "Argument passed is not an object" unless blessed $new;
51 confess "Object Passed does not meet constraint isa Reaction::InterfaceModel::Object"
52 unless $new->isa('Reaction::InterfaceModel::Object');
53 my $store = $self->_collection_store;
55 return $#$store; #return index # of inserted item
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 };
71 return scalar @{ $self->_collection_store };
74 __PACKAGE__->meta->make_immutable;
81 Reaction::InterfaceModel::Collection - Generic collections of
82 L<Reaction::InterfaceModel::Object>s
86 The base class for C<InterfaceModel::Collection>s. The functionality implemented here
87 is minimal and it is expected that specialized collections be built by sublclassing
88 this and exploiting the roles system.
94 Returns a list containing all known members of the collection
96 =head2 add_member $object
98 Will add the object passed to the collection
100 =head2 remove_member $object
102 Removed the object passed from the collection, if present
106 Returns the number of objects in the collection.
110 =head2 _collection_store
112 Read-write & lazy_build. Holds the arrayref where the collection of objects is
113 presently stored. Has a clearer of C<_clear_collection_store> and a predicate of
114 C<_has_collection_store>.
116 =head1 PRIVATE METHODS
118 _build__collection_store
120 Builder method for attribute_collection_store, returns an empty arrayref
124 See L<Reaction::Class> for authors.
128 See L<Reaction::Class> for the license.