File Coverage

blib/lib/Lim/RPC/Value.pm
Criterion Covered Total %
statement 27 49 55.1
branch 11 32 34.3
condition 2 6 33.3
subroutine 7 10 70.0
pod 6 6 100.0
total 53 103 51.4


line stmt bran cond sub pod time code
1             package Lim::RPC::Value;
2              
3 7     7   40 use common::sense;
  7         18  
  7         48  
4 7     7   322 use Carp;
  7         14  
  7         507  
5              
6 7     7   41 use Lim ();
  7         22  
  7         13703  
7              
8             =encoding utf8
9              
10             =head1 NAME
11              
12             ...
13              
14             =head1 VERSION
15              
16             See L for version.
17              
18             =over 4
19              
20             =item STRING
21              
22             =item INTEGER
23              
24             =item BOOL
25              
26             =item BASE64
27              
28             =item OPT_REQUIRED
29              
30             =item OPT_NOTEMPTY
31              
32             =back
33              
34             =cut
35              
36             our $VERSION = $Lim::VERSION;
37              
38             sub STRING (){ 'string' }
39             sub INTEGER (){ 'integer' }
40             sub BOOL (){ 'bool' }
41             sub BASE64 (){ 'base64' }
42              
43             our %TYPE = (
44             STRING() => STRING,
45             INTEGER() => INTEGER,
46             BOOL() => BOOL,
47             BASE64() => BASE64
48             );
49             our %XSD_TYPE = (
50             STRING() => 'xsd:string',
51             INTEGER() => 'xsd:integer',
52             BOOL() => 'xsd:boolean',
53             BASE64() => 'xsd:base64Binary'
54             );
55             our %XMLRPC_TYPE = (
56             STRING() => 'string',
57             INTEGER() => 'int',
58             BOOL() => 'boolean',
59             BASE64() => 'base64'
60             );
61              
62             sub OPT_REQUIRED (){ 0x00000001 }
63             sub OPT_NOTEMPTY (){ 0x00000002 }
64              
65             our %OPTIONS = (
66             'required' => OPT_REQUIRED,
67             'notEmpty' => OPT_NOTEMPTY
68             );
69              
70             our %NEGATIVE_OPTIONS = (
71             'optional' => OPT_REQUIRED,
72             'empty' => OPT_NOTEMPTY
73             );
74              
75             =head1 SYNOPSIS
76              
77             ...
78              
79             =head1 SUBROUTINES/METHODS
80              
81             =head2 new
82              
83             =cut
84              
85             sub new {
86 36     36 1 91 my $this = shift;
87 36   33     153 my $class = ref($this) || $this;
88 36 50       199 my %args = scalar @_ > 1 ? ( @_ ) : ( textual => $_[0] );
89 36         89 my $self = {
90             options => OPT_REQUIRED | OPT_NOTEMPTY
91             };
92            
93 36 50       91 if (exists $args{textual}) {
94 36         118 foreach (split(/\s+/o, lc($args{textual}))) {
95 40 100       112 if (exists $TYPE{$_}) {
    50          
    50          
96 36 50       93 if (exists $self->{type}) {
97 0         0 confess __PACKAGE__, ': type already defined';
98             }
99 36         128 $self->{type} = $_;
100             }
101             elsif (exists $OPTIONS{$_}) {
102 0         0 $self->{options} |= $OPTIONS{$_};
103             }
104             elsif (exists $NEGATIVE_OPTIONS{$_}) {
105 4         17 $self->{options} &= ~$NEGATIVE_OPTIONS{$_};
106             }
107             else {
108 0         0 confess __PACKAGE__, ': unknown RPC value setting "'.$_.'"';
109             }
110             }
111             }
112             else {
113 0 0       0 unless (defined $args{type}) {
114 0         0 confess __PACKAGE__, ': No type specified';
115             }
116 0 0       0 unless (exists $TYPE{$args{type}}) {
117 0         0 confess __PACKAGE__, ': Invalid type specified';
118             }
119            
120 0         0 $self->{type} = $args{type};
121              
122 0 0       0 if (defined $args{options}) {
123 0 0       0 unless (ref($args{options}) eq 'ARRAY') {
124 0         0 confess __PACKAGE__, ': Invalid options specified';
125             }
126            
127 0         0 foreach (@{$args{options}}) {
  0         0  
128 0 0       0 if (exists $OPTIONS{$_}) {
    0          
129 0         0 $self->{options} |= $OPTIONS{$_};
130             }
131             elsif (exists $NEGATIVE_OPTIONS{$_}) {
132 0         0 $self->{options} &= ~$NEGATIVE_OPTIONS{$_};
133             }
134             else {
135 0         0 confess __PACKAGE__, ': Unknown RPC value option "'.$_.'"';
136             }
137             }
138             }
139             }
140            
141 36 50       102 unless (exists $self->{type}) {
142 0         0 confess __PACKAGE__, ': no type defined';
143             }
144              
145 36         208 bless $self, $class;
146             }
147              
148             sub DESTROY {
149 36     36   198 my ($self) = @_;
150             }
151              
152             =head2 type
153              
154             =cut
155              
156             sub type {
157 0     0 1 0 $_[0]->{type};
158             }
159              
160             =head2 xsd_type
161              
162             =cut
163              
164             sub xsd_type {
165 0     0 1 0 $XSD_TYPE{$_[0]->{type}};
166             }
167              
168             =head2 xmlrpc_type
169              
170             =cut
171              
172             sub xmlrpc_type {
173 0     0 1 0 $XMLRPC_TYPE{$_[0]->{type}};
174             }
175              
176             =head2 required
177              
178             =cut
179              
180             sub required {
181 2 50   2 1 48 $_[0]->{options} & OPT_REQUIRED ? 1 : 0;
182             }
183              
184             =head2 comform
185              
186             =cut
187              
188             sub comform {
189 2     2 1 8 my ($self, $value) = @_;
190            
191             # TODO validate type
192            
193 2 50       13 if (($self->{options} & OPT_NOTEMPTY)) {
194 2 50 33     103 if (!defined $value or $value !~ /$\s*^/o) {
195 0         0 return 0;
196             }
197             }
198            
199 2         54 return 1;
200             }
201              
202             =head1 AUTHOR
203              
204             Jerry Lundström, C<< >>
205              
206             =head1 BUGS
207              
208             Please report any bugs or feature requests to L.
209              
210             =head1 SUPPORT
211              
212             You can find documentation for this module with the perldoc command.
213              
214             perldoc Lim
215              
216             You can also look for information at:
217              
218             =over 4
219              
220             =item * Lim issue tracker (report bugs here)
221              
222             L
223              
224             =back
225              
226             =head1 ACKNOWLEDGEMENTS
227              
228             =head1 LICENSE AND COPYRIGHT
229              
230             Copyright 2012-2013 Jerry Lundström.
231              
232             This program is free software; you can redistribute it and/or modify it
233             under the terms of either: the GNU General Public License as published
234             by the Free Software Foundation; or the Artistic License.
235              
236             See http://dev.perl.org/licenses/ for more information.
237              
238              
239             =cut
240              
241             1; # End of Lim::RPC::Value