| 1 | =head1 NAME | ||
| 2 | |||
| 3 | XAO::DO::FS::Collection - Collection class for XAO::FS | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | my $orders=$odb->collection(class => 'Data::Order'); | ||
| 8 | |||
| 9 | my $sr=$orders->search('placed_by', 'eq', 'user@host.name'); | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Collection class is similar to List object in the sense that it contains | ||
| 14 | Hash objects joined by some criteria. | ||
| 15 | |||
| 16 | All Collection objects are read-only, you can use them to search for | ||
| 17 | data and to get data objects from them but not to store. | ||
| 18 | |||
| 19 | Methods are (alphabetically): | ||
| 20 | |||
| 21 | =over | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | ############################################################################### | ||
| 26 | package XAO::DO::FS::Collection; | ||
| 27 | use strict; | ||
| 28 | use XAO::Utils; | ||
| 29 | use XAO::Objects; | ||
| 30 | |||
| 31 | use base XAO::Objects->load(objname => 'FS::Glue'); | ||
| 32 | |||
| 33 | use vars qw($VERSION); | ||
| 34 | $VERSION=(0+sprintf('%u.%03u',(q$Id: Collection.pm,v 2.1 2005/01/14 00:23:54 am Exp $ =~ /\s(\d+)\.(\d+)\s/))) || die "Bad VERSION"; | ||
| 35 | |||
| 36 | ############################################################################### | ||
| 37 | |||
| 38 | =item container_key () | ||
| 39 | |||
| 40 | Makes no sense for Collection, will throw an error. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub container_key () { | ||
| 45 | my $self=shift; | ||
| 46 | $self->throw("container_key() - makes no sense on Collection object"); | ||
| 47 | } | ||
| 48 | |||
| 49 | ############################################################################### | ||
| 50 | |||
| 51 | =item delete ($) | ||
| 52 | |||
| 53 | Makes no sense for Collection, will throw an error. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub delete () { | ||
| 58 | my $self=shift; | ||
| 59 | $self->throw("delete() - makes no sense on Collection object"); | ||
| 60 | } | ||
| 61 | |||
| 62 | ############################################################################### | ||
| 63 | |||
| 64 | =item describe () { | ||
| 65 | |||
| 66 | Describes itself, returns a hash reference with at least the following | ||
| 67 | elements: | ||
| 68 | |||
| 69 | type => 'collection' | ||
| 70 | class => class name | ||
| 71 | key => key name | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub describe ($;$) { | ||
| 76 | my $self=shift; | ||
| 77 | return { | ||
| 78 | type => @_ ? 'hash' : 'collection', | ||
| 79 | class => $$self->{class_name}, | ||
| 80 | key => $$self->{key_name}, | ||
| 81 | }; | ||
| 82 | } | ||
| 83 | |||
| 84 | ############################################################################### | ||
| 85 | |||
| 86 | =item detach () | ||
| 87 | |||
| 88 | Not implemented, but safe to use. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub detach ($) { | ||
| 93 | } | ||
| 94 | |||
| 95 | ############################################################################### | ||
| 96 | |||
| 97 | =item exists ($) | ||
| 98 | |||
| 99 | Checks if an object with the given name exists in the collection and | ||
| 100 | returns boolean value. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub exists ($$) { | ||
| 105 | my $self=shift; | ||
| 106 | my $name=shift; | ||
| 107 | $self->_collection_exists($name); | ||
| 108 | } | ||
| 109 | |||
| 110 | ############################################################################### | ||
| 111 | |||
| 112 | =item get (@) | ||
| 113 | |||
| 114 | Retrieves a Hash object from the Collection using the given name. | ||
| 115 | |||
| 116 | As a convenience you can pass more then one object name to the get() | ||
| 117 | method to retrieve multiple Hash references at once. | ||
| 118 | |||
| 119 | If an object does not exist an error will be thrown, use exists() method | ||
| 120 | to check if you really need to. | ||
| 121 | |||
| 122 | Note: It does not check if the object still exists in the database! If | ||
| 123 | you need to be sure that the object does exist use | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub get ($$) { | ||
| 128 | my $self=shift; | ||
| 129 | |||
| 130 | $self->throw("get - at least one ID required") unless @_; | ||
| 131 | |||
| 132 | my @results=map { | ||
| 133 | $_ || | ||
| 134 | throw $self "get - no object ID given"; | ||
| 135 | ref($_) && | ||
| 136 | throw $self "get - should be a scalar, not a ".ref($_)." reference"; | ||
| 137 | XAO::Objects->new( | ||
| 138 | objname => $$self->{class_name}, | ||
| 139 | glue => $self->_glue, | ||
| 140 | unique_id => $_, | ||
| 141 | key_name => $$self->{key_name}, | ||
| 142 | list_base_name => $$self->{base_name}, | ||
| 143 | ); | ||
| 144 | } @_; | ||
| 145 | |||
| 146 | @_==1 ? $results[0] : @results; | ||
| 147 | } | ||
| 148 | |||
| 149 | ############################################################################### | ||
| 150 | |||
| 151 | =item get_new () | ||
| 152 | |||
| 153 | Convenience method that returns new empty detached object of the type | ||
| 154 | that collection operates on. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub get_new ($) { | ||
| 159 | my $self=shift; | ||
| 160 | $self->glue->new(objname => $$self->{class_name}); | ||
| 161 | } | ||
| 162 | |||
| 163 | ############################################################################### | ||
| 164 | |||
| 165 | =item glue () | ||
| 166 | |||
| 167 | Returns the Glue object which was used to retrieve the current object | ||
| 168 | from. | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | # Implemented in Glue | ||
| 173 | |||
| 174 | ############################################################################### | ||
| 175 | |||
| 176 | =item keys () | ||
| 177 | |||
| 178 | Returns unsorted list of all keys for all objects stored in that list. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | sub keys ($) { | ||
| 183 | my $self=shift; | ||
| 184 | |||
| 185 | @{$self->_collection_keys()}; | ||
| 186 | } | ||
| 187 | |||
| 188 | ############################################################################### | ||
| 189 | |||
| 190 | =item new (%) | ||
| 191 | |||
| 192 | You cannot use this method directly. Use collection() method on Glue to | ||
| 193 | get a collection reference. Example: | ||
| 194 | |||
| 195 | my $orders=$odb->collection(class => 'Data::Order'); | ||
| 196 | |||
| 197 | Currently the only supported type of collection is by class name, a | ||
| 198 | collection that joins together all Hashes of the same class. | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub new ($%) { | ||
| 203 | my $class=shift; | ||
| 204 | my $self=$class->SUPER::new(@_); | ||
| 205 | |||
| 206 | my $args=get_args(\@_); | ||
| 207 | $$self->{class_name}=$args->{class} || $args->{class_name}; | ||
| 208 | |||
| 209 | $self->_collection_setup(); | ||
| 210 | |||
| 211 | $self; | ||
| 212 | } | ||
| 213 | |||
| 214 | ############################################################################### | ||
| 215 | |||
| 216 | =item objtype () | ||
| 217 | |||
| 218 | For all Collection objects always return a string 'Collection'. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub objtype ($) { | ||
| 223 | 'Collection'; | ||
| 224 | } | ||
| 225 | |||
| 226 | ############################################################################### | ||
| 227 | |||
| 228 | =item put ($;$) | ||
| 229 | |||
| 230 | Makes no sense on collections. Will throw an error. | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub put ($$;$) { | ||
| 235 | my $self=shift; | ||
| 236 | $self->throw("put - you can't store into collections"); | ||
| 237 | } | ||
| 238 | |||
| 239 | ############################################################################### | ||
| 240 | |||
| 241 | =item search (@) | ||
| 242 | |||
| 243 | Supports the same syntax as List's search() method. See | ||
| 244 | L<XAO::DO::FS::List> for reference. | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub search ($@) { | ||
| 249 | my $self=shift; | ||
| 250 | $self->_list_search(@_); | ||
| 251 | } | ||
| 252 | |||
| 253 | ############################################################################### | ||
| 254 | |||
| 255 | =item values () | ||
| 256 | |||
| 257 | Returns a list of all Hash objects in the list. | ||
| 258 | |||
| 259 | B<Note:> the order of values is the same as the order of keys returned | ||
| 260 | by keys() method. At least until you modify the object directly on | ||
| 261 | indirectly. It is not recommended to use values() method for the reason | ||
| 262 | of pure predictability. | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | # implemented in Glue.pm | ||
| 267 | |||
| 268 | ############################################################################### | ||
| 269 | |||
| 270 | =item uri ($) | ||
| 271 | |||
| 272 | Makes no sense on collections, will throw an error. | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | sub uri () { | ||
| 277 | my $self=shift; | ||
| 278 | $self->throw("uri - makes no sense on collections"); | ||
| 279 | } | ||
| 280 | |||
| 281 | ############################################################################### | ||
| 282 | 1; | ||
| 283 | __END__ |