CORS/Sharing.pm

filter: Statementstmt 100% 33 / 33 Branchbran 66.6% 4 / 6 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 4 / 4 totaltotal 88.3% 53 / 60 CC 9 SCAR 13.1
CC
9
Cov
91%
new
5.1
add
5.0
BEGIN
1.0
CRAP 3.7
« CORS.pm ↑ Summary
1 package Dancer::Plugin::CORS::Sharing;
2  
3 11 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Dancer::Plugin::CORS::Sharing - Helper class for I<sharing> keyword
10  
11 =head1 VERSION
12  
13 Version 0.11
14  
15 =cut
16  
17 11 our $VERSION = '0.11';
18  
19 =head1 DESCRIPTION
20  
21 In order to use many rules with many routes, this helpers class helps you to organize yourself.
22  
23 =head1 SYNOPSIS
24  
25 use Dancer::Plugin::CORS;
26  
27 sharing->rule(
28 origin => ...,
29 credentials => 1
30 );
31
32 $route = post '/' => sub { ... };
33
34 sharing->add($route);
35  
36 =head1 METHODS
37
38 =head2 new
39  
40 A convient way is to use the implicit form of the module. This means you don't have to call new() self, just start with defining rules and add routes.
41  
42 When you want more than one ruleset, obtain a new instance by calling new():
43  
44 my $sharing = sharing->new;
45 $sharing->rule(...);
46 $sharing->add(...);
47
48 =cut
49  
50 sub new($%) {
51 2 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.4
Pod documented
52 2 my %options = (rules => []);
53 2 ▶ if (blessed $class and $class->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($class) and $class->isa(__PACKAGE__)) 1 1 2
Condition66%2/3
builtin::blessed($class) and $class->isa(__PACKAGE__)
!ll&&!rl&&r
101
MC/DC50%1/2
builtin::blessed($class) and $class->isa(__PACKAGE__)
builtin::blessed($class) $class->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($class)B= $class->isa(__PACKAGE__)
ABresult
0X0
100
111
54 1 %options = (%$class, %options);
55 }
56 2 %options = (%options, @_);
57 2 ▶ croak "sharing->new should be called inside a dancer app, not outside" unless exists $options{_add_rule};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"_add_rule"} 0 2 2
58 2 ▶ return bless \%options => ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
59 }
60  
61 =head2 rule(%options)
62  
63 This method defines a optionset. See L<Dancer::Plugin::CORS::share> for a explaination of valid options.
64  
65 =cut
66  
67 sub rule($%) {
68 3 ▶ my ($self, %options) = @_;
Subroutine rule: called CC=1 SCAR=0.0
Pod documented
69 3 push @{$self->{rules}} => \%options;
70 3 $self;
71 }
72  
73 =head2 add(@routes)
74  
75 This method finally calls L<Dancer::Plugin::CORS::share> for any route. @routes maybe a list of arrayrefs of L<Dancer::Route> objects or paths.
76  
77 Note: L<Dancer::Plugin::CRUD::resource> returns a hash instead of a list. Use values() to obtain the route objects:
78  
79 sharing->add(values(resource(...)));
80  
81 =cut
82  
83 sub add {
84 4 ▶ my ($self, @routes) = @_;
Subroutine add: called CC=5 SCAR=16.1
Pod documented
85 4 foreach my $routes (@routes) {
86 5 ▶ $routes = [ $routes ] unless ref $routes eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $routes eq "ARRAY" 5 0 5
87 5 foreach my $route (@$routes) {
88 5 foreach my $options (@{$self->{rules}}) {
89 7 $self->{_add_rule}->($route, %$options);
90 }
91 }
92 }
93 4 $self;
94 }
95  
96 =head2 clear
97  
98 This method clears all previously defined rules.
99  
100 =cut
101  
102 sub clear {
103 1 ▶ my $self = shift;
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
104 1 $self->{rules} = [];
105 1 $self;
106 }
107  
108 =head1 AUTHOR
109  
110 David Zurborg, C<< <zurborg@cpan.org> >>
111  
112 =head1 SEE ALSO
113  
114 =over
115  
116 =item L<Dancer::Plugin::CORS>
117  
118 =back
119  
120 =head1 COPYRIGHT & LICENSE
121  
122 Copyright 2014 David Zurborg, all rights reserved.
123  
124 This program is released under the following license: open-source
125  
126 =cut
127  
128 11 1;
« CORS.pm ↑ Summary