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 - Simple facade to use with your API.

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';

Documentation

NOT IMPLEMENTED!

Expose documentation with the field. Is used in a Raisin::Plugin::Swagger API documentation systems.

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

All available keys for documentation you could find in the Raisin::Plugin::Swagger.

compile

Compile an entity.

AUTHOR

Artur Khabibullin - rtkh <at> cpan.org

LICENSE

This module and all the modules in this package are governed by the same license as Perl itself.