File Coverage

blib/lib/Poz/Types/scalar.pm
Criterion Covered Total %
statement 32 34 94.1
branch 8 8 100.0
condition n/a
subroutine 10 11 90.9
pod 4 4 100.0
total 54 57 94.7


line stmt bran cond sub pod time code
1             package Poz::Types::scalar;
2 11     11   5274 use 5.032;
  11         34  
3 11     11   51 use strict;
  11         16  
  11         315  
4 11     11   45 use warnings;
  11         17  
  11         517  
5 11     11   57 use parent 'Poz::Types';
  11         168  
  11         134  
6              
7             sub coerce {
8 0     0 1 0 my ($self, $value) = @_;
9 0         0 return $value;
10             }
11              
12             sub default {
13 11     11 1 23 my ($self, $default) = @_;
14 11         42 push @{$self->{transform}}, sub {
15 54     54   98 my ($self, $value) = @_;
16 54 100       160 return $value if defined $value;
17 19 100       81 return ref($default) eq "CODE" ? $default->($self) : $default;
18 11         16 };
19 11         35 return $self;
20             }
21              
22             sub nullable {
23 2     2 1 7 my ($self) = @_;
24 2         13 unshift @{$self->{rules}}, sub {
25 9     9   18 my ($self, $value) = @_;
26 9 100       47 return bless [], "Poz::Result::ShortCircuit" unless defined $value;
27 7         17 return;
28 2         6 };
29 2         9 return $self;
30             }
31              
32             sub optional {
33 4     4 1 12 my ($self) = @_;
34 4         29 unshift @{$self->{rules}}, sub {
35 15     15   30 my ($self, $value) = @_;
36 15 100       57 return bless [], "Poz::Result::ShortCircuit" unless defined $value;
37 12         49 return;
38 4         9 };
39 4         15 return $self;
40             }
41              
42             1;
43              
44             =head1 NAME
45              
46             Poz::Types::scalar - Scalar type handling for Poz framework
47              
48             =head1 SYNOPSIS
49              
50             use Poz::Types::scalar;
51              
52             my $scalar_type = Poz::Types::scalar->new();
53             $scalar_type->default('default_value');
54             $scalar_type->nullable();
55             $scalar_type->optional();
56              
57             =head1 DESCRIPTION
58              
59             Poz::Types::scalar provides methods to handle scalar types within the Poz framework. It allows setting default values, marking values as nullable or optional, and coercing values.
60              
61             =head1 METHODS
62              
63             =head2 coerce
64              
65             $scalar_type->coerce($value);
66              
67             Returns the given value without modification. This method is used to coerce values into the desired type.
68              
69             =head2 default
70              
71             $scalar_type->default($default_value);
72              
73             Sets a default value for the scalar type. If the value is undefined, the default value will be used. The default value can be a scalar or a code reference.
74              
75             =head2 nullable
76              
77             $scalar_type->nullable();
78              
79             Marks the scalar type as nullable. If the value is undefined, it will short-circuit and return without further processing.
80              
81             =head2 optional
82              
83             $scalar_type->optional();
84              
85             Marks the scalar type as optional. If the value is undefined, it will short-circuit and return without further processing.
86              
87             =head1 LICENSE
88              
89             Copyright (C) ytnobody.
90              
91             This library is free software; you can redistribute it and/or modify
92             it under the same terms as Perl itself.
93              
94             =head1 AUTHOR
95              
96             ytnobody E<lt>ytnobody@gmail.comE<gt>
97              
98             =cut