Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Created May 4, 2011 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharifulin/955787 to your computer and use it in GitHub Desktop.
Save sharifulin/955787 to your computer and use it in GitHub Desktop.
App::Base
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