File Coverage

blib/lib/Dancer/Plugin/CORS/Sharing.pm
Criterion Covered Total %
statement 9 27 33.3
branch 0 6 0.0
condition 0 6 0.0
subroutine 3 7 42.8
pod 4 4 100.0
total 16 50 32.0


line stmt bran cond sub pod time code
1             package Dancer::Plugin::CORS::Sharing;
2              
3 8     8   47 use Modern::Perl;
  8         13  
  8         56  
4 8     8   968 use Carp;
  8         15  
  8         638  
5 8     8   55 use Scalar::Util qw(blessed);
  8         16  
  8         3898  
6              
7             =head1 NAME
8              
9             Dancer::Plugin::CORS::Sharing - Helper class for I keyword
10              
11             =head1 VERSION
12              
13             Version 0.01
14              
15             =cut
16              
17             our $VERSION = '0.01';
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 0     0 1   my $class = shift;
52 0           my %options = (rules => []);
53 0 0 0       if (blessed $class and $class->isa(__PACKAGE__)) {
54 0           %options = (%$class, %options);
55             }
56 0           %options = (%options, @_);
57 0 0         croak "sharing->new should be called inside a dancer app, not outside" unless exists $options{_add_rule};
58 0   0       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 0     0 1   my ($self, %options) = @_;
69 0           push @{$self->{rules}} => \%options;
  0            
70             }
71              
72             =head2 add(@routes)
73              
74             This method finally calls L for any route. @routes maybe a list of arrayrefs of L objects or paths.
75              
76             Note: L returns a hash instead of a list. Use values() to obtain the route objects:
77              
78             sharing->add(values(resource(...)));
79              
80             =cut
81              
82             sub add {
83 0     0 1   my ($self, @routes) = @_;
84 0           foreach my $routes (@routes) {
85 0 0         $routes = [ $routes ] unless ref $routes eq 'ARRAY';
86 0           foreach my $route (@$routes) {
87 0           foreach my $options (@{$self->{rules}}) {
  0            
88 0           $self->{_add_rule}->($route, %$options);
89             }
90             }
91             }
92             }
93              
94             =head2 clear
95              
96             This method clears all previously defined rules.
97              
98             =cut
99              
100             sub clear {
101 0     0 1   shift->{rules} = [];
102             }
103              
104             =head1 AUTHOR
105              
106             David Zurborg, C<< >>
107              
108             =head1 SEE ALSO
109              
110             =over
111              
112             =item L
113              
114             =back
115              
116             =head1 COPYRIGHT & LICENSE
117              
118             Copyright 2014 David Zurborg, all rights reserved.
119              
120             This program is released under the following license: open-source
121              
122             =cut
123              
124             1;