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.027002';
3 99     99   66133 use strictures 2;
  99         577  
  99         3991  
4              
5 99     99   19553 use Carp ();
  99         179  
  99         1493  
6 99     99   710 use Scalar::Util ();
  99         181  
  99         1640  
7 99     99   39505 use Type::Tie ();
  99         627404  
  99         2088  
8              
9 99     99   625 use Role::Tiny;
  99         192  
  99         718  
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