|
| 1 | +package Specio::Constraint::Structurable; |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +our $VERSION = '0.34'; |
| 7 | + |
| 8 | +use Carp qw( confess ); |
| 9 | +use Role::Tiny::With; |
| 10 | +use Scalar::Util qw( blessed ); |
| 11 | +use Specio::DeclaredAt; |
| 12 | +use Specio::OO; |
| 13 | +use Specio::Constraint::Structured; |
| 14 | +use Specio::TypeChecks qw( does_role isa_class ); |
| 15 | + |
| 16 | +use Specio::Constraint::Role::Interface; |
| 17 | +with 'Specio::Constraint::Role::Interface'; |
| 18 | + |
| 19 | +{ |
| 20 | + ## no critic (Subroutines::ProtectPrivateSubs) |
| 21 | + my $role_attrs = Specio::Constraint::Role::Interface::_attrs(); |
| 22 | + ## use critic |
| 23 | + |
| 24 | + my $attrs = { |
| 25 | + %{$role_attrs}, |
| 26 | + _parameterization_args_builder => { |
| 27 | + isa => 'CodeRef', |
| 28 | + init_arg => 'parameterization_args_builder', |
| 29 | + required => 1, |
| 30 | + }, |
| 31 | + _name_builder => { |
| 32 | + isa => 'CodeRef', |
| 33 | + init_arg => 'name_builder', |
| 34 | + required => 1, |
| 35 | + }, |
| 36 | + _structured_constraint_generator => { |
| 37 | + isa => 'CodeRef', |
| 38 | + init_arg => 'structured_constraint_generator', |
| 39 | + predicate => '_has_structured_constraint_generator', |
| 40 | + }, |
| 41 | + _structured_inline_generator => { |
| 42 | + isa => 'CodeRef', |
| 43 | + init_arg => 'structured_inline_generator', |
| 44 | + predicate => '_has_structured_inline_generator', |
| 45 | + }, |
| 46 | + }; |
| 47 | + |
| 48 | + ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) |
| 49 | + sub _attrs { |
| 50 | + return $attrs; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +sub BUILD { |
| 55 | + my $self = shift; |
| 56 | + |
| 57 | + if ( $self->_has_constraint ) { |
| 58 | + die |
| 59 | + 'A structurable constraint with a constraint parameter must also have a structured_constraint_generator' |
| 60 | + unless $self->_has_structured_constraint_generator; |
| 61 | + } |
| 62 | + |
| 63 | + if ( $self->_has_inline_generator ) { |
| 64 | + die |
| 65 | + 'A structurable constraint with an inline_generator parameter must also have a structured_inline_generator' |
| 66 | + unless $self->_has_structured_inline_generator; |
| 67 | + } |
| 68 | + |
| 69 | + return; |
| 70 | +} |
| 71 | + |
| 72 | +sub parameterize { |
| 73 | + my $self = shift; |
| 74 | + my %args = @_; |
| 75 | + |
| 76 | + my $declared_at = $args{declared_at}; |
| 77 | + |
| 78 | + if ($declared_at) { |
| 79 | + isa_class( $declared_at, 'Specio::DeclaredAt' ) |
| 80 | + or confess |
| 81 | + q{The "declared_at" parameter passed to ->parameterize must be a Specio::DeclaredAt object}; |
| 82 | + } |
| 83 | + |
| 84 | + my %parameters |
| 85 | + = $self->_parameterization_args_builder->( $self, $args{of} ); |
| 86 | + |
| 87 | + $declared_at = Specio::DeclaredAt->new_from_caller(1) |
| 88 | + unless defined $declared_at; |
| 89 | + |
| 90 | + my %new_p = ( |
| 91 | + parent => $self, |
| 92 | + parameters => \%parameters, |
| 93 | + declared_at => $declared_at, |
| 94 | + name => $self->_name_builder->( $self, \%parameters ), |
| 95 | + ); |
| 96 | + |
| 97 | + if ( $self->_has_structured_constraint_generator ) { |
| 98 | + $new_p{constraint} |
| 99 | + = $self->_structured_constraint_generator->(%parameters); |
| 100 | + } |
| 101 | + else { |
| 102 | + for my $p ( |
| 103 | + grep { |
| 104 | + blessed($_) |
| 105 | + && does_role('Specio::Constraint::Role::Interface') |
| 106 | + } values %parameters |
| 107 | + ) { |
| 108 | + |
| 109 | + confess |
| 110 | + q{Any type objects passed to ->parameterize must be inlinable constraints if the structurable type has an inline_generator} |
| 111 | + unless $p->can_be_inlined; |
| 112 | + } |
| 113 | + |
| 114 | + my $ig = $self->_structured_inline_generator; |
| 115 | + $new_p{inline_generator} |
| 116 | + = sub { $ig->( shift, shift, %parameters, @_ ) }; |
| 117 | + } |
| 118 | + |
| 119 | + return Specio::Constraint::Structured->new(%new_p); |
| 120 | +} |
| 121 | + |
| 122 | +## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) |
| 123 | +sub _name_or_anon { |
| 124 | + return $_[1]->_has_name ? $_[1]->name : 'ANON'; |
| 125 | +} |
| 126 | +## use critic |
| 127 | + |
| 128 | +__PACKAGE__->_ooify; |
| 129 | + |
| 130 | +1; |
| 131 | + |
| 132 | +# ABSTRACT: A class which represents structurable constraints |
| 133 | + |
| 134 | +__END__ |
| 135 | +
|
| 136 | +=pod |
| 137 | +
|
| 138 | +=for Pod::Coverage BUILD |
| 139 | +
|
| 140 | +=head1 SYNOPSIS |
| 141 | +
|
| 142 | + my $tuple = t('Tuple'); |
| 143 | +
|
| 144 | + my $tuple_of_str_int = $tuple->parameterize( of => [ t('Str'), t('Int') ] ); |
| 145 | +
|
| 146 | +=head1 DESCRIPTION |
| 147 | +
|
| 148 | +This class implements the API for structurable types like C<Dict>, C<Map>< and |
| 149 | +C<Tuple>. |
| 150 | +
|
| 151 | +=head1 API |
| 152 | +
|
| 153 | +This class implements the same API as L<Specio::Constraint::Simple>, with a few |
| 154 | +additions. |
| 155 | +
|
| 156 | +=head2 Specio::Constraint::Structurable->new(...) |
| 157 | +
|
| 158 | +This class's constructor accepts two additional parameters: |
| 159 | +
|
| 160 | +=over 4 |
| 161 | +
|
| 162 | +=item * parameterization_args_builder |
| 163 | +
|
| 164 | +This is a subroutine that takes the values passed to C<of> and returns a hash |
| 165 | +of named arguments. These arguments will then be passed into the |
| 166 | +C<structured_constraint_generator> or C<structured_inline_generator>. |
| 167 | +
|
| 168 | +This should also do argument checking to make sure that the argument passed |
| 169 | +are valid. For example, the C<Tuple> type turns the arrayref passed to C<of> |
| 170 | +into a hash, along the way checking that the caller did not do things like |
| 171 | +interleave optional and required elements or mix optional and slurpy together |
| 172 | +in the definition. |
| 173 | +
|
| 174 | +This parameter is required. |
| 175 | +
|
| 176 | +=item * name_builder |
| 177 | +
|
| 178 | +This is a subroutine that is called to generate a name for the structured type |
| 179 | +when it is created. This will be called as a method on the |
| 180 | +C<Specio::Constraint::Structurable> object. It will be passed the hash of |
| 181 | +arguments returned by the C<parameterization_args_builder>. |
| 182 | +
|
| 183 | +This parameter is required. |
| 184 | +
|
| 185 | +=item * structured_constraint_generator |
| 186 | +
|
| 187 | +This is a subroutine that generates a new constraint subroutine when the type |
| 188 | +is structured. |
| 189 | +
|
| 190 | +It will be called as a method on the type and will be passed the hash of |
| 191 | +arguments returned by the C<parameterization_args_builder>. |
| 192 | +
|
| 193 | +This parameter is mutually exclusive with the C<structured_inline_generator> |
| 194 | +parameter. |
| 195 | +
|
| 196 | +This parameter or the C<structured_inline_generator> parameter is required. |
| 197 | +
|
| 198 | +=item * structured_inline_generator |
| 199 | +
|
| 200 | +This is a subroutine that generates a new inline generator subroutine when the |
| 201 | +type is structured. |
| 202 | +
|
| 203 | +It will be called as a method on the L<Specio::Constraint::Structured> object |
| 204 | +when that object needs to generate an inline constraint. It will receive the |
| 205 | +type parameter as the first argument and the variable name as a string as the |
| 206 | +second. |
| 207 | +
|
| 208 | +The remaining arguments will be the parameter hash returned by the |
| 209 | +C<parameterization_args_builder>. |
| 210 | +
|
| 211 | +This probably seems fairly confusing, so looking at the examples in the |
| 212 | +L<Specio::Library::Structured::*> code may be helpful. |
| 213 | +
|
| 214 | +This parameter is mutually exclusive with the |
| 215 | +C<structured_constraint_generator> parameter. |
| 216 | +
|
| 217 | +This parameter or the C<structured_constraint_generator> parameter is |
| 218 | +required. |
| 219 | +
|
| 220 | +=back |
| 221 | +
|
| 222 | +=head2 $type->parameterize(...) |
| 223 | +
|
| 224 | +This method takes two arguments. The C<of> argument should be an object which |
| 225 | +does the L<Specio::Constraint::Role::Interface> role, and is required. |
| 226 | +
|
| 227 | +The other argument, C<declared_at>, is optional. If it is not given, then a |
| 228 | +new L<Specio::DeclaredAt> object is creating using a call stack depth of 1. |
| 229 | +
|
| 230 | +This method returns a new L<Specio::Constraint::Structured> object. |
| 231 | +
|
| 232 | +=cut |
0 commit comments