File Coverage

blib/lib/App/Dochazka/REST/Mason.pm
Criterion Covered Total %
statement 21 32 65.6
branch 0 4 0.0
condition 0 12 0.0
subroutine 7 10 70.0
pod 1 1 100.0
total 29 59 49.1


line stmt bran cond sub pod time code
1             # *************************************************************************
2             # Copyright (c) 2014-2015, SUSE LLC
3             #
4             # All rights reserved.
5             #
6             # Redistribution and use in source and binary forms, with or without
7             # modification, are permitted provided that the following conditions are met:
8             #
9             # 1. Redistributions of source code must retain the above copyright notice,
10             # this list of conditions and the following disclaimer.
11             #
12             # 2. Redistributions in binary form must reproduce the above copyright
13             # notice, this list of conditions and the following disclaimer in the
14             # documentation and/or other materials provided with the distribution.
15             #
16             # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17             # used to endorse or promote products derived from this software without
18             # specific prior written permission.
19             #
20             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30             # POSSIBILITY OF SUCH DAMAGE.
31             # *************************************************************************
32              
33             # ------------------------
34             # store and dispense Mason interpreter singleton
35             # ------------------------
36              
37             package App::Dochazka::REST::Mason;
38              
39 42     42   1322 use strict;
  42         93  
  42         1300  
40 42     42   239 use warnings;
  42         92  
  42         1237  
41              
42 42     42   226 use App::CELL qw( $CELL $log $site );
  42         103  
  42         3911  
43 42     42   18475 use Mason;
  42         46119111  
  42         1590  
44 42     42   546 use Params::Validate qw( :all );
  42         105  
  42         8678  
45 42     42   338 use Try::Tiny;
  42         113  
  42         2505  
46              
47              
48              
49             =head1 NAME
50              
51             App::Dochazka::REST::Mason - Mason interpreter singleton
52              
53              
54              
55             =head1 SYNOPSIS
56              
57             use App::Dochazka::REST::Mason qw( $interp );
58              
59              
60              
61             =head1 DESCRIPTION
62              
63             Mason interpreter singleton.
64              
65             =cut
66              
67              
68              
69             =head1 EXPORTS
70              
71             =cut
72              
73 42     42   278 use Exporter qw( import );
  42         114  
  42         13962  
74             our @EXPORT_OK = qw( $comp_root $interp );
75              
76              
77              
78             =head1 PACKAGE VARIABLES
79              
80             This module stores and initializes the L<Mason> interpreter singleton object.
81              
82             =cut
83              
84             our ( $comp_root, $interp );
85              
86              
87              
88             =head1 FUNCTIONS
89              
90              
91             =head2 init_singleton
92              
93             Initialize the C<$interp> singleton. Takes a C<comp_root> and a
94             C<data_dir> which are expected to exist and be owned by the effective user.
95              
96             Idempotent.
97              
98             FIXME: Add parameters to the Mason->new() call as needed.
99              
100             =cut
101              
102             sub init_singleton {
103 0     0 1   my @ARGS = @_;
104 0           my %ARGS;
105 0           my $status = $CELL->status_ok;
106             try {
107 0     0     %ARGS = validate(
108             @ARGS, {
109             comp_root => { type => SCALAR },
110             data_dir => { type => SCALAR },
111             }
112             );
113             die "Mason comp_root $ARGS{comp_root} has a problem" unless
114             (
115             -r $ARGS{comp_root} and
116             -w $ARGS{comp_root} and
117             -x $ARGS{comp_root}
118 0 0 0       );
      0        
119             die "Mason comp_root $ARGS{data_dir} has a problem" unless
120             (
121             -r $ARGS{comp_root} and
122             -w $ARGS{comp_root} and
123             -x $ARGS{comp_root}
124 0 0 0       );
      0        
125             $interp = Mason->new(
126             comp_root => $ARGS{comp_root},
127             data_dir => $ARGS{data_dir},
128 0           class_header => q(
129             use App::CELL qw( $CELL $log $meta $site );
130             ),
131             );
132 0           $comp_root = $ARGS{comp_root};
133             } catch {
134 0     0     $status = $CELL->status_crit( 'DOCHAZKA_MASON_INIT_FAIL', args => [ $_ ] );
135 0           };
136 0           return $status;
137             }
138              
139             1;