File Coverage

blib/lib/Poz/Types/enum.pm
Criterion Covered Total %
statement 49 52 94.2
branch 9 10 90.0
condition 5 10 50.0
subroutine 7 8 87.5
pod 2 5 40.0
total 72 85 84.7


line stmt bran cond sub pod time code
1             package Poz::Types::enum;
2 11     11   74 use strict;
  11         20  
  11         502  
3 11     11   55 use warnings;
  11         50  
  11         598  
4 11     11   62 use parent 'Poz::Types::scalar';
  11         22  
  11         289  
5              
6             sub new {
7 5     5 1 13 my ($class, $enum) = @_;
8 5   50     18 $enum = $enum || [];
9 5         10 my $opts = {};
10 5   50     38 $opts->{required_error} //= "required";
11 5   50     29 $opts->{invalid_type_error} //= "Invalid data of enum";
12 5         30 my $self = $class->SUPER::new($opts);
13 5         11 $self->{__enum__} = $enum;
14 5         23 return $self;
15             }
16              
17             sub rule {
18 8     8 1 17 my ($self, $value) = @_;
19 8 50       26 return $self->{required_error} unless defined $value;
20 8 100       10 return $self->{invalid_type_error} unless grep { $_ eq $value } @{$self->{__enum__}};
  20         66  
  8         22  
21 4         13 return;
22             }
23              
24             sub as {
25 0     0 0 0 my ($self, $typename) = @_;
26 0         0 $self->{__as__} = $typename;
27 0         0 return $self;
28             }
29              
30             sub exclude {
31 1     1 0 4 my ($self, $opts) = @_;
32 1   50     5 $opts = $opts || [];
33 1         3 my $enum = [];
34 1         2 for my $e (@{$self->{__enum__}}) {
  1         4  
35 3         6 my $found = 0;
36 3         4 for my $o (@{$opts}) {
  3         6  
37 3 100       10 if ($e eq $o) {
38 1         2 $found = 1;
39 1         3 last;
40             }
41             }
42 3 100       9 push @{$enum}, $e unless $found;
  2         7  
43             }
44 1         5 return __PACKAGE__->new($enum);
45             }
46              
47             sub extract {
48 1     1 0 3 my ($self, $opts) = @_;
49 1   50     34 $opts = $opts || [];
50 1         5 my $enum = [];
51 1         2 for my $e (@{$self->{__enum__}}) {
  1         69  
52 3         7 for my $o (@{$opts}) {
  3         6  
53 5 100       13 if ($e eq $o) {
54 2         3 push @{$enum}, $e;
  2         4  
55 2         5 last;
56             }
57             }
58             }
59 1         5 return __PACKAGE__->new($enum);
60             }
61              
62             1;
63              
64             =head1 NAME
65              
66             Poz::Types::enum - Enum type handling for Poz
67              
68             =head1 SYNOPSIS
69              
70             use Poz qw/z/;
71              
72             my $enum = Poz::Types::enum->new(['foo', 'bar', 'baz']);
73             $enum->rule('foo'); # Valid
74             $enum->rule('qux'); # Throws error
75              
76             =head1 DESCRIPTION
77              
78             Poz::Types::enum is a module for handling enumeration types within the Poz framework. It allows you to define a set of valid values and provides methods to validate, exclude, and extract these values.
79              
80             =head1 METHODS
81              
82             =head2 new
83              
84             my $enum = Poz::Types::enum->new(\@values);
85              
86             Creates a new enum object with
87             =head1 AUTHOR
88              
89             Your Name <your.email@example.com>
90              
91             =head1 LICENSE
92              
93             This library is free software; you can redistribute it and/or modify
94             it under the same terms as Perl itself.
95              
96             =cut