NAME

Raisin::Entity - A 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';

Type

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

OpenAPI

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

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.