Permalink
Browse files

Fix trait merging with single traits for CX::SimpleLogin

  • Loading branch information...
bobtfish committed Nov 7, 2009
1 parent 388ff24 commit 6f66a350fbc2adc11a434ae3bfec535cae163df5
Showing with 51 additions and 14 deletions.
  1. +2 −0 Changes
  2. +1 −1 Makefile.PL
  3. +4 −13 lib/CatalystX/Component/Traits.pm
  4. +44 −0 t/07-merge-single.t
View
@@ -1,5 +1,7 @@
Revision history for CatalystX-Component-Traits
+ - Fix merging singular traits rather than lists of traits.
+
0.12 2009-10-30 16:47:15
- add support for root-level components
View
@@ -5,7 +5,7 @@ all_from 'lib/CatalystX/Component/Traits.pm';
author 'Rafael Kitover <rkitover@cpan.org>';
license 'perl';
-test_requires 'Test::More';
+test_requires 'Test::More' => '0.88';
requires 'Catalyst::Runtime' => '5.80005';
requires 'MooseX::Traits::Pluggable' => '0.08';
@@ -162,26 +162,17 @@ sub _merge_traits {
$should_merge = $should_merge->()
if ref($should_merge) && reftype($should_merge) eq 'CODE';
+ my @right_traits = ref($right_traits) ? @$right_traits : $right_traits;
+ my @left_traits = ref($left_traits) ? @$left_traits : $left_traits;
unless ($should_merge) {
- my @right_traits = ref($right_traits) ? @$right_traits : $right_traits;
- my @left_traits = ref($left_traits) ? @$left_traits : $left_traits;
-
return @right_traits ? \@right_traits : \@left_traits;
}
- my (@left_traits, @right_traits, @to_remove);
-
- for my $trait (@$right_traits) {
- if ($trait =~ /^-(.*)/) {
- push @to_remove, $1;
- } else {
- push @right_traits, $trait;
- }
- }
- @left_traits = @$left_traits;
+ my @to_remove = map { s/^-// and $_ } @left_traits, @right_traits;;
my @traits = grep {
my $trait = $_;
+ $trait =~ /^-/ and return;
not any { $trait eq $_ } @to_remove;
} (@left_traits, @right_traits);
View
@@ -0,0 +1,44 @@
+use strict;
+use warnings;
+use Test::More;
+use Catalyst::Utils;
+
+{
+ package MyApp::Controller::SomeController;
+ use Moose;
+ extends 'Catalyst::Controller';
+ with 'CatalystX::Component::Traits';
+ has '+_trait_merge' => (default => 1);
+
+ __PACKAGE__->config( traits => 'Foo' );
+
+ package MyApp::TraitFor::Controller::SomeController::Foo;
+ use Moose::Role;
+ has 'foo' => (is => 'ro');
+
+ package MyApp::TraitFor::Controller::SomeController::Bar;
+ use Moose::Role;
+ has 'bar' => (is => 'ro');
+
+}
+
+ok((my $instance = MyApp::Controller::SomeController->COMPONENT(
+ bless({}, 'Catalyst'),
+ { traits => 'Bar', foo => 'bar', bar => 'baz' }
+ )),
+ 'created a component instance');
+
+ok(($instance->does('MyApp::TraitFor::Controller::SomeController::Foo')),
+ 'instance had parent ns trait loaded from component config');
+
+ok(($instance->does('MyApp::TraitFor::Controller::SomeController::Bar')),
+ 'instance had app ns trait loaded from component config');
+
+is eval { $instance->foo }, 'bar',
+ 'trait initialized from component config works';
+
+is eval { $instance->bar }, 'baz',
+ 'trait initialized from app config works';
+
+done_testing;
+

0 comments on commit 6f66a35

Please sign in to comment.