File Coverage

blib/lib/List/Objects/WithUtils/Role/Hash/Typed.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             package List::Objects::WithUtils::Role::Hash::Typed;
2             $List::Objects::WithUtils::Role::Hash::Typed::VERSION = '2.028003';
3 99     99   39777 use strictures 2;
  99         423  
  99         2986  
4              
5 99     99   12363 use Carp ();
  99         122  
  99         1041  
6 99     99   497 use Scalar::Util ();
  99         106  
  99         1050  
7 99     99   23320 use Type::Tie ();
  99         422115  
  99         1511  
8              
9 99     99   410 use Role::Tiny;
  99         103  
  99         474  
10             requires 'type', 'untyped', 'new';
11              
12             around type => sub { tied(%{$_[1]})->type };
13              
14             around untyped => sub {
15             my (undef, $self) = @_;
16             require List::Objects::WithUtils::Hash;
17             List::Objects::WithUtils::Hash->new(%$self)
18             };
19              
20             around new => sub {
21             my (undef, $class, $type) = splice @_, 0, 2;
22              
23             if (my $blessed = Scalar::Util::blessed $class) {
24             $type = $class->type;
25             $class = $blessed;
26             } else {
27             $type = shift;
28             }
29              
30             my $self = +{};
31             tie %$self, 'Type::Tie::HASH', $type;
32             %$self = @_;
33             bless $self, $class;
34             };
35              
36             1;
37              
38             =pod
39              
40             =for Pod::Coverage new hash_of
41              
42             =head1 NAME
43              
44             List::Objects::WithUtils::Role::Hash::Typed - Type-checking hash behavior
45              
46             =head1 SYNOPSIS
47              
48             # Via List::Objects::WithUtils::Hash::Typed ->
49             use List::Objects::WithUtils 'hash_of';
50             use Types::Standard -all;
51              
52             my $arr = hash_of(Int, foo => 1, bar => 2);
53             $arr->set(baz => 3.14159); # dies, failed type check
54              
55             =head1 DESCRIPTION
56              
57             This role makes use of L to add type-checking behavior to
58             L consumers.
59              
60             The first argument passed to the constructor should be a L type
61             (or other object conforming to L, as of C):
62              
63             use Types::Standard -all;
64             my $arr = hash_of ArrayRef() => (foo => [], bar => []);
65              
66             Values are checked against the specified type when the object is constructed
67             or new elements are added.
68              
69             If the initial type-check fails, a coercion is attempted.
70              
71             Values that cannot be coerced will throw an exception.
72              
73             Also see L, L
74              
75             =head2 type
76              
77             Returns the L type the object was created with.
78              
79             =head2 untyped
80              
81             Returns a (shallow) clone that is a plain L.
82              
83             =head1 AUTHOR
84              
85             Jon Portnoy ; typed hashes implemented by Toby Inkster
86             (CPAN: TOBYINK)
87              
88             =cut