-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Moose's meta traits more effectively so that single args may be d…
…eclared in roles.
- Loading branch information
Showing
9 changed files
with
160 additions
and
68 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
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
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
package # Hide from the indexer for now until docs are added later. | |||
MooseX::SingleArg::Meta::Class; | |||
use Moose::Role; | |||
|
|||
has single_arg => ( | |||
is => 'rw', | |||
isa => 'Str', | |||
predicate => 'has_single_arg', | |||
); | |||
|
|||
has force_single_arg => ( | |||
is => 'rw', | |||
isa => 'Bool', | |||
default => 0, | |||
); | |||
|
|||
1; |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,24 @@ | |||
package # Hide from the indexer for now until docs are added later. | |||
MooseX::SingleArg::Meta::Object; | |||
use Moose::Role; | |||
|
|||
use Carp qw( croak ); | |||
|
|||
around BUILDARGS => sub{ | |||
my $orig = shift; | |||
my $class = shift; | |||
|
|||
my $meta = $class->meta(); | |||
croak("single_arg() has not been called for $class") if !$meta->has_single_arg(); | |||
|
|||
my $force = $meta->force_single_arg(); | |||
croak("$class accepts only one argument") if $force and @_>1; | |||
|
|||
if (@_==1 and ($force or ref($_[0]) ne 'HASH')) { | |||
return $class->$orig( $meta->single_arg() => $_[0] ); | |||
} | |||
|
|||
return $class->$orig( @_ ); | |||
}; | |||
|
|||
1; |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
package # Hide from the indexer for now until docs are added later. | |||
MooseX::SingleArg::Meta::Role; | |||
use Moose::Role; | |||
|
|||
has single_arg => ( | |||
is => 'rw', | |||
isa => 'Str', | |||
predicate => 'has_single_arg', | |||
); | |||
|
|||
has force_single_arg => ( | |||
is => 'rw', | |||
isa => 'Bool', | |||
default => 0, | |||
); | |||
|
|||
1; |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,29 @@ | |||
package # Hide from the indexer for now until docs are added later. | |||
MooseX::SingleArg::Meta::ToClass; | |||
use Moose::Role; | |||
|
|||
around apply => sub { | |||
my $orig = shift; | |||
my $self = shift; | |||
my $from_role = shift; | |||
my $to_class = shift; | |||
|
|||
$to_class = Moose::Util::MetaRole::apply_metaroles( | |||
for => $to_class, | |||
class_metaroles => { | |||
class => ['MooseX::SingleArg::Meta::Class'], | |||
}, | |||
); | |||
|
|||
Moose::Util::MetaRole::apply_base_class_roles( | |||
for => $to_class, | |||
roles => ['MooseX::SingleArg::Meta::Object'], | |||
); | |||
|
|||
$to_class->single_arg( $from_role->single_arg() ) if $from_role->has_single_arg(); | |||
$to_class->force_single_arg( $from_role->force_single_arg() ); | |||
|
|||
return $self->$orig( $from_role, $to_class ); | |||
}; | |||
|
|||
1; |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,26 @@ | |||
package # Hide from the indexer for now until docs are added later. | |||
MooseX::SingleArg::Meta::ToRole; | |||
use Moose::Role; | |||
|
|||
around apply => sub{ | |||
my $orig = shift; | |||
my $self = shift; | |||
my $from_role = shift; | |||
my $to_role = shift; | |||
|
|||
$to_role = Moose::Util::MetaRole::apply_metaroles( | |||
for => $to_role, | |||
role_metaroles => { | |||
role => ['MooseX::SingleArg::Meta::Role'], | |||
application_to_class => ['MooseX::SingleArg::Meta::ToClass'], | |||
application_to_role => ['MooseX::SingleArg::Meta::ToRole'], | |||
}, | |||
); | |||
|
|||
$to_role->single_arg( $from_role->single_arg() ) if $from_role->has_single_arg(); | |||
$to_role->force_single_arg( $from_role->force_single_arg() ); | |||
|
|||
return $self->$orig( $from_role, $to_role ); | |||
}; | |||
|
|||
1; |
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