blib/lib/MooseX/Types/Data/Serializer.pm

filter: Statementstmt 90.9% 40 / 44 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 91.3% 53 / 58 CC 1 SCAR 0.0
CC
1
Cov
89%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package MooseX::Types::Data::Serializer;
2 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.05';
6  
7 =encoding utf8
8  
9 =head1 NAME
10  
11 MooseX::Types::Data::Serializer - A Data::Serializer type library for Moose.
12  
13 =head1 SYNOPSIS
14  
15 package MyClass;
16 use Moose;
17 use MooseX::Types::Data::Serializer;
18
19 has serializer => (
20 is => 'ro',
21 isa => 'Data::Serializer',
22 required => 1,
23 coerce => 1,
24 );
25
26 has raw_serializer => (
27 is => 'ro',
28 isa => 'Data::Serializer::Raw',
29 required => 1,
30 coerce => 1,
31 );
32
33 # String will be coerced in to a Data::Serializer object:
34 MyClass->new(
35 serializer => 'YAML',
36 raw_serializer => 'Storable',
37 );
38
39 # Hashref will be coerced as well:
40 MyClass->new(
41 serializer => { serializer => 'YAML', digester => 'MD5' },
42 raw_serializer => { serializer => 'Storable' },
43 );
44
45 use MooseX::Types::Data::Serializer qw( Serializer RawSerializer );
46 my $serializer = to_Serializer( 'YAML' );
47 my $raw_serializer = to_RawSerializer({ serializer=>'Storable', digester=>'MD5' });
48 if (is_Serializer($serializer)) { ... }
49 if (is_RawSerializer($raw_serializer)) { ... }
50  
51 =head1 DESCRIPTION
52  
53 This module provides L<Data::Serializer> types and coercians for L<Moose> attributes.
54  
55 Two standard Moose types are provided; Data::Serializer and Data::Serializer::Raw.
56 In addition, two other MooseX::Types types are provided; Serializer and RawSerializer.
57  
58 See the L<MooseX::Types> documentation for details on how that works.
59  
60 =head1 TYPES
61  
62 =head2 Data::Serializer
63  
64 This is a standard Moose type that provides coercion from a string or a hashref. If
65 a string is passed then it is used for the 'serializer' argumen to Data::Serializer->new().
66 If a hashref is being coerced from then it will be de-referenced and used as the
67 arguments to Data::Serializer->new().
68  
69 =head2 Data::Serializer::Raw
70  
71 This type works just like Data::Serializer, but for the L<Data::Serializer::Raw> module.
72  
73 =head2 Serializer
74  
75 This is a L<MooseX::Types> type that works just like the Data::Serializer type.
76  
77 =head2 RawSerializer
78  
79 Just like the Serializer type, but for Data::Serializer::Raw.
80  
81 =cut
82  
83 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use Data::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use Data::Serializer::Raw;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 1 ▶use MooseX::Types -declare => [qw( Serializer RawSerializer )];
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 1 ▶use MooseX::Types::Moose qw( Str HashRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 1 class_type 'Data::Serializer';
92 1 class_type 'Data::Serializer::Raw';
93  
94 1 subtype Serializer, as 'Data::Serializer';
95 1 subtype RawSerializer, as 'Data::Serializer::Raw';
96  
97 1 foreach my $type ('Data::Serializer', Serializer) {
98 coerce $type,
99 1 ▶ from Str, via { Data::Serializer->new( serializer => $_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
100 1 ▶ from HashRef, via { Data::Serializer->new( %$_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
101 }
102  
103 1 foreach my $type ('Data::Serializer::Raw', RawSerializer) {
104 coerce $type,
105 1 ▶ from Str, via { Data::Serializer::Raw->new( serializer => $_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
106 1 ▶ from HashRef, via { Data::Serializer::Raw->new( %$_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
107 }
108  
109 1 eval { require MooseX::Getopt; };
110 1 ▶if ( !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 1 1
111 0 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( 'Data::Serializer', '=s' );
112 0 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( 'Data::Serializer::Raw', '=s' );
113 0 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( Serializer, '=s' );
114 0 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( RawSerializer, '=s' );
115 }
116  
117 1 1;
118 __END__
↑ Summary