Skip to content

Commit

Permalink
Kelp::Base can now import just attr with -attr
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrtj committed Jun 26, 2024
1 parent 9564f3e commit a1d8a77
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
19 changes: 10 additions & 9 deletions lib/Kelp/Base.pm
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package Kelp::Base;

use strict ();
use warnings ();
use strict;
use warnings;
use feature ();
use Carp;
use Data::Dumper ();
use namespace::autoclean ();

require Data::Dumper;
require namespace::autoclean;
require Kelp::Util;

sub import
{
Expand All @@ -22,12 +23,12 @@ sub import
no strict 'refs';
no warnings 'redefine';

my $file = $base;
$file =~ s/::|'/\//g;
require "$file.pm" unless $base->can('new'); # thanks sri

push @{"${caller}::ISA"}, $base;
*{"${caller}::attr"} = sub { attr($caller, @_) };

if ($base ne '-attr') {
Kelp::Util::load_package($base);
push @{"${caller}::ISA"}, $base;
}
}

strict->import;
Expand Down
7 changes: 6 additions & 1 deletion lib/Kelp/Util.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package Kelp::Util;

use Kelp::Base -strict;
# No Kelp::Base here, because Kelp::Base uses Kelp::Util;
use v5.10;
use strict;
use warnings;

use Carp;
use Encode qw();
use Class::Inspector;
Expand All @@ -10,6 +14,7 @@ use Plack::Util;
our @CARP_NOT = (
qw(
Kelp
Kelp::Base
Kelp::Routes
Kelp::Context
)
Expand Down
18 changes: 18 additions & 0 deletions t/base_attr.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use Kelp::Base -strict;
use Test::More;

{

package WithAttrs;

use Kelp::Base -attr;

attr a1 => 55;
}

ok !WithAttrs->can('new'), 'new ok';
ok !WithAttrs->isa('Kelp::Base'), 'base ok';
can_ok 'WithAttrs', 'a1';

done_testing;

0 comments on commit a1d8a77

Please sign in to comment.