Created
January 2, 2013 10:41
-
-
Save issm/4433703 to your computer and use it in GitHub Desktop.
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 Amon2::Plugin::Model; | |
use strict; | |
use warnings; | |
use Module::Find; | |
use Try::Tiny; | |
our $VERSION = '0.01'; | |
sub init { | |
my ($class, $context_class, $config) = @_; | |
my ($class_prefix) = split /::/, $context_class; | |
$class_prefix .= '::Model'; | |
useall $class_prefix; | |
no strict 'refs'; | |
*{"$context_class\::model"} = \&_model; | |
*{"$context_class\::__models"} = {}; | |
*{"$context_class\::__class_prefix"} = sub { $class_prefix }; | |
} | |
sub _model { | |
my ($self, @names) = @_; | |
die 'Model name is not specified.' unless @names; | |
for my $name ( @names ) { | |
if ( ! defined $self->{__models}{$name} ) { | |
try { | |
my $model_class = sprintf '%s::%s', $self->__class_prefix, (ucfirst $name); | |
$self->{__models}{$name} = $model_class->new( | |
c => $self, | |
); | |
} catch { | |
my ($msg) = @_; | |
die $msg; | |
}; | |
} | |
} | |
return wantarray ? @{$self->{__models}}{@names} : $self->{__models}{$names[0]}; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see also: https://github.com/issm/p5-Amon2-Plugin-Model