Skip to content

Commit

Permalink
spin off of Throwable-X
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbs committed Oct 28, 2010
1 parent f516413 commit d96e5ff
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 320 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,2 +1,2 @@
.build
Throwable-X-*
Role-HasPayload-*
15 changes: 1 addition & 14 deletions Changes
@@ -1,17 +1,4 @@
Revision history for {{$dist->name}}

{{$NEXT}}

0.004 2010-10-20 11:38:07 America/New_York
replace [\v] with [\x0d\x0a] to support antiquated perl 5 version 8

0.003 2010-10-19 21:59:07 America/New_York
another missing prereq; I think that's the last one...

0.002 2010-10-19 14:26:28 America/New_York
add missing prereq for Throwable

0.001 2010-10-18 21:44:13 America/New_York

first release

spun off of Throwable-X 0.004
6 changes: 1 addition & 5 deletions dist.ini
@@ -1,10 +1,6 @@
name = Throwable-X
name = Role-HasPayload
author = Ricardo Signes <rjbs@cpan.org>
license = Perl_5
copyright_holder = Ricardo Signes

[Prereqs]
Throwable = 0
MooseX::OneArgNew = 0

[@RJBS]
19 changes: 19 additions & 0 deletions lib/Role/HasPayload.pm
@@ -0,0 +1,19 @@
package Role::HasPayload;
use Moose::Role;
# ABSTRACT: something that carries a payload

=head1 OVERVIEW
Including Role::HasPayload in your class is a promise to provide a C<payload>
method that returns a hashref of data to be used for some purpose. Some
implementations of pre-built payload behavior are bundled with Role-HasPayload:
=for :list
* L<Role::HasPayload::Auto> - automatically compute a payload from attribtues
* L<Role::HasPayload::Merged> - merge auto-payload with data from constructor
=cut

requires 'payload';

no Moose::Role;
27 changes: 13 additions & 14 deletions lib/Throwable/X/AutoPayload.pm → lib/Role/HasPayload/Auto.pm
@@ -1,15 +1,15 @@
package Throwable::X::AutoPayload;
package Role::HasPayload::Auto;
use Moose::Role;
# ABSTRACT: a thing that automatically computes its payload based on attributes

=head1 SYNOPSIS
package X::Example;
package Example;
use Moose;
with qw(Throwable::X::AutoPayload);
with qw(Role::HasPayload::Auto);
sub Payload { 'Throwable::X::Meta::Attribute::Payload' }
sub Payload { 'Role::HasPayload::Meta::Attribute::Payload' }
has height => (
is => 'ro',
Expand All @@ -27,7 +27,7 @@ use Moose::Role;
...then...
my $example = X::Example->new({
my $example = Example->new({
height => 10,
width => 20,
color => 'blue',
Expand All @@ -37,24 +37,22 @@ use Moose::Role;
=head1 DESCRIPTION
Throwable::X::AutoPayload only provides one method, C<payload>, which returns a
Role::HasPayload::Auto only provides one method, C<payload>, which returns a
hashref of the name and value of every attribute on the object with the
Throwable::X::Meta::Attribute::Payload trait. (The attribute value is gotten
with the the method returned by the attribute's C<get_read_method> method.)
Role::HasPayload::Meta::Attribute::Payload trait. (The attribute value is
gotten with the the method returned by the attribute's C<get_read_method>
method.)
This role is especially useful when combined with
L<Throwable::X::WithMessage::Errf>.
This role is especially useful when combined with L<Role::HasMessage::Errf>.
=cut

use Throwable::X::Meta::Attribute::Payload;

use namespace::clean -except => 'meta';
use Role::HasPayload::Meta::Attribute::Payload;

sub payload {
my ($self) = @_;

my @attrs = grep { $_->does('Throwable::X::Meta::Attribute::Payload') }
my @attrs = grep { $_->does('Role::HasPayload::Meta::Attribute::Payload') }
$self->meta->get_all_attributes;

my %payload = map {;
Expand All @@ -66,4 +64,5 @@ sub payload {
}


no Moose::Role;
1;
89 changes: 89 additions & 0 deletions lib/Role/HasPayload/Merged.pm
@@ -0,0 +1,89 @@
package Role::HasPayload::Merged;
use Moose::Role;
# ABSTRACT: merge autogenerated payload with constructor-specified payload

=head1 SYNOPSIS
package Example;
use Moose;
with qw(Role::HasPayload::Merged);
sub Payload { 'Role::HasPayload::Meta::Attribute::Payload' }
has height => (
is => 'ro',
traits => [ Payload ],
);
has width => (
is => 'ro',
traits => [ Payload ],
);
has color => (
is => 'ro',
);
...then...
my $example = Example->new({
height => 10,
width => 20,
color => 'blue',
payload => { depth => 30 },
});
$example->payload; # { height => 10, width => 20, depth => 30 }
=head1 DESCRIPTION
Role::HasPayload::Merged provides a C<payload> method and a C<payload>
attribute. It computes the result of the C<payload> method when it's called,
first by gathering the values of attributes marked with
Role::HasPayload::Meta::Attribute::Payload, then by merging in the contents of
the C<payload> attribute (provided at construction).
If an entry in the constructor-provided payload already exists in the
autogenerated payload, a warning is issued and the autogenerated value is used.
For a bit more on the autogenerated payload, see L<Role::HasPayload::Auto>.
This role is especially useful when combined with L<Role::HasMessage::Errf>.
=cut

use Role::HasPayload::Meta::Attribute::Payload;

has payload => (
reader => '_payload_to_merge',
isa => 'HashRef',
default => sub { {} },
);

sub payload {
my ($self) = @_;

my @attrs = grep { $_->does('Role::HasPayload::Meta::Attribute::Payload') }
$self->meta->get_all_attributes;

my %payload = map {;
my $method = $_->get_read_method;
($_->name => $self->$method)
} @attrs;

my $manual_payload = $self->_payload_to_merge;
KEY: for my $key (keys %$manual_payload) {
if (exists $payload{ $key }) {
Carp::carp("declining to override automatic payload entry $key");
next KEY;
}

$payload{ $key } = $manual_payload->{ $key };
}

return \%payload;
}

no Moose::Role;
1;
@@ -1,11 +1,11 @@
package Throwable::X::Meta::Attribute::Payload;
package Role::HasPayload::Meta::Attribute::Payload;
use Moose::Role;
# ABSTRACT: a meta-trait for attributes forming the autopayload

=head1 DESCRIPTION
This package basically does nothing but serve as a marker. You want to look at
the docs for L<Throwable::X> and L<Throwable::X::AutoPayload> instead.
the docs for L<Role::HasPayload::Auto> instead.
=cut

Expand Down

0 comments on commit d96e5ff

Please sign in to comment.