File Coverage

blib/lib/Specio/Constraint/Enum.pm
Criterion Covered Total %
statement 34 34 100.0
branch n/a
condition n/a
subroutine 13 13 100.0
pod n/a
total 47 47 100.0


line stmt bran cond sub pod time code
1             package Specio::Constraint::Enum;
2              
3 3     3   16 use strict;
  3         5  
  3         113  
4 3     3   12 use warnings;
  3         5  
  3         238  
5              
6             our $VERSION = '0.53';
7              
8 3     3   16 use Role::Tiny::With;
  3         5  
  3         164  
9 3     3   12 use Scalar::Util qw( refaddr );
  3         6  
  3         96  
10 3     3   13 use Specio::Library::Builtins;
  3         4  
  3         47  
11 3     3   16 use Specio qw( _clone );
  3         4  
  3         167  
12 3     3   12 use Specio::OO;
  3         4  
  3         135  
13              
14 3     3   15 use Specio::Constraint::Role::Interface;
  3         3  
  3         1111  
15             with 'Specio::Constraint::Role::Interface';
16              
17             {
18             ## no critic (Subroutines::ProtectPrivateSubs)
19             my $attrs = _clone( Specio::Constraint::Role::Interface::_attrs() );
20             ## use critic
21              
22             for my $name (qw( parent _inline_generator )) {
23             $attrs->{$name}{init_arg} = undef;
24             $attrs->{$name}{builder}
25             = $name =~ /^_/ ? '_build' . $name : '_build_' . $name;
26             }
27              
28             $attrs->{values} = {
29             isa => 'ArrayRef',
30             required => 1,
31             };
32              
33             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
34             sub _attrs {
35 6     6   11 return $attrs;
36             }
37             }
38              
39             {
40             my $Str = t('Str');
41 5     5   43 sub _build_parent {$Str}
42             }
43              
44             {
45             my $_inline_generator = sub {
46             my $self = shift;
47             my $val = shift;
48              
49             return sprintf( <<'EOF', ($val) x 2, $self->_env_var_name, $val );
50             ( !ref( %s ) && defined( %s ) && $%s{ %s } )
51             EOF
52             };
53              
54 5     5   131 sub _build_inline_generator {$_inline_generator}
55             }
56              
57             sub _build_inline_environment {
58 3     3   13 my $self = shift;
59              
60 3         5 my %values = map { $_ => 1 } @{ $self->values };
  9         25  
  3         7  
61              
62 3         9 return { '%' . $self->_env_var_name => \%values };
63             }
64              
65             sub _env_var_name {
66 7     7   9 my $self = shift;
67              
68 7         33 return '_Specio_Constraint_Enum_' . refaddr($self);
69             }
70              
71             __PACKAGE__->_ooify;
72              
73             1;
74              
75             # ABSTRACT: A class for constraints which require a string matching one of a set of values
76              
77             __END__
78              
79             =pod
80              
81             =encoding UTF-8
82              
83             =head1 NAME
84              
85             Specio::Constraint::Enum - A class for constraints which require a string matching one of a set of values
86              
87             =head1 VERSION
88              
89             version 0.53
90              
91             =head1 SYNOPSIS
92              
93             my $type = Specio::Constraint::Enum->new(...);
94             print $_, "\n" for @{ $type->values };
95              
96             =head1 DESCRIPTION
97              
98             This is a specialized type constraint class for types which require a string
99             that matches one of a list of values.
100              
101             =head1 API
102              
103             This class provides all of the same methods as L<Specio::Constraint::Simple>,
104             with a few differences:
105              
106             =head2 Specio::Constraint::Enum->new( ... )
107              
108             The C<parent> parameter is ignored if it passed, as it is always set to the
109             C<Str> type.
110              
111             The C<inline_generator> and C<constraint> parameters are also ignored. This
112             class provides its own default inline generator subroutine reference.
113              
114             Finally, this class requires an additional parameter, C<values>. This must be a
115             an arrayref of valid strings for the type.
116              
117             =head2 $enum->values
118              
119             Returns an array reference of valid values for the type.
120              
121             =head1 ROLES
122              
123             This class does the L<Specio::Constraint::Role::Interface> and
124             L<Specio::Role::Inlinable> roles.
125              
126             =head1 SUPPORT
127              
128             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
129              
130             =head1 SOURCE
131              
132             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
133              
134             =head1 AUTHOR
135              
136             Dave Rolsky <autarch@urth.org>
137              
138             =head1 COPYRIGHT AND LICENSE
139              
140             This software is Copyright (c) 2012 - 2025 by Dave Rolsky.
141              
142             This is free software, licensed under:
143              
144             The Artistic License 2.0 (GPL Compatible)
145              
146             The full text of the license can be found in the
147             F<LICENSE> file included with this distribution.
148              
149             =cut