I know this is probably EXTREMELY wrong, but it appears to work:
use Mojo::Base;
use Mojo::Log;
#------------------------------------------------------------------------------
sub GetFakeSelf {
my ($dbh, $Organization, $User, $Type) = @_;
my $Stash = { User => $User, Organization => $Organization };
my $Session = { ClientType => $Type };
my $App = Mojo::Base->new();
my $Log = Mojo::Log->new();
my $Self = Mojo::Base->new();
$Self->attr(app => sub { return $App; } );
$Self->attr(stash => sub { return $Stash; } );
$Self->attr(session => sub { return $Session; } );
$Self->app->attr(mdb => sub { return $dbh; } );
$Self->app->attr(log => sub { return $Log; } );
$Self->app->log->level('warn'); # debug, info, warn, error, or fatal
return $Self;
}
The cool thing is this more easily opens up the possibility of forking a
child process when necessary.
James
On Tuesday, December 31, 2013 2:17:17 PM UTC-6, James Bearden wrote:
>
> Ben,
>
> Thanks for the suggestion. I actually do something very similar to that in
> other situations, but I was hoping for an easier solution here. It is
> basically easier to split up the bigger data files than rewrite a
> significant chunk of my code.
>
> James
>
> On Tuesday, December 31, 2013 2:04:42 PM UTC-6, Ben van Staveren wrote:
>>
>> [snip]
>>
>> You're better off moving all that code into a separate module, something
>> like this:
>>
>> package My::Work::Module;
>> use Mojo::Base '-base';
>>
>> has 'app' => undef;
>> has 'log' => sub {
>> my $self = shift;
>> return $self->app->log if defined $self->app;
>> return Mojo::Log->new(\%your_log_options);
>> };
>> has 'dbh' => undef;
>>
>> sub ImportClients {
>> ...
>> }
>>
>> 1;
>>
>> In your app code you can then use that as such:
>>
>> my $work = My::Work::Module->new(app => $self->app, dbh =>
>> GetMyDBHandle());
>>
>> And in command line scripts, you'd set up the DBH, and would just do:
>> my $work = My::Work::Module->new(dbh => $my_dbh);
>>
>> And it would then not have an app attribute, but i would have a log
>> attribute. You'd just have to make sure that code in the work module
>> calls "$self->log->debug(...)" and not "$self->app->log->debug".
>>
>> At least that's how I'd do it....
>>
>
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.