fix for class attributes in roles
[gitmo/MooseX-AlwaysCoerce.git] / lib / MooseX / AlwaysCoerce.pm
index 240dff5..d676e25 100644 (file)
@@ -18,11 +18,11 @@ MooseX::AlwaysCoerce - Automatically enable coercions for Moose attributes
 
 =head1 VERSION
 
-Version 0.05
+Version 0.07
 
 =cut
 
-our $VERSION = '0.05';
+our $VERSION = '0.07';
 
 =head1 SYNOPSIS
 
@@ -54,20 +54,22 @@ Use C<< coerce => 0 >> to disable a coercion explicitly.
     use namespace::autoclean;
     use Moose::Role;
 
-    has coerce => (
-        lazy    => 1,
-        reader  => "should_coerce",
-        default => sub {
-            return 1 if shift->type_constraint->has_coercion;
-            return 0;
-        }
-    );
+    around should_coerce => sub {
+        my $orig = shift;
+        my $self = shift;
+
+        my $current_val = $self->$orig(@_);
 
+        return 0 if defined $current_val && $current_val == 0;
+        return 1 if $self->type_constraint->has_coercion;
+        return 0;
+    };
 
     package MooseX::AlwaysCoerce::Role::Meta::Class;
     use namespace::autoclean;
     use Moose::Role;
     use Moose::Util::TypeConstraints;
+    use MooseX::ClassAttribute;
 
     around add_class_attribute => sub {
         my $next = shift;
@@ -82,11 +84,19 @@ Use C<< coerce => 0 >> to disable a coercion explicitly.
 }
 
 my (undef, undef, $init_meta) = Moose::Exporter->build_import_methods(
+
     install => [ qw(import unimport) ],
+
     class_metaroles => {
         attribute   => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
         class       => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
     },
+
+    role_metaroles => {
+        # applied_attribute should be available soon, for now roles are borked
+        # applied_attribute   => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
+        role                => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
+    }
 );
 
 sub init_meta {
@@ -146,7 +156,7 @@ Dave Rolsky, for telling me how to do it the L<Moose> way.
 
 =head1 COPYRIGHT & LICENSE
 
-Copyright (c) 2009 Rafael Kitover
+Copyright (c) 2009-2010 Rafael Kitover
 
 This program is free software; you can redistribute it and/or modify it
 under the same terms as Perl itself.