File Coverage

blib/lib/MooseX/Types/Data/Serializer.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             package MooseX::Types::Data::Serializer;
2              
3             $MooseX::Types::Data::Serializer::VERSION = '0.03';
4              
5             =head1 NAME
6              
7             MooseX::Types::Data::Serializer - A Data::Serializer type library for Moose.
8              
9             =head1 SYNOPSIS
10              
11             package MyClass;
12             use Moose;
13             use MooseX::Types::Data::Serializer;
14            
15             has serializer => (
16             is => 'ro',
17             isa => 'Data::Serializer',
18             required => 1,
19             coerce => 1,
20             );
21            
22             has raw_serializer => (
23             is => 'ro',
24             isa => 'Data::Serializer::Raw',
25             required => 1,
26             coerce => 1,
27             );
28            
29             # String will be coerced in to a Data::Serializer object:
30             MyClass->new(
31             serializer => 'YAML',
32             raw_serializer => 'Storable',
33             );
34            
35             # Hashref will be coerced as well:
36             MyClass->new(
37             serializer => { serializer => 'YAML', digester => 'MD5' },
38             raw_serializer => { serializer => 'Storable' },
39             );
40            
41             use MooseX::Types::Data::Serializer qw( Serializer RawSerializer );
42             my $serializer = to_Serializer( 'YAML' );
43             my $raw_serializer = to_RawSerializer({ serializer=>'Storable', digester=>'MD5' });
44             if (is_Serializer($serializer)) { ... }
45             if (is_RawSerializer($raw_serializer)) { ... }
46              
47             =head1 DESCRIPTION
48              
49             This module provides L<Data::Serializer> types and coercians for L<Moose> attributes.
50              
51             Two standard Moose types are provided; Data::Serializer and Data::Serializer::Raw.
52             In addition, two other MooseX::Types types are provided; Serializer and RawSerializer.
53              
54             See the L<MooseX::Types> documentation for details on how that works.
55              
56             =head1 TYPES
57              
58             =head2 Data::Serializer
59              
60             This is a standard Moose type that provides coercion from a string or a hashref. If
61             a string is passed then it is used for the 'serializer' argumen to Data::Serializer->new().
62             If a hashref is being coerced from then it will be de-referenced and used as the
63             arguments to Data::Serializer->new().
64              
65             =head2 Data::Serializer::Raw
66              
67             This type works just like Data::Serializer, but for the L<Data::Serializer::Raw> module.
68              
69             =head2 Serializer
70              
71             This is a L<MooseX::Types> type that works just like the Data::Serializer type.
72              
73             =head2 RawSerializer
74              
75             Just like the Serializer type, but for Data::Serializer::Raw.
76              
77             =cut
78              
79 1     1   212451 use strict;
  1         3  
  1         29  
80 1     1   5 use warnings;
  1         2  
  1         38  
81              
82 1     1   652 use Moose::Util::TypeConstraints;
  1         286542  
  1         9  
83 1     1   2076 use Data::Serializer;
  1         4  
  1         29  
84 1     1   489 use Data::Serializer::Raw;
  1         813  
  1         42  
85              
86 1     1   533 use MooseX::Types -declare => [qw( Serializer RawSerializer )];
  1         226595  
  1         9  
87              
88 1     1   5604 use MooseX::Types::Moose qw( Str HashRef );
  1         17692  
  1         22  
89              
90             class_type 'Data::Serializer';
91             class_type 'Data::Serializer::Raw';
92              
93             subtype Serializer, as 'Data::Serializer';
94             subtype RawSerializer, as 'Data::Serializer::Raw';
95              
96             foreach my $type ('Data::Serializer', Serializer) {
97             coerce $type,
98             from Str, via { Data::Serializer->new( serializer => $_ ) },
99             from HashRef, via { Data::Serializer->new( %$_ ) };
100             }
101              
102             foreach my $type ('Data::Serializer::Raw', RawSerializer) {
103             coerce $type,
104             from Str, via { Data::Serializer::Raw->new( serializer => $_ ) },
105             from HashRef, via { Data::Serializer::Raw->new( %$_ ) };
106             }
107              
108             eval { require MooseX::Getopt; };
109             if ( !$@ ) {
110             MooseX::Getopt::OptionTypeMap->add_option_type_to_map( 'Data::Serializer', '=s' );
111             MooseX::Getopt::OptionTypeMap->add_option_type_to_map( 'Data::Serializer::Raw', '=s' );
112             MooseX::Getopt::OptionTypeMap->add_option_type_to_map( Serializer, '=s' );
113             MooseX::Getopt::OptionTypeMap->add_option_type_to_map( RawSerializer, '=s' );
114             }
115              
116             1;
117             __END__
118              
119             =head1 AUTHOR
120              
121             Aran Clary Deltac <bluefeet@gmail.com>
122              
123             =head1 LICENSE
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127