File Coverage

blib/lib/Dancer/Plugin/CORS/Sharing.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 6 66.6
condition 4 6 66.6
subroutine 7 7 100.0
pod 4 4 100.0
total 50 54 92.5


line stmt bran cond sub pod time code
1             package Dancer::Plugin::CORS::Sharing;
2              
3 11     11   49 use Modern::Perl;
  11         14  
  11         48  
4 11     11   1130 use Carp;
  11         15  
  11         565  
5 11     11   53 use Scalar::Util qw(blessed);
  11         14  
  11         3974  
6              
7             =head1 NAME
8              
9             Dancer::Plugin::CORS::Sharing - Helper class for I keyword
10              
11             =head1 VERSION
12              
13             Version 0.11
14              
15             =cut
16              
17             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     2 1 3 my $class = shift;
52 2         5 my %options = (rules => []);
53 2 100 66     18 if (blessed $class and $class->isa(__PACKAGE__)) {
54 1         4 %options = (%$class, %options);
55             }
56 2         22 %options = (%options, @_);
57 2 50       6 croak "sharing->new should be called inside a dancer app, not outside" unless exists $options{_add_rule};
58 2   66     20 return bless \%options => ref $class || $class;
59             }
60              
61             =head2 rule(%options)
62              
63             This method defines a optionset. See L for a explaination of valid options.
64              
65             =cut
66              
67             sub rule($%) {
68 3     3 1 10 my ($self, %options) = @_;
69 3         2 push @{$self->{rules}} => \%options;
  3         5  
70 3         6 $self;
71             }
72              
73             =head2 add(@routes)
74              
75             This method finally calls L for any route. @routes maybe a list of arrayrefs of L objects or paths.
76              
77             Note: L 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     4 1 899 my ($self, @routes) = @_;
85 4         5 foreach my $routes (@routes) {
86 5 50       21 $routes = [ $routes ] unless ref $routes eq 'ARRAY';
87 5         7 foreach my $route (@$routes) {
88 5         5 foreach my $options (@{$self->{rules}}) {
  5         8  
89 7         22 $self->{_add_rule}->($route, %$options);
90             }
91             }
92             }
93 4         8 $self;
94             }
95              
96             =head2 clear
97              
98             This method clears all previously defined rules.
99              
100             =cut
101              
102             sub clear {
103 1     1 1 2 my $self = shift;
104 1         2 $self->{rules} = [];
105 1         3 $self;
106             }
107              
108             =head1 AUTHOR
109              
110             David Zurborg, C<< >>
111              
112             =head1 SEE ALSO
113              
114             =over
115              
116             =item L
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             1;