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

Raisin::Entity - A simple facade to use with your API

VERSION

version 0.94

SYNOPSIS

package MusicApp::Entity::Artist;

use strict;
use warnings;

use Raisin::Entity;

expose 'id';
expose 'name', as => 'artist';
expose 'website', if => sub {
    my $artist = shift;
    $artist->website;
};
expose 'albums', using => 'MusicApp::Entity::Album';
expose 'hash', sub {
    my $artist = shift;
    my $hash = 0;
    my $name = blessed($artist) ? $artist->name : $artist->{name};
    foreach (split //, $name) {
        $hash = $hash * 42 + ord($_);
    }
    $hash;
};

1;

DESCRIPTION

Supports DBIx::Class, Rose::DB::Object and basic Perl data structures like SCALAR, ARRAY & HASH.

METHODS

expose

Define a fields that will be exposed.

The field lookup requests specified name

Basic exposure

expose 'id';

Exposing with a presenter

Use using to expose a field with a presenter.

expose 'albums', using => 'MusicApp::Entity::Album';

Conditional exposure

You can use if to expose fields conditionally.

expose 'website', if => sub {
    my $artist = shift;
    blessed($artist) && $artist->can('website');
};

Nested exposure

Supply a block to define a hash using nested exposures.

expose 'contact_info', sub {
    expose 'phone';
    expose 'address', using => 'API::Address';
};

Runtime exposure

Use a subroutine to evaluate exposure at runtime.

expose 'hash', sub {
    my $artist = shift;
    my $hash;
    foreach (split //, $artist->name) {
        $hash = $hash * 42 + ord($_);
    }
    $hash;
};

Aliases exposure

Expose under an alias with as.

expose 'name', as => 'artist';

Type

expose 'name', documentation => { type => 'String', desc => 'Artists name' };

OpenAPI

OpenAPI compatible specification generates automatically if OpenAPI/Swagger plugin enabled.

AUTHOR

Artur Khabibullin

COPYRIGHT AND LICENSE

This software is copyright (c) 2019 by Artur Khabibullin.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.