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   22 use strict;
  3         6  
  3         127  
4 3     3   18 use warnings;
  3         7  
  3         161  
5              
6             our $VERSION = '0.47';
7              
8 3     3   20 use Role::Tiny::With;
  3         6  
  3         194  
9 3     3   20 use Scalar::Util qw( refaddr );
  3         6  
  3         157  
10 3     3   17 use Specio::Library::Builtins;
  3         8  
  3         34  
11 3     3   33 use Specio::OO;
  3         6  
  3         202  
12 3     3   37 use Storable qw( dclone );
  3         6  
  3         175  
13              
14 3     3   30 use Specio::Constraint::Role::Interface;
  3         7  
  3         1272  
15             with 'Specio::Constraint::Role::Interface';
16              
17             {
18             ## no critic (Subroutines::ProtectPrivateSubs)
19             my $attrs = dclone( 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   17 return $attrs;
36             }
37             }
38              
39             {
40             my $Str = t('Str');
41 5     5   42 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   144 sub _build_inline_generator {$_inline_generator}
55             }
56              
57             sub _build_inline_environment {
58 3     3   18 my $self = shift;
59              
60 3         7 my %values = map { $_ => 1 } @{ $self->values };
  9         33  
  3         8  
61              
62 3         8 return { '%' . $self->_env_var_name => \%values };
63             }
64              
65             sub _env_var_name {
66 7     7   13 my $self = shift;
67              
68 7         52 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.47
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             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
131              
132             =head1 SOURCE
133              
134             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
135              
136             =head1 AUTHOR
137              
138             Dave Rolsky <autarch@urth.org>
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is Copyright (c) 2012 - 2021 by Dave Rolsky.
143              
144             This is free software, licensed under:
145              
146             The Artistic License 2.0 (GPL Compatible)
147              
148             The full text of the license can be found in the
149             F<LICENSE> file included with this distribution.
150              
151             =cut