Skip to content

Commit

Permalink
Mo/Mouse parts
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Oct 11, 2011
1 parent 5369280 commit 3041848
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Mo/Features.pod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ Finalizer for L<Mo::Inline> usage. Only works with inlined Mo.

Add C<is> feature to C<has>.

=item L<Mo::Moose>

Use Moose in place of Mo for everything.

=item L<Mo::Mouse>

Use Mouse in place of Mo for everything.

=item L<Mo::option>

Add C<option> feature to C<has>.
Expand Down
3 changes: 3 additions & 0 deletions lib/Mo/Mouse.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package Mo::Mouse;$M="Mo::";
$VERSION=0.28;
*{$M.'Mouse::e'}=sub{my($P,$e)=@_;$P=~s/::$//;%$e=(M=>1);require Mouse;Mouse->import({into=>$P});Mouse::Util::MetaRole::apply_metaroles(for=>$P,class_metaroles=>{attribute=>['Attr::Trait']},)};BEGIN{package Attr::Trait;use Mouse::Role;around _process_options=>sub{my$orig=shift;my$c=shift;my($n,$o)=@_;$o->{is}||='rw';$o->{lazy}||=1 if defined$o->{default}or defined$o->{builder};$c->$orig(@_)};$INC{'Attr/Trait.pm'}=1}
22 changes: 22 additions & 0 deletions lib/Mo/Mouse.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=encoding utf8

=head1 Name

Mo::Mouse - Use Mouse instead of Mo

=head1 Synopsis

use Mo 'Mouse';
has foo => ();

=head1 Description

If you use L<Mo> and want to try L<Mouse> without adding C<is> and C<lazy>
options to all your attributes you can just do this:

use Mo 'Mouse';
use foo => ();

and everything should work, but now uses Mouse instead of Mo for everything.

=cut
1 change: 1 addition & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ALL := \
../lib/Mo/required.pm \
../lib/Mo/xs.pm \
../lib/Mo/Moose.pm \
../lib/Mo/Mouse.pm \

all: $(ALL)

Expand Down
33 changes: 33 additions & 0 deletions src/Mo/Mouse.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Mo::Mouse;$MoPKG = "Mo::";
$VERSION = 0.28;

*{$MoPKG.'Mouse::e'} = sub {
my ($caller_pkg, $exports) = @_;
$caller_pkg =~ s/::$//;
%$exports = (M => 1);
require Mouse;
Mouse->import({into => $caller_pkg});
Mouse::Util::MetaRole::apply_metaroles(
for => $caller_pkg,
class_metaroles => { attribute => ['Attr::Trait'] },
);
};

BEGIN {
package Attr::Trait;
use Mouse::Role;

around _process_options => sub {
my $orig = shift;
my $class = shift;
my ($name, $options) = @_;

$options->{is} ||= 'rw';
$options->{lazy} ||= 1
if defined $options->{default} or
defined $options->{builder};

$class->$orig(@_);
};
$INC{'Attr/Trait.pm'} = 1;
}
18 changes: 18 additions & 0 deletions t/Mouse.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use Test::More tests => 4;

{
package Foo;
use Mo 'Mouse';

has foo => ();
has bar => (default => 'I like pie!');
}

my $f = Foo->new(foo => 42);
die @Foo::ISA;

is $f->foo, 42, 'Normal';
is $f->{bar}, undef, 'before (lazy)';
is $f->bar, 'I like pie!', 'default';
is $f->{bar}, 'I like pie!', 'after';

0 comments on commit 3041848

Please sign in to comment.