The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Data::Object::Types::Keywords

ABSTRACT

Data-Object Type Library Keywords

SYNOPSIS

package Test::Library;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

register
{
  name => 'Person',
  aliases => ['Student', 'Teacher'],
  validation => is_instance_of('Test::Person'),
  parent => 'Object'
},
{
  name => 'Principal',
  validation => is_instance_of('Test::Person'),
  parent => 'Object'
};

# creates person, student, and teacher constraints

package main;

my $library = Test::Library->meta;

DESCRIPTION

This package provides type library keyword functions for Data::Object::Types::Library and Type::Library libraries.

LIBRARIES

This package uses type constraints from:

Types::Standard

SCENARIOS

This package supports the following scenarios:

exports

package Test::Library::Exports;

use base 'Data::Object::Types::Library';

use Data::Object::Types::Keywords;

# The following is a snapshot of the exported keyword functions:

# as
# class_type
# classifier
# coerce
# compile_match_on_type
# declare
# declare_coercion
# duck_type
# dwim_type
# english_list
# enum
# extends
# from
# is_all_of
# is_any_of
# is_one_of
# inline_as
# intersection
# is_capable_of
# is_consumer_of
# is_instance_of
# match_on_type
# message
# register
# role_type
# subtype
# to_type
# type
# union
# via
# where

"Test::Library::Exports"

This package supports exporting functions which help configure Type::Library derived libraries.

FUNCTIONS

This package implements the following functions:

is_all_of

is_all_of(CodeRef @checks) : CodeRef

The is_all_of function accepts one or more callbacks and returns truthy if all of the callbacks return truthy.

is_all_of example #1
package Test::Library::HasAllOf;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

my $validation = is_all_of(
  sub {
    my ($value) = @_;

    return 0 if !$value->isa('Test::Entity');
    return 1;
  },
  sub {
    my ($value) = @_;

    return 0 if !$value->isa('Test::Person');
    return 1;
  },
);

register {
  name => 'Person',
  validation => $validation,
  parent => 'Object'
};

$validation

is_any_of

is_any_of(CodeRef @checks) : CodeRef

The is_any_of function accepts one or more callbacks and returns truthy if any of the callbacks return truthy.

is_any_of example #1
package Test::Library::HasAnyOf;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

my $validation = is_any_of(
  sub {
    my ($value) = @_;

    return 0 if !$value->isa('App::Person');
    return 1;
  },
  sub {
    my ($value) = @_;

    return 0 if !$value->isa('Test::Person');
    return 1;
  },
);

register {
  name => 'Person',
  validation => $validation,
  parent => 'Object'
};

$validation

is_capable_of

is_capable_of(Str @routines) : CodeRef

The is_capable_of function accepts one or more subroutine names and returns a callback which returns truthy if the value passed to the callback has implemented all of the routines specified.

is_capable_of example #1
package Test::Library::IsCapableOf;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

my $validation = is_capable_of(qw(create update delete));

register {
  name => 'Person',
  validation => $validation,
  parent => 'Object'
};

$validation

is_comprised_of

is_comprised_of(Str @names) : CodeRef

The is_comprised_of function accepts one or more names and returns a callback which returns truthy if the value passed to the callback is a hashref or hashref based object which has keys that correspond to the names provided.

is_comprised_of example #1
package Test::Library::IsComprisedOf;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

my $validation = is_comprised_of(qw(mon tues wed thurs fri sat sun));

register {
  name => 'WorkHours',
  validation => $validation,
  parent => 'HashRef'
};

$validation

is_consumer_of

is_consumer_of(Str $name) : CodeRef

The is_consumer_of function accepts a role name and returns a callback which returns truthy if the value passed to the callback consumes the role specified.

is_consumer_of example #1
package Test::Library::IsConsumerOf;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

my $validation = is_consumer_of('Test::Role::Identifiable');

register {
  name => 'Person',
  validation => $validation,
  parent => 'Object'
};

$validation

is_instance_of

is_instance_of(Str $name) : CodeRef

The is_instance_of function accepts a class or package name and returns a callback which returns truthy if the value passed to the callback inherits from the class or package specified.

is_instance_of example #1
package Test::Library::IsInstanceOf;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

my $validation = is_instance_of('Test::Person');

register {
  name => 'Person',
  validation => $validation,
  parent => 'Object'
};

$validation

is_one_of

is_one_of(CodeRef @checks) : CodeRef

The is_one_of function accepts one or more callbacks and returns truthy if only one of the callbacks return truthy.

is_one_of example #1
package Test::Library::HasOneOf;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

my $validation = is_one_of(
  sub {
    my ($value) = @_;

    return 0 if !$value->isa('Test::Student');
    return 1;
  },
  sub {
    my ($value) = @_;

    return 0 if !$value->isa('Test::Teacher');
    return 1;
  },
);

register {
  name => 'Person',
  validation => $validation,
  parent => 'Object'
};

$validation

register

register(HashRef $type) : InstanceOf["Type::Tiny"]

The register function takes a simple hashref and creates and registers a Type::Tiny type object.

register example #1
package Test::Library::Standard;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

register {
  name => 'Message',
  coercions => [
    'Str', sub {
      my ($value) = @_;

      {
        type => 'simple',
        payload => $value
      }
    }
  ],
  validation => sub {
    my ($value) = @_;

    return 0 if !$value->{type};
    return 0 if !$value->{payload};
    return 1;
  },
  parent => 'HashRef'
};
register example #2
package Test::Library::Parameterized;

use Data::Object::Types::Keywords;

use base 'Data::Object::Types::Library';

extends 'Types::Standard';

register {
  name => 'People',
  coercions => [
    'ArrayRef', sub {
      my ($value) = @_;

      Test::People->new($value)
    }
  ],
  validation => sub {
    my ($value) = @_;

    return 0 if !$value->isa('Test::People');
    return 1;
  },
  explaination => sub {
    my ($value, $type, $name) = @_;

    my $param = $type->parameters->[0];

    for my $i (0 .. $#$value) {
      next if $param->check($value->[$i]);

      my $indx = sprintf('%s->[%d]', $name, $i);
      my $desc = $param->validate_explain($value->[$i], $indx);
      my $text = '"%s" constrains each value in the array object with "%s"';

      return [sprintf($text, $type, $param), @{$desc}];
    }

    return;
  },
  parameterize_constraint => sub {
    my ($value, $type) = @_;

    $type->check($_) || return for @$value;

    return !!1;
  },
  parameterize_coercions => sub {
    my ($data, $type, $anon) = @_;

    my $coercions = [];

    push @$coercions, 'ArrayRef', sub {
      my $value = @_ ? $_[0] : $_;
      my $items = [];

      for (my $i = 0; $i < @$value; $i++) {
        return $value unless $anon->check($value->[$i]);
        $items->[$i] = $data->coerce($value->[$i]);
      }

      return $type->coerce($items);
    };

    return $coercions;
  },
  parent => 'Object'
};

AUTHOR

Al Newkirk, awncorp@cpan.org

LICENSE

Copyright (C) 2011-2019, Al Newkirk, et al.

This is free software; you can redistribute it and/or modify it under the terms of the The Apache License, Version 2.0, as elucidated in the "license file".

PROJECT

Wiki

Project

Initiatives

Milestones

Contributing

Issues