Commit | Line | Data |
7adfd53f |
1 | package Reaction::InterfaceModel::Collection; |
2 | |
f670cfd0 |
3 | use Reaction::Class; |
7adfd53f |
4 | use Scalar::Util qw/refaddr blessed/; |
f670cfd0 |
5 | use aliased 'Reaction::Meta::InterfaceModel::Object::DomainModelAttribute'; |
7adfd53f |
6 | |
7 | # WARNING - DANGER: this is just an RFC, please DO NOT USE YET |
8 | |
f670cfd0 |
9 | class Collection is "Reaction::InterfaceModel::Object", which { |
7adfd53f |
10 | |
11 | # consider supporting slice, first, iterator, last etc. |
12 | # pager functionality should probably be a role |
13 | |
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... |
17 | |
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 |
21 | |
22 | #Answer: No, because collections should be able to hold more than one type of object |
23 | |
24 | # ALL IMPLEMENTATIONS ARE TO ILLUSTRATE POSSIBLE BEHAVIOR ONLY. DON'T CONSIDER |
25 | # THEM CORRECT, OR FINAL. JUST A ROUGH DRAFT. |
26 | |
27 | #domain_models are 'ro' unless otherwise specified |
f670cfd0 |
28 | has _collection_store => ( |
29 | is => 'rw', |
30 | isa => 'ArrayRef', |
31 | lazy_build => 1, |
32 | clearer => "_clear_collection_store", |
33 | metaclass => DomainModelAttribute, |
34 | ); |
7adfd53f |
35 | |
c8fbb8ad |
36 | has 'member_type' => (is => 'ro', isa => 'ClassName'); |
37 | |
89939ff9 |
38 | implements _build__collection_store => as { [] }; |
7adfd53f |
39 | |
40 | implements members => as { |
41 | my $self = shift; |
42 | return @{ $self->_collection_store }; |
43 | }; |
44 | |
45 | #return new member or it's index # ? |
46 | implements add_member => as { |
47 | my $self = shift; |
48 | my $new = shift; |
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; |
53 | push @$store, $new; |
54 | return $#$store; #return index # of inserted item |
55 | }; |
56 | |
57 | implements remove_member => as { |
58 | my $self = shift; |
59 | my $rem = shift; |
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'); |
63 | |
64 | my $addr = refaddr $rem; |
65 | @{ $self->_collection_store } = grep {$addr ne refaddr $_ } @{ $self->_store }; |
66 | }; |
67 | |
68 | #that was easy.. |
69 | implements count_members => sub{ |
70 | my $self = shift; |
71 | return scalar @{ $self->_collection_store }; |
72 | }; |
73 | |
74 | }; |
75 | |
76 | 1; |
77 | |
78 | =head1 NAME |
79 | |
80 | Reaction::InterfaceModel::Collection - Generic collections of |
b8faba69 |
81 | L<Reaction::InterfaceModel::Object>s |
7adfd53f |
82 | |
83 | =head1 DESCRIPTION |
84 | |
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. |
88 | |
89 | =head1 METHODS |
90 | |
91 | =head2 members |
92 | |
93 | Returns a list containing all known members of the collection |
94 | |
95 | =head2 add_member $object |
96 | |
97 | Will add the object passed to the collection |
98 | |
99 | =head2 remove_member $object |
100 | |
101 | Removed the object passed from the collection, if present |
102 | |
103 | =head2 count_members |
104 | |
105 | Returns the number of objects in the collection. |
106 | |
107 | =head1 ATTRIBUTES |
108 | |
109 | =head2 _collection_store |
110 | |
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>. |
114 | |
115 | =head1 PRIVATE METHODS |
116 | |
89939ff9 |
117 | _build__collection_store |
7adfd53f |
118 | |
119 | Builder method for attribute_collection_store, returns an empty arrayref |
120 | |
121 | =head1 AUTHORS |
122 | |
123 | See L<Reaction::Class> for authors. |
124 | |
125 | =head1 LICENSE |
126 | |
127 | See L<Reaction::Class> for the license. |
128 | |
129 | =cut |