Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add simpler has_many syntax for Reflex::Collection.

  • Loading branch information...
commit 134c7e07d5a25f8006f822137c319d8773df7424 1 parent aeb3852
Rocco Caputo authored
25 TODO
@@ -14,29 +14,4 @@ I've done a half-baked implementation, but it needs:
14 14
15 15 ---
16 16
17   -Declarative syntactic sugar for collection attributes.
18   -Try to get it into the 0.080 release.
19   -
20   -They currently look like this:
21   -
22   - has connections => (
23   - is => 'rw',
24   - isa => 'Reflex::Collection',
25   - default => sub { Reflex::Collection->new() },
26   - handles => { remember_client => "remember" },
27   - );
28   -
29   -Perhaps they should look like this:
30   -
31   - collection connections => (
32   - handles => { remember_client => "remember" },
33   - );
34   -
35   -That is, the "collection" sub build an attribute that is 'rw', isa
36   -'Reflex::Collection', and instantiates its default value.
37   -
38   -Can it instantiate itself lazily?
39   -
40   ----
41   -
42 17 For a larger, longer-term list of todos, please see docs/TODO.otl.
7 eg/eg-34-tcp-server-echo.pl
@@ -14,12 +14,7 @@
14 14 use Reflex::Collection;
15 15 use EchoStream;
16 16
17   - has clients => (
18   - is => 'rw',
19   - isa => 'Reflex::Collection',
20   - default => sub { Reflex::Collection->new() },
21   - handles => { remember_client => "remember" },
22   - );
  17 + has_many clients => ( handles => { remember_client => "remember" } );
23 18
24 19 sub on_accept {
25 20 my ($self, $args) = @_;
14 eg/eg-42-reflex-in-poe.pl
@@ -18,12 +18,7 @@
18 18 use Reflex::Collection;
19 19 use Reflex::Client;
20 20
21   - has connections => (
22   - is => 'rw',
23   - isa => 'Reflex::Collection',
24   - default => sub { Reflex::Collection->new() },
25   - handles => { remember_client => "remember" },
26   - );
  21 + has_many clients => ( handles => { remember_client => "remember" } );
27 22
28 23 sub said {
29 24 my ($mybot, $bot_event) = @_;
@@ -92,12 +87,7 @@
92 87 extends 'Reflex::Acceptor';
93 88 use Reflex::Collection;
94 89
95   - has clients => (
96   - is => 'rw',
97   - isa => 'Reflex::Collection',
98   - default => sub { Reflex::Collection->new() },
99   - handles => { remember_client => "remember" },
100   - );
  90 + has_many clients => ( handles => { remember_client => "remember" } );
101 91
102 92 sub on_accept {
103 93 my ($self, $args) = @_;
6 lib/Reflex/Acceptor.pm
@@ -35,10 +35,8 @@ Reflex::Acceptor - non-blocking client socket acceptor
35 35 use Reflex::Collection;
36 36 use EchoStream; # See eg directory.
37 37
38   - has clients => (
39   - is => 'rw',
40   - isa => 'Reflex::Collection',
41   - default => sub { Reflex::Collection->new() },
  38 + # From Reflex::Collection.
  39 + has_many clients => (
42 40 handles => { remember_client => "remember" },
43 41 );
44 42
49 lib/Reflex/Collection.pm
@@ -4,10 +4,14 @@
4 4
5 5 package Reflex::Collection;
6 6 use Moose;
  7 +use Moose::Exporter;
7 8 use Reflex::Callbacks qw(cb_method);
  9 +use Carp qw(cluck);
8 10
9 11 extends 'Reflex::Base';
10 12
  13 +Moose::Exporter->setup_import_methods( with_caller => [ qw( has_many ) ]);
  14 +
11 15 has objects => (
12 16 is => 'rw',
13 17 isa => 'HashRef[Reflex::Collectible]',
@@ -30,6 +34,22 @@ sub cb_forget {
30 34 delete $self->objects()->{$args->{_sender}};
31 35 }
32 36
  37 +sub has_many {
  38 + my ($caller, $name, %etc) = @_;
  39 +
  40 + my $meta = Class::MOP::class_of($caller);
  41 + foreach (qw(is isa default)) {
  42 + cluck "has_many is ignoring your '$_' parameter" if exists $etc{$_};
  43 + }
  44 +
  45 + $etc{is} = 'ro';
  46 + $etc{isa} = 'Reflex::Collection';
  47 + $etc{lazy} = 1 unless exists $etc{lazy};
  48 + $etc{default} = sub { Reflex::Collection->new() };
  49 +
  50 + $meta->add_attribute($name, %etc);
  51 +}
  52 +
33 53 1;
34 54
35 55 __END__
@@ -47,10 +67,8 @@ Reflex::Collection - Autmatically manage a collection of collectible objects
47 67 use Reflex::Collection;
48 68 use EchoStream;
49 69
50   - has clients => (
51   - is => 'rw',
52   - isa => 'Reflex::Collection',
53   - default => sub { Reflex::Collection->new() },
  70 + # From Reflex::Collection.
  71 + has_many clients => (
54 72 handles => { remember_client => "remember" },
55 73 );
56 74
@@ -90,6 +108,29 @@ TODO - Reflex::Collection is an excellent place to manage pools of
90 108 objects. Provide a callback interface for pulling new objects as
91 109 needed.
92 110
  111 +=head2 has_many
  112 +
  113 +Reflex::Collection exports the has_many() function, which works like
  114 +Moose's has() with "is", "isa", "lazy" and "default" set to common
  115 +values. For example:
  116 +
  117 + has_many connections => (
  118 + handles => { remember_connection => "remember" },
  119 + );
  120 +
  121 +... is equivalent to:
  122 +
  123 + has connections => (
  124 + # Defaults provided by has_many.
  125 + is => 'ro',
  126 + isa => 'Reflex::Collection',
  127 + lazy => 1,
  128 + default => sub { Reflex::Collection->new() {,
  129 +
  130 + # Customization.
  131 + handles => { remember_connection => "remember" },
  132 + );
  133 +
93 134 =head2 new
94 135
95 136 Create a new Reflex::Collection. It takes no parameters.

0 comments on commit 134c7e0

Please sign in to comment.
Something went wrong with that request. Please try again.