File Coverage

blib/lib/Dancer2/Plugin/Adapter.pm
Criterion Covered Total %
statement 31 33 93.9
branch 13 20 65.0
condition 6 7 85.7
subroutine 6 6 100.0
pod n/a
total 56 66 84.8


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