Created
May 4, 2011 18:58
-
-
Save sharifulin/955787 to your computer and use it in GitHub Desktop.
App::Base
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package App::Base; | |
use Mojo::Base; | |
use common::sense; | |
# code from Mojo::Base | |
sub import { | |
my $class = shift; | |
# Flag | |
return unless my $flag = shift; | |
# Args | |
my %args = @_; | |
# Caller | |
my $caller = caller; | |
# No limits! | |
no strict 'refs'; | |
no warnings 'redefine'; | |
# Base | |
if ($flag eq '-base') { $flag = 'Mojo::Base' } | |
# Controller | |
if ($flag eq '-controller') { $flag = 'Mojolicious::Controller' } | |
# Module | |
else { | |
my $file = $flag; | |
$file =~ s/::|'/\//g; | |
require "$file.pm" unless $flag->can('new'); | |
} | |
# ISA | |
push @{"${caller}::ISA"}, $flag; | |
# Can haz? | |
*{"${caller}::has"} = sub { Mojo::Base::attr($caller, @_) }; | |
# With Loader | |
if (my $with = $args{with}) { | |
for my $m (ref $with eq 'ARRAY' ? @$with : $with) { | |
my $file = $m; | |
$file =~ s/::|'/\//g; | |
require "$file.pm" unless $m->can('new'); | |
# Can attr? | |
my $attr = lc $m; | |
$attr =~ s/^app:://; | |
$attr =~ s/::|'/_/g; | |
*{"${caller}::$attr"} = sub { $m->new(%{ +shift }) }; | |
} | |
} | |
# App modules are common sense! | |
common::sense->import; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment