| 1 | package SQL::Engine::Collection; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | our $VERSION = '0.01'; # VERSION | ||
| 15 | |||
| 16 | # ATTRIBUTES | ||
| 17 | |||
| 18 | has items => ( | ||
| 19 | is => 'ro', | ||
| 20 | isa => 'ArrayRef[Object]', | ||
| 21 | new => 1 | ||
| 22 | ); | ||
| 23 | |||
| 24 | fun new_items($self) { | ||
| 25 | [] | ||
| 26 | } | ||
| 27 | |||
| 28 | # METHODS | ||
| 29 | |||
| 30 | method clear() { | ||
| 31 | |||
| 32 | return @{$self->items} = (); | ||
| 33 | } | ||
| 34 | |||
| 35 | method count() { | ||
| 36 | |||
| 37 | return scalar @{$self->items}; | ||
| 38 | } | ||
| 39 | |||
| 40 | method each(CodeRef $value) { | ||
| 41 | my $results = []; | ||
| 42 | |||
| 43 | for my $item ($self->list) { | ||
| 44 | push @$results, $value->($item); | ||
| 45 | } | ||
| 46 | |||
| 47 | return $results; | ||
| 48 | } | ||
| 49 | |||
| 50 | method first() { | ||
| 51 | |||
| 52 | return $self->items->[0]; | ||
| 53 | } | ||
| 54 | |||
| 55 | method last() { | ||
| 56 | |||
| 57 | return $self->items->[1]; | ||
| 58 | } | ||
| 59 | |||
| 60 | method list() { | ||
| 61 | |||
| 62 | return wantarray ? (@{$self->items}) : $self->items; | ||
| 63 | } | ||
| 64 | |||
| 65 | method pop() { | ||
| 66 | |||
| 67 | return CORE::pop @{$self->items}; | ||
| 68 | } | ||
| 69 | |||
| 70 | method pull() { | ||
| 71 | |||
| 72 | return shift @{$self->items}; | ||
| 73 | } | ||
| 74 | |||
| 75 | method push(Object @values) { | ||
| 76 | |||
| 77 | return CORE::push @{$self->items}, @values; | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | =encoding utf8 | ||
| 83 | |||
| 84 | =head1 NAME | ||
| 85 | |||
| 86 | SQL::Engine::Collection - Generic Object Container | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | =head1 ABSTRACT | ||
| 91 | |||
| 92 | Generic Object Container | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | =head1 SYNOPSIS | ||
| 97 | |||
| 98 | use SQL::Engine::Collection; | ||
| 99 | |||
| 100 | my $collection = SQL::Engine::Collection->new; | ||
| 101 | |||
| 102 | # $collection->count; | ||
| 103 | |||
| 104 | # 0 | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | =head1 DESCRIPTION | ||
| 109 | |||
| 110 | This package provides a generic container for working with sets of objects. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | =head1 LIBRARIES | ||
| 115 | |||
| 116 | This package uses type constraints from: | ||
| 117 | |||
| 118 | L<Types::Standard> | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | =head1 ATTRIBUTES | ||
| 123 | |||
| 124 | This package has the following attributes: | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | =head2 items | ||
| 129 | |||
| 130 | items(ArrayRef[Object]) | ||
| 131 | |||
| 132 | This attribute is read-only, accepts C<(ArrayRef[Object])> values, and is optional. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | =head1 METHODS | ||
| 137 | |||
| 138 | This package implements the following methods: | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | =head2 clear | ||
| 143 | |||
| 144 | clear() : Bool | ||
| 145 | |||
| 146 | The clear method clears the collection and returns an empty list. | ||
| 147 | |||
| 148 | =over 4 | ||
| 149 | |||
| 150 | =item clear example #1 | ||
| 151 | |||
| 152 | # given: synopsis | ||
| 153 | |||
| 154 | $collection->clear; | ||
| 155 | |||
| 156 | =back | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | =head2 count | ||
| 161 | |||
| 162 | count() : Int | ||
| 163 | |||
| 164 | The count method counts and returns the number of items in the collection. | ||
| 165 | |||
| 166 | =over 4 | ||
| 167 | |||
| 168 | =item count example #1 | ||
| 169 | |||
| 170 | # given: synopsis | ||
| 171 | |||
| 172 | $collection->count; | ||
| 173 | |||
| 174 | =back | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | =head2 each | ||
| 179 | |||
| 180 | each(CodeRef $value) : ArrayRef[Any] | ||
| 181 | |||
| 182 | The each method iterates through the collection executing the callback for each | ||
| 183 | item and returns the set of results. | ||
| 184 | |||
| 185 | =over 4 | ||
| 186 | |||
| 187 | =item each example #1 | ||
| 188 | |||
| 189 | # given: synopsis | ||
| 190 | |||
| 191 | $collection->each(sub { | ||
| 192 | my ($item) = shift; | ||
| 193 | |||
| 194 | $item | ||
| 195 | }); | ||
| 196 | |||
| 197 | =back | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | =head2 first | ||
| 202 | |||
| 203 | first() : Maybe[Object] | ||
| 204 | |||
| 205 | The first method returns the first item in the collection. | ||
| 206 | |||
| 207 | =over 4 | ||
| 208 | |||
| 209 | =item first example #1 | ||
| 210 | |||
| 211 | # given: synopsis | ||
| 212 | |||
| 213 | $collection->first; | ||
| 214 | |||
| 215 | =back | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | =head2 last | ||
| 220 | |||
| 221 | last() : Maybe[Object] | ||
| 222 | |||
| 223 | The last method returns the last item in the collection. | ||
| 224 | |||
| 225 | =over 4 | ||
| 226 | |||
| 227 | =item last example #1 | ||
| 228 | |||
| 229 | # given: synopsis | ||
| 230 | |||
| 231 | $collection->last; | ||
| 232 | |||
| 233 | =back | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | =head2 list | ||
| 238 | |||
| 239 | list() : ArrayRef | ||
| 240 | |||
| 241 | The list method returns the collection as a list of items. | ||
| 242 | |||
| 243 | =over 4 | ||
| 244 | |||
| 245 | =item list example #1 | ||
| 246 | |||
| 247 | # given: synopsis | ||
| 248 | |||
| 249 | $collection->list; | ||
| 250 | |||
| 251 | =back | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | =head2 pop | ||
| 256 | |||
| 257 | pop() : Maybe[Object] | ||
| 258 | |||
| 259 | The pop method removes and returns an item from the tail of the collection. | ||
| 260 | |||
| 261 | =over 4 | ||
| 262 | |||
| 263 | =item pop example #1 | ||
| 264 | |||
| 265 | # given: synopsis | ||
| 266 | |||
| 267 | $collection->pop; | ||
| 268 | |||
| 269 | =back | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | =head2 pull | ||
| 274 | |||
| 275 | pull() : Maybe[Object] | ||
| 276 | |||
| 277 | The pull method removes and returns an item from the head of the collection. | ||
| 278 | |||
| 279 | =over 4 | ||
| 280 | |||
| 281 | =item pull example #1 | ||
| 282 | |||
| 283 | # given: synopsis | ||
| 284 | |||
| 285 | $collection->pull; | ||
| 286 | |||
| 287 | =back | ||
| 288 | |||
| 289 | =cut | ||
| 290 | |||
| 291 | =head2 push | ||
| 292 | |||
| 293 | push(Object @values) : Int | ||
| 294 | |||
| 295 | The push method inserts an item onto the tail of the collection and returns the | ||
| 296 | count. | ||
| 297 | |||
| 298 | =over 4 | ||
| 299 | |||
| 300 | =item push example #1 | ||
| 301 | |||
| 302 | # given: synopsis | ||
| 303 | |||
| 304 | $collection->push(bless {}); | ||
| 305 | |||
| 306 | =back | ||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | =head1 AUTHOR | ||
| 311 | |||
| 312 | Al Newkirk, C<awncorp@cpan.org> | ||
| 313 | |||
| 314 | =head1 LICENSE | ||
| 315 | |||
| 316 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 317 | |||
| 318 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 319 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 320 | file"|https://github.com/iamalnewkirk/sql-engine/blob/master/LICENSE>. | ||
| 321 | |||
| 322 | =head1 PROJECT | ||
| 323 | |||
| 324 | L<Wiki|https://github.com/iamalnewkirk/sql-engine/wiki> | ||
| 325 | |||
| 326 | L<Project|https://github.com/iamalnewkirk/sql-engine> | ||
| 327 | |||
| 328 | L<Initiatives|https://github.com/iamalnewkirk/sql-engine/projects> | ||
| 329 | |||
| 330 | L<Milestones|https://github.com/iamalnewkirk/sql-engine/milestones> | ||
| 331 | |||
| 332 | L<Contributing|https://github.com/iamalnewkirk/sql-engine/blob/master/CONTRIBUTE.md> | ||
| 333 | |||
| 334 | L<Issues|https://github.com/iamalnewkirk/sql-engine/issues> | ||
| 335 | |||
| 336 | =cut |