File Coverage

lib/Transmission/Types.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             # ex:ts=4:sw=4:sts=4:et
2             package Transmission::Types;
3             # See Transmission::Client for copyright statement.
4              
5             =head1 NAME
6              
7             Transmission::Types - Moose types for Transmission
8              
9             =head1 DESCRIPTION
10              
11             The types below is pretty much what you would expect them to be, execpt
12             for some (maybe weird?) default values - that is for coercion from "Any".
13              
14             The type names correspond to types used in the Transmission RPC
15             specification.
16              
17             =head1 TYPES
18              
19             =head2 number
20              
21             =head2 double
22              
23             =head2 string
24              
25             =head2 boolean
26              
27             =head2 array
28              
29             =cut
30              
31 2     2   36107 use MooseX::Types -declare => [qw/number double string boolean array/];
  0            
  0            
32             use MooseX::Types::Moose ':all';
33             use B;
34              
35             # If Perl thinks a value is a string, JSON will encode it as such. But
36             # Transmission is picky about how parameters are encoded in the JSON
37             # request, so we make sure Perl knows how to store numeric types.
38             sub _coerce_num {
39             local $_ = shift;
40             return -1 unless defined $_ and /^[0-9]+(?:\.[0-9]+)?$/;
41             return 0+$_;
42             }
43              
44             sub _is_num {
45             my $sv = shift;
46             my $flags = B::svref_2object(\$sv)->FLAGS;
47             return $flags & (B::SVp_NOK | B::SVp_IOK) and not $flags & B::SVp_POK;
48             }
49              
50             subtype number, as Num, where { _is_num($_) and $_ == int $_};
51             coerce number, from Any, via { int _coerce_num($_) };
52              
53             subtype double, as Num, where { _is_num($_) };
54             coerce double, from Any, via { _coerce_num($_) };
55              
56             subtype string, as Str;
57             coerce string, from Any, via { defined $_ ? "$_" : "__UNDEF__" };
58              
59             type boolean, where { defined $_ and $_ =~ /^(1|0)$/ };
60             coerce boolean, from Object, via { int $_ };
61              
62             subtype array, as ArrayRef;
63             coerce array, from Any, via { [] };
64              
65             =head1 LICENSE
66              
67             =head1 NAME
68              
69             See L<Transmission::Client>
70              
71             =cut
72              
73             1;