File Coverage

blib/lib/WebService/MinFraud/Role/Model.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 33 33 100.0


line stmt bran cond sub pod time code
1             package WebService::MinFraud::Role::Model;
2              
3 4     4   1778 use Moo::Role;
  4         8  
  4         22  
4 4     4   1172 use namespace::autoclean;
  4         9  
  4         20  
5              
6             our $VERSION = '1.010000';
7              
8 4     4   324 use Sub::Quote qw( quote_sub );
  4         10  
  4         196  
9 4     4   24 use Types::Standard qw( HashRef );
  4         10  
  4         22  
10              
11             requires '_has';
12              
13             has raw => (
14             is => 'ro',
15             isa => HashRef,
16             required => 1,
17             );
18              
19             around BUILDARGS => sub {
20             my $orig = shift;
21             my $self = shift;
22              
23             my $p = $self->$orig(@_);
24             delete $p->{raw};
25              
26             # We make a copy to avoid a circular reference
27             $p->{raw} = { %{$p} };
28              
29             return $p;
30             };
31              
32             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
33             sub _define_model_attributes {
34 6     6   18 my $class = shift;
35 6         44 my %models = @_;
36              
37 6         31 my $has = $class->can('_has');
38              
39 6         23 for my $key ( keys %models ) {
40 34         29699 my $record_class = "WebService::MinFraud::Record::$models{$key}";
41              
42 34         72 my $raw_attr = '_raw_' . $key;
43              
44 34         96 $has->(
45             $raw_attr => (
46             is => 'ro',
47             isa => HashRef,
48             init_arg => $key,
49             default => quote_sub(q{ {} }),
50             ),
51             );
52              
53             ## no critic (ProhibitCallsToUnexportedSubs, RequireExplicitInclusion)
54             $has->(
55             $key => (
56             is => 'ro',
57             isa => quote_sub(
58             sprintf(
59             q{ WebService::MinFraud::Types::object_isa_type( $_[0], %s ) },
60             B::perlstring($record_class)
61             )
62             ),
63             init_arg => undef,
64             lazy => 1,
65             default => quote_sub(
66             sprintf(
67             q{ $_[0]->_build_record( %s, %s ) },
68 34         10435 map { B::perlstring($_) } $record_class, $raw_attr
  68         2216  
69             )
70             ),
71             ),
72             );
73             }
74             }
75             ## use critic
76              
77             sub _build_record {
78 37     37   94684 my $self = shift;
79 37         75 my $record_class = shift;
80 37         59 my $method = shift;
81              
82 37         129 my $raw = $self->$method;
83              
84 37         62 return $record_class->new( %{$raw}, locales => $self->locales );
  37         571  
85             }
86              
87             1;
88              
89             # ABSTRACT: A role for storing there original response in the raw attribute
90              
91             __END__
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             WebService::MinFraud::Role::Model - A role for storing there original response in the raw attribute
100              
101             =head1 VERSION
102              
103             version 1.010000
104              
105             =head1 SUPPORT
106              
107             Bugs may be submitted through L<https://github.com/maxmind/minfraud-api-perl/issues>.
108              
109             =head1 AUTHOR
110              
111             Mateu Hunter <mhunter@maxmind.com>
112              
113             =head1 COPYRIGHT AND LICENSE
114              
115             This software is copyright (c) 2015 - 2020 by MaxMind, Inc.
116              
117             This is free software; you can redistribute it and/or modify it under
118             the same terms as the Perl 5 programming language system itself.
119              
120             =cut