Skip to content

Commit

Permalink
add requries feature into Voson::Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ytnobody committed Aug 8, 2013
1 parent c1e90f6 commit a7aeaa3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/Voson/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use Carp;
sub new {
my ($class, %opts) = @_;
$class->_check_needs($opts{app});
$class->_check_requires($opts{app});
return bless {%opts}, $class;
}

Expand All @@ -21,6 +22,8 @@ sub exports {

sub needs { return () }

sub requires { return () }

sub _check_needs {
my ($class, $app) = @_;
local $Carp::CarpLevel = $Carp::CarpLevel + 1;
Expand All @@ -30,6 +33,14 @@ sub _check_needs {
}
}

sub _check_requires {
my ($class, $app) = @_;
local $Carp::CarpLevel = $Carp::CarpLevel + 1;
for my $requires ($class->requires) {
croak "$class requires $requires DSL, you have to load some plugin that provides $requires DSL" unless $app->{DSL}{$requires};
}
}

1;

__END__
Expand Down Expand Up @@ -72,7 +83,13 @@ You have to override it if you want to export some DSL.
Specifier for needs plugins.
You have to override it if you want to export some DSL.
=head2 requires
sub requires {
return qw/dsl_a dsl_b/;
}
Specifier for required DSLs.
=head1 AUTHOR
Expand Down
15 changes: 15 additions & 0 deletions t/model/010_plugin.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ use Voson::Core;
};
};
}
{
package Voson::Plugin::TestSeta;
use parent 'Voson::Plugin';
sub requires { qw/one/ };
sub two {
sub ($) { one() + 1 };
}
}

subtest basal => sub {
my $x = Voson::Plugin->new;
Expand All @@ -45,4 +53,11 @@ subtest needs_ok => sub {
is $v->dsl('incr')->(2), 3;
};

subtest requires_failure => sub {
throws_ok(
sub { Voson::Core->new(plugins => [qw/TestSeta/]) },
qr/Voson::Plugin::TestSeta requires one DSL, you have to load some plugin that provides one DSL/
);
};

done_testing;

0 comments on commit a7aeaa3

Please sign in to comment.