4460e72c1937c314084c5fa09d19e70ec8181a3c
[gitmo/Moose.git] / lib / Moose / Role.pm
1
2 package Moose::Role;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'blessed';
9 use Sub::Name    'subname';
10
11 our $VERSION = '0.01';
12
13 use Moose::Meta::Role;
14 use Moose::Util::TypeConstraints;
15
16 sub import {
17         shift;
18         my $pkg = caller();
19         
20         # we should never export to main
21         return if $pkg eq 'main';
22         
23         Moose::Util::TypeConstraints->import($pkg);
24
25         my $meta;
26         if ($pkg->can('meta')) {
27                 $meta = $pkg->meta();
28                 (blessed($meta) && $meta->isa('Moose::Meta::Role'))
29                         || confess "Whoops, not møøsey enough";
30         }
31         else {
32                 $meta = Moose::Meta::Role->initialize(':package' => $pkg);
33                 $meta->add_method('meta' => sub {
34                         # re-initialize so it inherits properly
35                         Moose::Meta::Role->initialize(':package' => $pkg);                      
36                 })              
37         }
38         
39         # NOTE:
40         # &alias_method will install the method, but it 
41         # will not name it with 
42         $meta->alias_method('requires' => subname 'Moose::Role::requires' => sub {
43             push @{$meta->requires} => @_;
44         });     
45
46
47         # make sure they inherit from Moose::Role::Base
48         {
49             no strict 'refs';
50             @{$meta->name . '::ISA'} = ('Moose::Role::Base');
51         }
52
53         # we recommend using these things 
54         # so export them for them
55         $meta->alias_method('confess' => \&Carp::confess);                      
56         $meta->alias_method('blessed' => \&Scalar::Util::blessed);          
57 }
58
59 package Moose::Role::Base;
60
61 use strict;
62 use warnings;
63
64 our $VERSION = '0.01';
65
66 1;
67
68 __END__
69
70 =pod
71
72 =head1 NAME
73
74 Moose::Role - The Moose role
75
76 =head1 SYNOPSIS
77
78 =head1 DESCRIPTION
79
80 =head1 METHODS
81
82 =over 4
83
84 =back
85
86 =head1 BUGS
87
88 All complex software has bugs lurking in it, and this module is no 
89 exception. If you find a bug please either email me, or add the bug
90 to cpan-RT.
91
92 =head1 AUTHOR
93
94 Stevan Little E<lt>stevan@iinteractive.comE<gt>
95
96 =head1 COPYRIGHT AND LICENSE
97
98 Copyright 2006 by Infinity Interactive, Inc.
99
100 L<http://www.iinteractive.com>
101
102 This library is free software; you can redistribute it and/or modify
103 it under the same terms as Perl itself. 
104
105 =cut