File Coverage

blib/lib/Dancer/Plugin/Adapter.pm
Criterion Covered Total %
statement 18 37 48.6
branch 0 20 0.0
condition 0 7 0.0
subroutine 6 7 85.7
pod n/a
total 24 71 33.8


line stmt bran cond sub pod time code
1 4     4   1946650 use 5.008001;
  4         16  
  4         158  
2 4     4   87 use strict;
  4         8  
  4         126  
3 4     4   23 use warnings;
  4         6  
  4         288  
4              
5             package Dancer::Plugin::Adapter;
6             # ABSTRACT: Wrap any simple class as a service for Dancer
7             our $VERSION = '0.004'; # VERSION
8              
9 4     4   3583 use Dancer::Plugin;
  4         6418  
  4         447  
10 4     4   32 use Dancer ':syntax';
  4         8  
  4         31  
11 4     4   6429 use Class::Load qw/try_load_class/;
  4         108069  
  4         2768  
12              
13             my %singletons;
14             my $conf;
15              
16             my %save_by_scope = (
17             singleton => sub { $singletons{ $_[0] } = $_[1] },
18             session => sub {
19             my $hr = session("_dpa") || {};
20             $hr->{ $_[0] } = $_[1];
21             session( "_dpa", $hr );
22             },
23             request => sub {
24             my $hr = var("_dpa") || {};
25             $hr->{ $_[0] } = $_[1];
26             var( "_dpa", $hr );
27             },
28             none => sub { },
29             );
30              
31             my %fetch_by_scope = (
32             singleton => sub { $singletons{ $_[0] } },
33             session => sub { my $hr = session("_dpa") || {}; $hr->{ $_[0] }; },
34             request => sub { my $hr = var("_dpa") || {}; $hr->{ $_[0] }; },
35             none => sub { },
36             );
37              
38             register service => sub {
39 0     0     my ( $self, $name ) = plugin_args(@_);
40              
41 0 0         unless ($name) {
42 0           die "Dancer::Plugin::Adapter::service() requires a name argumenet";
43             }
44              
45 0   0       $conf ||= plugin_setting();
46              
47             # ensure service is defined
48 0 0         my $object_conf = $conf->{$name}
49             or die "No configuration for Adapter '$name'";
50              
51             # set scope, but default to 'request' if not set
52 0   0       my $scope = $conf->{$name}{scope} || 'request';
53 0 0         unless ( $fetch_by_scope{$scope} ) {
54 0           die "Scope '$scope' is invalid";
55             }
56              
57             # return cached object if already created
58 0           my $cached = $fetch_by_scope{$scope}->($name);
59 0 0         return $cached if defined $cached;
60              
61             # otherwise, instantiate the object from config settings
62 0 0         my $class = $object_conf->{class}
63             or die "No class specified for Adapter '$name'";
64              
65 0 0         try_load_class($class)
66             or die "Module '$class' could not be loaded";
67              
68 0   0       my $new = $object_conf->{constructor} || 'new';
69 0           my $options = $object_conf->{options};
70              
71 0 0         my @options =
    0          
    0          
72             ref($options) eq 'HASH' ? %$options
73             : ref($options) eq 'ARRAY' ? @$options
74             : defined($options) ? $options
75             : ();
76              
77 0 0         my $object = eval { $class->$new(@options) }
  0            
78             or die "Could not create $class object: $@";
79              
80             # cache by scope
81 0           $save_by_scope{$scope}->( $name, $object );
82 0           return $object;
83             };
84              
85             register_plugin for_versions => [ 1, 2 ];
86              
87             1;
88              
89              
90             # vim: ts=2 sts=2 sw=2 et:
91              
92             __END__