| 1 | package Badger; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use Carp; | ||
| 5 | use lib; | ||
| 6 | use Badger::Hub; | ||
| 7 | use Badger::Class | ||
| 8 | debug => 0, | ||
| 9 | base => 'Badger::Base', | ||
| 10 | import => 'class', | ||
| 11 | words => 'HUB', | ||
| 12 | constants => 'PKG ARRAY DELIMITER', | ||
| 13 | filesystem => 'Bin', | ||
| 14 | exports => { | ||
| 15 | hooks => { | ||
| 16 | lib => [ sub { $_[0]->lib($_[3]) }, 1], | ||
| 17 | }, | ||
| 18 | fail => \&_export_handler, | ||
| 19 | }; | ||
| 20 | |||
| 21 | our $VERSION = 0.14; | ||
| 22 | our $HUB = 'Badger::Hub::Badger'; | ||
| 23 | our $AUTOLOAD; | ||
| 24 | |||
| 25 | |||
| 26 | sub _export_handler { | ||
| 27 | # TODO: we should be able to refactor this down, now that Badger::Exporter | ||
| 28 | # can handle this argument shifting | ||
| 29 | my ($class, $target, $key, $symbols) = @_; | ||
| 30 | croak "You didn't specify a value for the '$key' load option." | ||
| 31 | unless @$symbols; | ||
| 32 | my $module = join(PKG, $class, $key); | ||
| 33 | my $option = shift @$symbols; | ||
| 34 | class($module)->load; | ||
| 35 | $module->export($target, $option); | ||
| 36 | return 1; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub init { | ||
| 41 | my ($self, $config) = @_; | ||
| 42 | my $hub = $config->{ hub } || $self->class->any_var(HUB); | ||
| 43 | unless (ref $hub) { | ||
| 44 | class($hub)->load; | ||
| 45 | $hub = $hub->new($config); | ||
| 46 | } | ||
| 47 | $self->{ hub } = $hub; | ||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub lib { | ||
| 52 | my ($self, $lib) = @_; | ||
| 53 | $lib = [split(DELIMITER, $lib)] | ||
| 54 | unless ref $lib eq ARRAY; | ||
| 55 | foreach (@$lib) { | ||
| 56 | # resolve directories relative to current working directory so that | ||
| 57 | # relative paths Just Work[tm], e.g. ../perl/lib as well as absolute | ||
| 58 | # paths. e.g. /full/path/to/perl/lib | ||
| 59 | my $dir = Bin->dir($_)->must_exist; | ||
| 60 | $self->debug("adding lib: $dir") if DEBUG; | ||
| 61 | lib->import($dir->absolute); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub hub { | ||
| 66 | my $self = shift; | ||
| 67 | |||
| 68 | if (ref $self) { | ||
| 69 | return @_ | ||
| 70 | ? ($self->{ hub } = shift) | ||
| 71 | : $self->{ hub }; | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | return @_ | ||
| 75 | ? $self->class->var(HUB => shift) | ||
| 76 | : $self->class->var(HUB) | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | sub codec { | ||
| 81 | shift->hub->codec(@_); | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | sub config { | ||
| 86 | my $self = shift; | ||
| 87 | return $self->hub->config; | ||
| 88 | } | ||
| 89 | |||
| 90 | # TODO: AUTOLOAD method which polls hub to see what it supports | ||
| 91 | |||
| 92 | 1; | ||
| 93 | |||
| 94 | __END__ |