Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
238 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
.build | ||
Throwable-X-* | ||
Role-HasPayload-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
4 changes: 2 additions & 2 deletions
4
lib/Throwable/X/Meta/Attribute/Payload.pm → ...Role/HasPayload/Meta/Attribute/Payload.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.