File Coverage

blib/lib/Venus/Role/Pluggable.pm
Criterion Covered Total %
statement 25 26 96.1
branch 4 6 66.6
condition n/a
subroutine 8 8 100.0
pod 0 3 0.0
total 37 43 86.0


line stmt bran cond sub pod time code
1             package Venus::Role::Pluggable;
2              
3 32     32   576 use 5.018;
  32         118  
4              
5 32     32   174 use strict;
  32         78  
  32         668  
6 32     32   161 use warnings;
  32         84  
  32         980  
7              
8 32     32   185 use Venus::Role 'fault';
  32         96  
  32         247  
9              
10             # AUDITS
11              
12             sub AUDIT {
13 32     32 0 87 my ($self, $from) = @_;
14              
15 32 50       328 if (!$from->does('Venus::Role::Proxyable')) {
16 0         0 fault "${self} requires ${from} to consume Venus::Role::Proxyable";
17             }
18              
19 32         101 return $self;
20             }
21              
22             # METHODS
23              
24             sub build_proxy {
25 2     2 0 4 my ($self, $package, $method, @args) = @_;
26              
27 2         627 require Venus::Space;
28              
29 2         11 my $space = Venus::Space->new($package)->child('plugin', $method);
30              
31 2 50       6 return undef if !$space->tryload;
32              
33             return sub {
34 2 100   2   15 if ($space->package->can('construct')) {
35 1         4 my $class = $space->load;
36              
37 1         29 return $class->construct($self, @args)->execute;
38             }
39             else {
40 1         4 my $class = $space->load;
41              
42 1         10 return $class->new->execute($self, @args);
43             }
44 2         13 };
45             }
46              
47             # EXPORTS
48              
49             sub EXPORT {
50 32     32 0 136 ['build_proxy']
51             }
52              
53             1;
54              
55              
56              
57             =head1 NAME
58              
59             Venus::Role::Pluggable - Pluggable Role
60              
61             =cut
62              
63             =head1 ABSTRACT
64              
65             Pluggable Role for Perl 5
66              
67             =cut
68              
69             =head1 SYNOPSIS
70              
71             package Example::Plugin::Username;
72              
73             use Venus::Class;
74              
75             use Digest::SHA ();
76              
77             sub execute {
78             my ($self, $example) = @_;
79              
80             return Digest::SHA::sha1_hex($example->login);
81             }
82              
83             package Example::Plugin::Password;
84              
85             use Venus::Class;
86              
87             use Digest::SHA ();
88              
89             attr 'value';
90              
91             sub construct {
92             my ($class, $example) = @_;
93              
94             return $class->new(value => $example->secret);
95             }
96              
97             sub execute {
98             my ($self) = @_;
99              
100             return Digest::SHA::sha1_hex($self->value);
101             }
102              
103             package Example;
104              
105             use Venus::Class;
106              
107             with 'Venus::Role::Proxyable';
108             with 'Venus::Role::Pluggable';
109              
110             attr 'login';
111             attr 'secret';
112              
113             package main;
114              
115             my $example = Example->new(login => 'admin', secret => 'p@ssw0rd');
116              
117             # $example->username;
118             # $example->password;
119              
120             =cut
121              
122             =head1 DESCRIPTION
123              
124             This package provides a mechanism for dispatching to plugin classes.
125              
126             =cut
127              
128             =head1 AUTHORS
129              
130             Awncorp, C
131              
132             =cut
133              
134             =head1 LICENSE
135              
136             Copyright (C) 2000, Al Newkirk.
137              
138             This program is free software, you can redistribute it and/or modify it under
139             the terms of the Apache license version 2.0.
140              
141             =cut