File Coverage

blib/lib/Data/Result.pm
Criterion Covered Total %
statement 31 33 93.9
branch 3 4 75.0
condition n/a
subroutine 12 14 85.7
pod 3 6 50.0
total 49 57 85.9


line stmt bran cond sub pod time code
1             package Data::Result;
2              
3 2     2   586 use Modern::Perl;
  2         5  
  2         18  
4 2     2   765 use Moo;
  2         10432  
  2         10  
5 2     2   1862 use MooX::Types::MooseLike::Base qw(:all);
  2         10167  
  2         550  
6 2     2   17 use Carp qw(croak);
  2         5  
  2         99  
7 2     2   707 use namespace::clean;
  2         18629  
  2         14  
8              
9             use overload
10             fallback=>1,
11 3     3   1263 '""'=>sub { $_[0]->msg },
12 9     9   602 bool=>sub { $_[0]->is_true },
13 2     2   2593 ;
  2         804  
  2         19  
14              
15             our $VERSION='1.0001';
16              
17             =head1 NAME
18              
19             Data::Result - Handling true and false in a better way!
20              
21             =head1 SYNOPSIS
22              
23             use Modern::Perl;
24             use Data::Result;
25              
26             # just true
27             my $result=Data::Result->new(is_true=>0);
28             if($result) {
29             print "Yup its true!\n";
30             }
31              
32             # True with data
33             $result=Data::Result->new(is_true=>1,data=>'Yup This is true!');
34             if($result) {
35             print $result->data,"\n";
36             }
37              
38             # just flase
39             $result=Data::Result->new(is_true=>0);
40             if($result) {
41             print $result->data,"\n";
42             } else {
43             print "well, something went wrong!\n";
44             }
45              
46             # handle false, but give us an error!
47             $result=Data::Result->new(is_true=>0,msg=>'this is our message');
48             if($result) {
49             print $result->data,"\n";
50             } else {
51             print "$result\n";
52             }
53              
54             =head1 DESCRIPTION
55              
56             Handling true and false isn't always enough. This alows true and false to encapsulate things as a simple state.
57              
58             =cut
59              
60             # This method runs after the new constructor
61             sub BUILD {
62 13     13 0 1424 my ($self)=@_;
63             }
64              
65             # this method runs before the new constructor, and can be used to change the arguments passed to the module
66             around BUILDARGS => sub {
67             my ($org,$class,@args)=@_;
68            
69             return $class->$org(@args);
70             };
71              
72              
73             =head1 Object Constructor Arguments
74              
75             Data::Result provides the following constructor arguments
76              
77             Required arguments:
78              
79             is_true: true or fale
80              
81             Optional arguments
82              
83             data: Data this object contains
84             msg: A human readable string representing this object.
85             extra: another slot to put data in
86              
87             =cut
88              
89             has is_true=>(
90             is=>'rw',
91             isa=>Bool,
92             required=>1,
93             );
94              
95             has msg=>(
96             is=>'rw',
97             isa=>Str,
98             default=>'',
99             lazy=>1,
100             );
101              
102             has data=>(
103             is=>'rw',
104             lazy=>1,
105             );
106              
107             has extra=>(
108             is=>'rw',
109             lazy=>1,
110             );
111              
112             =head1 OO Methods
113              
114             =over 4
115              
116             =item * my $data=$result->get_data
117              
118             Simply a wrapper for $result->data
119              
120             =cut
121              
122 0     0 1 0 sub get_data { $_[0]->data }
123              
124             =item * if($result->is_false) { ... }
125              
126             Inverts $self->is_true
127              
128             =cut
129              
130 0     0 0 0 sub is_false { !$_[0]->is_true }
131              
132             =item * my $result=Data::Result->new_true($data,$extra);
133              
134             Wrapper for
135              
136             my $result=Data::Result->new(is_true=>1,data=>$data,extra=>$data);
137              
138             =cut
139              
140             sub new_true {
141 4     4 1 257 my ($self,$data,$extra)=@_;
142 4         65 return $self->new(is_true=>1,data=>$data,extra=>$extra);
143             }
144              
145             =item * my $result=Data::Result->new_false($msg,$extra);
146              
147             Wrapper for
148              
149             my $result=Data::Result->new(is_true=>1,msg=>$msg,extra=>$data);
150              
151             =cut
152              
153             sub new_false {
154 5     5 1 68 my ($self,$msg,$extra)=@_;
155 5 100       118 croak '$msg is a required argument' unless defined($msg);
156 4         79 return $self->new(is_true=>0,msg=>$msg,extra=>$extra);
157             }
158              
159             sub DEMOLISH {
160 13     13 0 5058 my ($self)=@_;
161 13 50       40 return unless defined($self);
162 13         115 delete $self->{extra};
163 13         25 delete $self->{data};
164 13         378 delete $self->{msg};
165             }
166              
167             =back
168              
169             =head1 AUTHOR
170              
171             Mike Shipper
172              
173             =cut
174              
175             1;