File Coverage

blib/lib/MooseX/Types/Data/Serializer.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 31 31 100.0


line stmt bran cond sub pod time code
1             package MooseX::Types::Data::Serializer;
2 1     1   211583 use 5.008001;
  1         3  
3 1     1   5 use strict;
  1         2  
  1         18  
4 1     1   4 use warnings;
  1         2  
  1         51  
5             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     1   532 use Moose::Util::TypeConstraints;
  1         249026  
  1         8  
84 1     1   1698 use Data::Serializer;
  1         3  
  1         24  
85 1     1   461 use Data::Serializer::Raw;
  1         690  
  1         36  
86              
87 1     1   497 use MooseX::Types -declare => [qw( Serializer RawSerializer )];
  1         182872  
  1         6  
88              
89 1     1   4445 use MooseX::Types::Moose qw( Str HashRef );
  1         14240  
  1         6  
90              
91             class_type 'Data::Serializer';
92             class_type 'Data::Serializer::Raw';
93              
94             subtype Serializer, as 'Data::Serializer';
95             subtype RawSerializer, as 'Data::Serializer::Raw';
96              
97             foreach my $type ('Data::Serializer', Serializer) {
98             coerce $type,
99             from Str, via { Data::Serializer->new( serializer => $_ ) },
100             from HashRef, via { Data::Serializer->new( %$_ ) };
101             }
102              
103             foreach my $type ('Data::Serializer::Raw', RawSerializer) {
104             coerce $type,
105             from Str, via { Data::Serializer::Raw->new( serializer => $_ ) },
106             from HashRef, via { Data::Serializer::Raw->new( %$_ ) };
107             }
108              
109             eval { require MooseX::Getopt; };
110             if ( !$@ ) {
111             MooseX::Getopt::OptionTypeMap->add_option_type_to_map( 'Data::Serializer', '=s' );
112             MooseX::Getopt::OptionTypeMap->add_option_type_to_map( 'Data::Serializer::Raw', '=s' );
113             MooseX::Getopt::OptionTypeMap->add_option_type_to_map( Serializer, '=s' );
114             MooseX::Getopt::OptionTypeMap->add_option_type_to_map( RawSerializer, '=s' );
115             }
116              
117             1;
118             __END__
119              
120             =head1 AUTHOR
121              
122             Aran Clary Deltac <bluefeet@gmail.com>
123              
124             =head1 LICENSE
125              
126             This library is free software; you can redistribute it and/or modify
127             it under the same terms as Perl itself.
128              
129             =cut
130