| 1 | package MooseX::Role::Atom; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.008005; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Moose::Role 1.08 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | our $VERSION = '0.02'; | |||||||||
| 9 | |||||||||||
| 10 | 0 | ▶ | sub import { | ||||||||
|
Subroutine
import: not called CC=6 SCAR=34.9
| |||||||||||
| 11 | 0 | my $class = shift; | |||||||||
| 12 | 0 | my @model = @{ pop() }; | |||||||||
| 13 | 0 | my $package = caller(); | |||||||||
| 14 | |||||||||||
| 15 | # Generate the metaobject, and immediately remove all the exported symbols. | ||||||||||
| 16 | # This is a hack until I can work out how to avoid exporting them at all. | ||||||||||
| 17 | # NOTE: Yuck! | ||||||||||
| 18 | 0 | eval "package $package; use Moose::Role; no Moose::Role;"; | |||||||||
| 19 | 0 | ▶ | die $@ if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | |||||||||||
| 21 | # Push the commands to generate the role directly through the meta object. | ||||||||||
| 22 | 0 | my $meta = $package->meta; | |||||||||
| 23 | 0 | my @with = (); | |||||||||
| 24 | 0 | ▶ | while ( @model ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | my $command = shift @model; | |||||||||
| 26 | 0 | my $params = shift @model; | |||||||||
| 27 | 0 | ▶ | $params = [ $params ] unless ref $params eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | ▶ | if ( $command eq 'with' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | push @with, $params; | |||||||||
| 30 | 0 | next; | |||||||||
| 31 | } | ||||||||||
| 32 | 1 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 0 | &{"Moose::Role::$command"}( $meta, @$params ); | |||||||||
| 34 | } | ||||||||||
| 35 | 0 | foreach my $params ( @with ) { | |||||||||
| 36 | 0 | Moose::with( $meta, @$params ); | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | # Immediately make the role immutable | ||||||||||
| 40 | 0 | $meta->make_immutable; | |||||||||
| 41 | |||||||||||
| 42 | 0 | return 1; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 1 | 1; | |||||||||
| 46 | |||||||||||
| 47 | =pod | ||||||||||
| 48 | |||||||||||
| 49 | =head1 NAME | ||||||||||
| 50 | |||||||||||
| 51 | MooseX::Role::Atom - Non-immutable roles are silly. Lets fix that. | ||||||||||
| 52 | |||||||||||
| 53 | =head1 SYNOPSIS | ||||||||||
| 54 | |||||||||||
| 55 | # A basic role the official way | ||||||||||
| 56 | package Foo; | ||||||||||
| 57 | |||||||||||
| 58 | use Moose::Role; | ||||||||||
| 59 | use namespace::autoclean; | ||||||||||
| 60 | |||||||||||
| 61 | requires 'icecream'; | ||||||||||
| 62 | |||||||||||
| 63 | has 'something' => ( | ||||||||||
| 64 | is => 'ro', | ||||||||||
| 65 | isa => 'Str', | ||||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | __PACKAGE__->meta->make_immutable; | ||||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | # A basic role the atomic way | ||||||||||
| 73 | package Bar; | ||||||||||
| 74 | |||||||||||
| 75 | use MooseX::Role::Atom [ | ||||||||||
| 76 | requires => 'icecream', | ||||||||||
| 77 | has => [ | ||||||||||
| 78 | something => ( | ||||||||||
| 79 | is => 'ro', | ||||||||||
| 80 | isa => 'Str', | ||||||||||
| 81 | ), | ||||||||||
| 82 | ], | ||||||||||
| 83 | ]; | ||||||||||
| 84 | |||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||
| 86 | |||||||||||
| 87 | B<WARNING: THIS MODULE IS PRIMARILY A POLITICAL STATEMENT AT THIS TIME AND MAY | ||||||||||
| 88 | CHANGE WITHOUT NOTICE IN RESPONSE TO FEEDBACK> | ||||||||||
| 89 | |||||||||||
| 90 | See the description for L<MooseX::Atom>. | ||||||||||
| 91 | |||||||||||
| 92 | =head1 SUPPORT | ||||||||||
| 93 | |||||||||||
| 94 | Bugs should be reported via the CPAN bug tracker at | ||||||||||
| 95 | |||||||||||
| 96 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Atom> | ||||||||||
| 97 | |||||||||||
| 98 | =head1 AUTHOR | ||||||||||
| 99 | |||||||||||
| 100 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||
| 101 | |||||||||||
| 102 | =head1 SEE ALSO | ||||||||||
| 103 | |||||||||||
| 104 | L<Moose> | ||||||||||
| 105 | |||||||||||
| 106 | =head1 COPYRIGHT | ||||||||||
| 107 | |||||||||||
| 108 | Copyright 2010 Adam Kennedy. | ||||||||||
| 109 | |||||||||||
| 110 | This program is free software; you can redistribute | ||||||||||
| 111 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 112 | |||||||||||
| 113 | The full text of the license can be found in the | ||||||||||
| 114 | LICENSE file included with this module. | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||