| 1 | package Zing::Domain; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry 'Zing::Types'; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | extends 'Zing::Channel'; | ||
| 15 | |||
| 16 | use Scalar::Util (); | ||
| 17 | |||
| 18 | our $VERSION = '0.26'; # VERSION | ||
| 19 | |||
| 20 | # ATTRIBUTES | ||
| 21 | |||
| 22 | has 'metadata' => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => 'HashRef', | ||
| 25 | new => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | fun new_metadata($self) { | ||
| 29 | {} | ||
| 30 | } | ||
| 31 | |||
| 32 | has 'snapshots' => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => 'Bool', | ||
| 35 | def => 0, | ||
| 36 | ); | ||
| 37 | |||
| 38 | # BUILDERS | ||
| 39 | |||
| 40 | fun BUILD($self) { | ||
| 41 | if ($self->snapshots) { | ||
| 42 | $self->{position} = $self->size; | ||
| 43 | $self->{position}-- if $self->{position}; | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | $self->reset; | ||
| 47 | $self->state; | ||
| 48 | } | ||
| 49 | |||
| 50 | return !$self->isa('Zing::Lookup') ? $self->apply : $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | # SHIMS | ||
| 54 | |||
| 55 | sub _copy { | ||
| 56 | my ($data) = @_; | ||
| 57 | |||
| 58 | if (!defined $data) { | ||
| 59 | return undef; | ||
| 60 | } | ||
| 61 | elsif (ref $data eq 'ARRAY') { | ||
| 62 | my $copy = []; | ||
| 63 | for (my $i = 0; $i < @$data; $i++) { | ||
| 64 | $copy->[$i] = _copy($data->[$i]); | ||
| 65 | } | ||
| 66 | return $copy; | ||
| 67 | } | ||
| 68 | elsif (ref $data eq 'HASH') { | ||
| 69 | my $copy = {}; | ||
| 70 | for my $key (keys %$data) { | ||
| 71 | $copy->{$key} = _copy($data->{$key}); | ||
| 72 | } | ||
| 73 | return $copy; | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | return $data; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _merge { | ||
| 81 | my ($a_data, $b_data) = @_; | ||
| 82 | |||
| 83 | return $a_data unless defined $b_data; | ||
| 84 | |||
| 85 | for my $key (keys %$b_data) { | ||
| 86 | my ($x, $y) = map { ref $_->{$key} eq 'HASH' } $b_data, $a_data; | ||
| 87 | |||
| 88 | if ($x and $y) { | ||
| 89 | $a_data->{$key} = _merge($a_data->{$key}, $b_data->{$key}); | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | $a_data->{$key} = $b_data->{$key}; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | return $a_data; | ||
| 97 | } | ||
| 98 | |||
| 99 | # METHODS | ||
| 100 | |||
| 101 | method apply() { | ||
| 102 | undef $self->{state} if $self->renew; | ||
| 103 | |||
| 104 | while (my $data = $self->recv) { | ||
| 105 | my $op = $data->{op}; | ||
| 106 | my $key = $data->{key}; | ||
| 107 | my $val = $data->{value}; | ||
| 108 | |||
| 109 | if ((not defined $self->{state}) && %{$data->{snapshot}}) { | ||
| 110 | $self->restore($data); | ||
| 111 | } | ||
| 112 | |||
| 113 | local $@; | ||
| 114 | |||
| 115 | if ($op eq 'decr') { | ||
| 116 | eval{($self->state->{$key} //= 0) -= ($val->[0] // 0)}; | ||
| 117 | } | ||
| 118 | elsif ($op eq 'del') { | ||
| 119 | eval{CORE::delete $self->state->{$key}}; | ||
| 120 | } | ||
| 121 | elsif ($op eq 'incr') { | ||
| 122 | eval{($self->state->{$key} //= 0 ) += ($val->[0] // 0)}; | ||
| 123 | } | ||
| 124 | elsif ($op eq 'merge') { | ||
| 125 | eval{_merge(($self->state->{$key} //= {}), $val->[0])}; | ||
| 126 | } | ||
| 127 | elsif ($op eq 'pop') { | ||
| 128 | eval{CORE::pop @{$self->state->{$key}}}; | ||
| 129 | } | ||
| 130 | elsif ($op eq 'push') { | ||
| 131 | eval{CORE::push @{$self->state->{$key}}, @$val}; | ||
| 132 | } | ||
| 133 | elsif ($op eq 'set') { | ||
| 134 | eval{$self->state->{$key} = $val->[0]}; | ||
| 135 | } | ||
| 136 | elsif ($op eq 'shift') { | ||
| 137 | eval{CORE::shift @{$self->state->{$key}}}; | ||
| 138 | } | ||
| 139 | elsif ($op eq 'unshift') { | ||
| 140 | eval{CORE::unshift @{$self->state->{$key}}, @$val}; | ||
| 141 | } | ||
| 142 | |||
| 143 | if (%{$data->{metadata}}) { | ||
| 144 | $self->{metadata} = _copy($data->{metadata}); | ||
| 145 | } | ||
| 146 | |||
| 147 | $self->emit($key, $data); | ||
| 148 | } | ||
| 149 | |||
| 150 | return $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | method change(Str $op, Str $key, Any @value) { | ||
| 154 | my %fields = ( | ||
| 155 | key => $key, | ||
| 156 | metadata => $self->metadata, | ||
| 157 | snapshot => $self->snapshot, | ||
| 158 | time => time, | ||
| 159 | value => [@value], | ||
| 160 | ); | ||
| 161 | |||
| 162 | if ($op eq 'decr') { | ||
| 163 | $self->send({ %fields, op => 'decr' }); | ||
| 164 | } | ||
| 165 | elsif ($op eq 'del') { | ||
| 166 | $self->send({ %fields, op => 'del' }); | ||
| 167 | } | ||
| 168 | elsif ($op eq 'incr') { | ||
| 169 | $self->send({ %fields, op => 'incr' }); | ||
| 170 | } | ||
| 171 | elsif ($op eq 'merge') { | ||
| 172 | $self->send({ %fields, op => 'merge' }); | ||
| 173 | } | ||
| 174 | elsif ($op eq 'pop') { | ||
| 175 | $self->send({ %fields, op => 'pop' }); | ||
| 176 | } | ||
| 177 | elsif ($op eq 'push') { | ||
| 178 | $self->send({ %fields, op => 'push' }); | ||
| 179 | } | ||
| 180 | elsif ($op eq 'set') { | ||
| 181 | $self->send({ %fields, op => 'set' }); | ||
| 182 | } | ||
| 183 | elsif ($op eq 'set') { | ||
| 184 | $self->send({ %fields, op => 'set' }); | ||
| 185 | } | ||
| 186 | elsif ($op eq 'shift') { | ||
| 187 | $self->send({ %fields, op => 'shift' }); | ||
| 188 | } | ||
| 189 | elsif ($op eq 'unshift') { | ||
| 190 | $self->send({ %fields, op => 'unshift' }); | ||
| 191 | } | ||
| 192 | |||
| 193 | return $self->apply; | ||
| 194 | } | ||
| 195 | |||
| 196 | method decr(Str $key, Int $value = 1) { | ||
| 197 | return $self->apply->change('decr', $key, $value); | ||
| 198 | } | ||
| 199 | |||
| 200 | method del(Str $key) { | ||
| 201 | return $self->apply->change('del', $key); | ||
| 202 | } | ||
| 203 | |||
| 204 | method emit(Str $key, HashRef $data) { | ||
| 205 | my $handlers = $self->handlers->{$key}; | ||
| 206 | |||
| 207 | return $self if !$handlers; | ||
| 208 | |||
| 209 | for my $handler (@$handlers) { | ||
| 210 | $handler->[1]->($self, $data); | ||
| 211 | } | ||
| 212 | |||
| 213 | return $self; | ||
| 214 | } | ||
| 215 | |||
| 216 | method get(Str $key) { | ||
| 217 | return $self->apply->state->{$key}; | ||
| 218 | } | ||
| 219 | |||
| 220 | method handlers() { | ||
| 221 | return $self->{handlers} ||= {}; | ||
| 222 | } | ||
| 223 | |||
| 224 | method ignore(Str $key, Maybe[CodeRef] $sub) { | ||
| 225 | return $self if !$self->handlers->{$key}; | ||
| 226 | |||
| 227 | return do { delete $self->handlers->{$key}; $self } if !$sub; | ||
| 228 | |||
| 229 | my $ref = Scalar::Util::refaddr($sub); | ||
| 230 | |||
| 231 | @{$self->handlers->{$key}} = grep {$ref ne $$_[0]} @{$self->handlers->{$key}}; | ||
| 232 | |||
| 233 | delete $self->handlers->{$key} if !@{$self->handlers->{$key}}; | ||
| 234 | |||
| 235 | return $self; | ||
| 236 | } | ||
| 237 | |||
| 238 | method incr(Str $key, Int $value = 1) { | ||
| 239 | return $self->apply->change('incr', $key, $value); | ||
| 240 | } | ||
| 241 | |||
| 242 | method merge(Str $key, HashRef $value) { | ||
| 243 | return $self->apply->change('merge', $key, $value); | ||
| 244 | } | ||
| 245 | |||
| 246 | method pop(Str $key) { | ||
| 247 | return $self->apply->change('pop', $key); | ||
| 248 | } | ||
| 249 | |||
| 250 | method push(Str $key, Any @value) { | ||
| 251 | return $self->apply->change('push', $key, @value); | ||
| 252 | } | ||
| 253 | |||
| 254 | method restore(HashRef $data) { | ||
| 255 | return $self->{state} = _copy($data->{snapshot}); | ||
| 256 | } | ||
| 257 | |||
| 258 | method set(Str $key, Any $value) { | ||
| 259 | return $self->apply->change('set', $key, $value); | ||
| 260 | } | ||
| 261 | |||
| 262 | method shift(Str $key) { | ||
| 263 | return $self->apply->change('shift', $key); | ||
| 264 | } | ||
| 265 | |||
| 266 | method snapshot() { | ||
| 267 | return $self->snapshots ? _copy($self->state) : {}; | ||
| 268 | } | ||
| 269 | |||
| 270 | method state() { | ||
| 271 | return $self->{state} ||= {}; | ||
| 272 | } | ||
| 273 | |||
| 274 | method listen(Str $key, CodeRef $sub) { | ||
| 275 | my $ref = Scalar::Util::refaddr($sub); | ||
| 276 | |||
| 277 | push @{$self->ignore($key, $sub)->handlers->{$key}}, [$ref, $sub]; | ||
| 278 | |||
| 279 | return $self; | ||
| 280 | } | ||
| 281 | |||
| 282 | method term() { | ||
| 283 | return $self->app->term($self)->domain; | ||
| 284 | } | ||
| 285 | |||
| 286 | method unshift(Str $key, Any @value) { | ||
| 287 | return $self->apply->change('unshift', $key, @value); | ||
| 288 | } | ||
| 289 | |||
| 290 | 1; | ||
| 291 | |||
| 292 | =encoding utf8 | ||
| 293 | |||
| 294 | =head1 NAME | ||
| 295 | |||
| 296 | Zing::Domain - Shared State Management | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | =head1 ABSTRACT | ||
| 301 | |||
| 302 | Shared State Management Construct | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | =head1 SYNOPSIS | ||
| 307 | |||
| 308 | use Zing::Domain; | ||
| 309 | |||
| 310 | my $domain = Zing::Domain->new(name => 'user-1'); | ||
| 311 | |||
| 312 | # $domain->recv; | ||
| 313 | |||
| 314 | =cut | ||
| 315 | |||
| 316 | =head1 DESCRIPTION | ||
| 317 | |||
| 318 | This package provides an aggregate abstraction and real-time cross-process | ||
| 319 | sharable data structure which offers many benefits, not least being able to see | ||
| 320 | a full history of state changes. | ||
| 321 | |||
| 322 | =cut | ||
| 323 | |||
| 324 | =head1 INHERITS | ||
| 325 | |||
| 326 | This package inherits behaviors from: | ||
| 327 | |||
| 328 | L<Zing::Channel> | ||
| 329 | |||
| 330 | =cut | ||
| 331 | |||
| 332 | =head1 LIBRARIES | ||
| 333 | |||
| 334 | This package uses type constraints from: | ||
| 335 | |||
| 336 | L<Zing::Types> | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | =head1 ATTRIBUTES | ||
| 341 | |||
| 342 | This package has the following attributes: | ||
| 343 | |||
| 344 | =cut | ||
| 345 | |||
| 346 | =head2 metadata | ||
| 347 | |||
| 348 | metadata(HashRef) | ||
| 349 | |||
| 350 | This attribute is read-only, accepts C<(HashRef)> values, and is optional. | ||
| 351 | |||
| 352 | =cut | ||
| 353 | |||
| 354 | =head1 METHODS | ||
| 355 | |||
| 356 | This package implements the following methods: | ||
| 357 | |||
| 358 | =cut | ||
| 359 | |||
| 360 | =head2 apply | ||
| 361 | |||
| 362 | apply() : Object | ||
| 363 | |||
| 364 | The apply method receives events from the channel and applies the operations. | ||
| 365 | |||
| 366 | =over 4 | ||
| 367 | |||
| 368 | =item apply example #1 | ||
| 369 | |||
| 370 | # given: synopsis | ||
| 371 | |||
| 372 | $domain->apply; | ||
| 373 | |||
| 374 | =back | ||
| 375 | |||
| 376 | =cut | ||
| 377 | |||
| 378 | =head2 change | ||
| 379 | |||
| 380 | change(Str $op, Str $key, Any @val) : Object | ||
| 381 | |||
| 382 | The change method commits an operation (and snapshot) to the channel. This | ||
| 383 | method is used internally and shouldn't need to be called directly. | ||
| 384 | |||
| 385 | =over 4 | ||
| 386 | |||
| 387 | =item change example #1 | ||
| 388 | |||
| 389 | # given: synopsis | ||
| 390 | |||
| 391 | $domain->change('incr', 'karma', 1); | ||
| 392 | |||
| 393 | =back | ||
| 394 | |||
| 395 | =cut | ||
| 396 | |||
| 397 | =head2 decr | ||
| 398 | |||
| 399 | decr(Str $key, Int $val = 1) : Object | ||
| 400 | |||
| 401 | The decr method decrements the data associated with a specific key. | ||
| 402 | |||
| 403 | =over 4 | ||
| 404 | |||
| 405 | =item decr example #1 | ||
| 406 | |||
| 407 | # given: synopsis | ||
| 408 | |||
| 409 | $domain->decr('karma'); | ||
| 410 | |||
| 411 | =back | ||
| 412 | |||
| 413 | =over 4 | ||
| 414 | |||
| 415 | =item decr example #2 | ||
| 416 | |||
| 417 | # given: synopsis | ||
| 418 | |||
| 419 | $domain->decr('karma', 2); | ||
| 420 | |||
| 421 | =back | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | =head2 del | ||
| 426 | |||
| 427 | del(Str $key) : Object | ||
| 428 | |||
| 429 | The del method deletes the data associated with a specific key. | ||
| 430 | |||
| 431 | =over 4 | ||
| 432 | |||
| 433 | =item del example #1 | ||
| 434 | |||
| 435 | # given: synopsis | ||
| 436 | |||
| 437 | $domain->del('missing'); | ||
| 438 | |||
| 439 | =back | ||
| 440 | |||
| 441 | =over 4 | ||
| 442 | |||
| 443 | =item del example #2 | ||
| 444 | |||
| 445 | # given: synopsis | ||
| 446 | |||
| 447 | $domain->set('email', 'me@example.com'); | ||
| 448 | |||
| 449 | $domain->del('email'); | ||
| 450 | |||
| 451 | =back | ||
| 452 | |||
| 453 | =cut | ||
| 454 | |||
| 455 | =head2 emit | ||
| 456 | |||
| 457 | emit(Str $key, HashRef $data) : Object | ||
| 458 | |||
| 459 | The emit method executes any callbacks registered using the L</listen> method | ||
| 460 | associated with a specific key. | ||
| 461 | |||
| 462 | =over 4 | ||
| 463 | |||
| 464 | =item emit example #1 | ||
| 465 | |||
| 466 | # given: synopsis | ||
| 467 | |||
| 468 | $domain->emit('email', { val => ['me@example.com'] }); | ||
| 469 | |||
| 470 | =back | ||
| 471 | |||
| 472 | =over 4 | ||
| 473 | |||
| 474 | =item emit example #2 | ||
| 475 | |||
| 476 | # given: synopsis | ||
| 477 | |||
| 478 | $domain->listen('email', sub { my ($self, $data) = @_; $self->{event} = $data; }); | ||
| 479 | |||
| 480 | $domain->emit('email', { val => ['me@example.com'] }); | ||
| 481 | |||
| 482 | =back | ||
| 483 | |||
| 484 | =cut | ||
| 485 | |||
| 486 | =head2 get | ||
| 487 | |||
| 488 | get(Str $key) : Any | ||
| 489 | |||
| 490 | The get method return the data associated with a specific key. | ||
| 491 | |||
| 492 | =over 4 | ||
| 493 | |||
| 494 | =item get example #1 | ||
| 495 | |||
| 496 | # given: synopsis | ||
| 497 | |||
| 498 | $domain->get('email'); | ||
| 499 | |||
| 500 | =back | ||
| 501 | |||
| 502 | =over 4 | ||
| 503 | |||
| 504 | =item get example #2 | ||
| 505 | |||
| 506 | # given: synopsis | ||
| 507 | |||
| 508 | $domain->set('email', 'me@example.com'); | ||
| 509 | |||
| 510 | $domain->get('email'); | ||
| 511 | |||
| 512 | =back | ||
| 513 | |||
| 514 | =cut | ||
| 515 | |||
| 516 | =head2 ignore | ||
| 517 | |||
| 518 | ignore(Str $key, Maybe[CodeRef] $sub) : Any | ||
| 519 | |||
| 520 | The ignore method removes the callback specified by the L</listen>, or all | ||
| 521 | callbacks associated with a specific key if no specific callback if provided. | ||
| 522 | |||
| 523 | =over 4 | ||
| 524 | |||
| 525 | =item ignore example #1 | ||
| 526 | |||
| 527 | # given: synopsis | ||
| 528 | |||
| 529 | $domain->ignore('email'); | ||
| 530 | |||
| 531 | =back | ||
| 532 | |||
| 533 | =over 4 | ||
| 534 | |||
| 535 | =item ignore example #2 | ||
| 536 | |||
| 537 | # given: synopsis | ||
| 538 | |||
| 539 | my $callback = sub { my ($self, $data) = @_; $self->{event} = $data; }; | ||
| 540 | |||
| 541 | $domain->listen('email', $callback); | ||
| 542 | |||
| 543 | $domain->ignore('email', $callback); | ||
| 544 | |||
| 545 | =back | ||
| 546 | |||
| 547 | =over 4 | ||
| 548 | |||
| 549 | =item ignore example #3 | ||
| 550 | |||
| 551 | # given: synopsis | ||
| 552 | |||
| 553 | my $callback_1 = sub { my ($self, $data) = @_; $self->{event} = [$data, 2]; }; | ||
| 554 | |||
| 555 | $domain->listen('email', $callback_1); | ||
| 556 | |||
| 557 | my $callback_2 = sub { my ($self, $data) = @_; $self->{event} = [$data, 1]; }; | ||
| 558 | |||
| 559 | $domain->listen('email', $callback_2); | ||
| 560 | |||
| 561 | $domain->ignore('email', $callback_1); | ||
| 562 | |||
| 563 | =back | ||
| 564 | |||
| 565 | =over 4 | ||
| 566 | |||
| 567 | =item ignore example #4 | ||
| 568 | |||
| 569 | # given: synopsis | ||
| 570 | |||
| 571 | my $callback_1 = sub { my ($self, $data) = @_; $self->{event} = [$data, 1]; }; | ||
| 572 | |||
| 573 | $domain->listen('email', $callback_1); | ||
| 574 | |||
| 575 | my $callback_2 = sub { my ($self, $data) = @_; $self->{event} = [$data, 2]; }; | ||
| 576 | |||
| 577 | $domain->listen('email', $callback_2); | ||
| 578 | |||
| 579 | $domain->ignore('email'); | ||
| 580 | |||
| 581 | =back | ||
| 582 | |||
| 583 | =cut | ||
| 584 | |||
| 585 | =head2 incr | ||
| 586 | |||
| 587 | incr(Str $key, Int $val = 1) : Object | ||
| 588 | |||
| 589 | The incr method increments the data associated with a specific key. | ||
| 590 | |||
| 591 | =over 4 | ||
| 592 | |||
| 593 | =item incr example #1 | ||
| 594 | |||
| 595 | # given: synopsis | ||
| 596 | |||
| 597 | $domain->incr('karma'); | ||
| 598 | |||
| 599 | =back | ||
| 600 | |||
| 601 | =over 4 | ||
| 602 | |||
| 603 | =item incr example #2 | ||
| 604 | |||
| 605 | # given: synopsis | ||
| 606 | |||
| 607 | $domain->incr('karma', 5); | ||
| 608 | |||
| 609 | =back | ||
| 610 | |||
| 611 | =cut | ||
| 612 | |||
| 613 | =head2 listen | ||
| 614 | |||
| 615 | listen(Str $key, CodeRef $sub) : Object | ||
| 616 | |||
| 617 | The listen method registers callbacks associated with a specific key which | ||
| 618 | will be invoked by the L</emit> method or whenever an event matching the key | ||
| 619 | specified is received and applied. | ||
| 620 | |||
| 621 | =over 4 | ||
| 622 | |||
| 623 | =item listen example #1 | ||
| 624 | |||
| 625 | # given: synopsis | ||
| 626 | |||
| 627 | $domain->ignore('email'); | ||
| 628 | |||
| 629 | $domain->listen('email', sub { my ($self, $data) = @_; $self->{event} = $data; }); | ||
| 630 | |||
| 631 | =back | ||
| 632 | |||
| 633 | =over 4 | ||
| 634 | |||
| 635 | =item listen example #2 | ||
| 636 | |||
| 637 | # given: synopsis | ||
| 638 | |||
| 639 | $domain->ignore('email'); | ||
| 640 | |||
| 641 | my $callback = sub { my ($self, $data) = @_; $self->{event} = $data; }; | ||
| 642 | |||
| 643 | $domain->listen('email', $callback); | ||
| 644 | |||
| 645 | $domain->listen('email', $callback); | ||
| 646 | |||
| 647 | =back | ||
| 648 | |||
| 649 | =over 4 | ||
| 650 | |||
| 651 | =item listen example #3 | ||
| 652 | |||
| 653 | # given: synopsis | ||
| 654 | |||
| 655 | $domain->ignore('email'); | ||
| 656 | |||
| 657 | my $callback_1 = sub { my ($self, $data) = @_; $self->{event} = [$data, 1]; }; | ||
| 658 | |||
| 659 | $domain->listen('email', $callback_1); | ||
| 660 | |||
| 661 | my $callback_2 = sub { my ($self, $data) = @_; $self->{event} = [$data, 2]; }; | ||
| 662 | |||
| 663 | $domain->listen('email', $callback_2); | ||
| 664 | |||
| 665 | =back | ||
| 666 | |||
| 667 | =cut | ||
| 668 | |||
| 669 | =head2 merge | ||
| 670 | |||
| 671 | merge(Str $key, HashRef $val) : Object | ||
| 672 | |||
| 673 | The merge method commits the data associated with a specific key to the channel | ||
| 674 | as a partial to be merged into any existing data. | ||
| 675 | |||
| 676 | =over 4 | ||
| 677 | |||
| 678 | =item merge example #1 | ||
| 679 | |||
| 680 | # given: synopsis | ||
| 681 | |||
| 682 | $domain->merge(data => { email => 'me@example.com', username => 'me' }); | ||
| 683 | |||
| 684 | $domain->merge(data => { email => 'we@example.com' }); | ||
| 685 | |||
| 686 | =back | ||
| 687 | |||
| 688 | =over 4 | ||
| 689 | |||
| 690 | =item merge example #2 | ||
| 691 | |||
| 692 | # given: synopsis | ||
| 693 | |||
| 694 | $domain->set(data => { username => 'we' }); | ||
| 695 | |||
| 696 | $domain->merge(data => { email => 'me@example.com', username => 'me' }); | ||
| 697 | |||
| 698 | $domain->merge(data => { email => 'we@example.com' }); | ||
| 699 | |||
| 700 | =back | ||
| 701 | |||
| 702 | =over 4 | ||
| 703 | |||
| 704 | =item merge example #3 | ||
| 705 | |||
| 706 | # given: synopsis | ||
| 707 | |||
| 708 | $domain->set(data => { username => 'we', colors => ['white'] }); | ||
| 709 | |||
| 710 | $domain->merge(data => { email => 'me@example.com', username => 'me' }); | ||
| 711 | |||
| 712 | $domain->merge(data => { email => 'we@example.com' }); | ||
| 713 | |||
| 714 | $domain->merge(data => { colors => ['white', 'green'], username => 'we' }); | ||
| 715 | |||
| 716 | =back | ||
| 717 | |||
| 718 | =cut | ||
| 719 | |||
| 720 | =head2 pop | ||
| 721 | |||
| 722 | pop(Str $key) : Object | ||
| 723 | |||
| 724 | The pop method pops the data off of the stack associated with a specific key. | ||
| 725 | |||
| 726 | =over 4 | ||
| 727 | |||
| 728 | =item pop example #1 | ||
| 729 | |||
| 730 | # given: synopsis | ||
| 731 | |||
| 732 | $domain->pop('history'); | ||
| 733 | |||
| 734 | =back | ||
| 735 | |||
| 736 | =cut | ||
| 737 | |||
| 738 | =head2 push | ||
| 739 | |||
| 740 | push(Str $key, Any @val) : Object | ||
| 741 | |||
| 742 | The push method pushes data onto the stack associated with a specific key. | ||
| 743 | |||
| 744 | =over 4 | ||
| 745 | |||
| 746 | =item push example #1 | ||
| 747 | |||
| 748 | # given: synopsis | ||
| 749 | |||
| 750 | $domain->push('history', { updated => 1234567890 }); | ||
| 751 | |||
| 752 | =back | ||
| 753 | |||
| 754 | =cut | ||
| 755 | |||
| 756 | =head2 set | ||
| 757 | |||
| 758 | set(Str $key, Any $val) : Object | ||
| 759 | |||
| 760 | The set method commits the data associated with a specific key to the channel. | ||
| 761 | |||
| 762 | =over 4 | ||
| 763 | |||
| 764 | =item set example #1 | ||
| 765 | |||
| 766 | # given: synopsis | ||
| 767 | |||
| 768 | $domain->set('updated', 1234567890); | ||
| 769 | |||
| 770 | =back | ||
| 771 | |||
| 772 | =cut | ||
| 773 | |||
| 774 | =head2 shift | ||
| 775 | |||
| 776 | shift(Str $key) : Object | ||
| 777 | |||
| 778 | The shift method shifts data off of the stack associated with a specific key. | ||
| 779 | |||
| 780 | =over 4 | ||
| 781 | |||
| 782 | =item shift example #1 | ||
| 783 | |||
| 784 | # given: synopsis | ||
| 785 | |||
| 786 | $domain->shift('history'); | ||
| 787 | |||
| 788 | =back | ||
| 789 | |||
| 790 | =cut | ||
| 791 | |||
| 792 | =head2 state | ||
| 793 | |||
| 794 | state() : HashRef | ||
| 795 | |||
| 796 | The state method returns the raw aggregate data associated with the object. | ||
| 797 | |||
| 798 | =over 4 | ||
| 799 | |||
| 800 | =item state example #1 | ||
| 801 | |||
| 802 | # given: synopsis | ||
| 803 | |||
| 804 | $domain->state; | ||
| 805 | |||
| 806 | =back | ||
| 807 | |||
| 808 | =cut | ||
| 809 | |||
| 810 | =head2 unshift | ||
| 811 | |||
| 812 | unshift(Str $key, Any @val) : Object | ||
| 813 | |||
| 814 | The unshift method unshifts data onto the stack associated with a specific key. | ||
| 815 | |||
| 816 | =over 4 | ||
| 817 | |||
| 818 | =item unshift example #1 | ||
| 819 | |||
| 820 | # given: synopsis | ||
| 821 | |||
| 822 | $domain->unshift('history', { updated => 1234567890 }); | ||
| 823 | |||
| 824 | =back | ||
| 825 | |||
| 826 | =cut | ||
| 827 | |||
| 828 | =head1 AUTHOR | ||
| 829 | |||
| 830 | Al Newkirk, C<awncorp@cpan.org> | ||
| 831 | |||
| 832 | =head1 LICENSE | ||
| 833 | |||
| 834 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 835 | |||
| 836 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 837 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 838 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 839 | |||
| 840 | =head1 PROJECT | ||
| 841 | |||
| 842 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 843 | |||
| 844 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 845 | |||
| 846 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 847 | |||
| 848 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 849 | |||
| 850 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 851 | |||
| 852 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 853 | |||
| 854 | =cut |