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

    __PACKAGE__->expose('id');
    __PACKAGE__->expose('name', as => 'artist');
    __PACKAGE__->expose('website', if => sub {
        my $artist = shift;
        $artist->website;
    });
    __PACKAGE__->expose('albums', using => 'MusicApp::Entity::Album');
    __PACKAGE__->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

    __PACKAGE__->expose('id');

Exposing with a presenter

Use using to expose field with a presenter.

    __PACKAGE__->expose('albums', using => 'MusicApp::Entity::Album');

Conditional exposure

You can use if to expose fields conditionally.

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

Nested exposure

NOT IMPLEMENTED!

Supply a block to define a hash using nested exposures.

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

Runtime exposure

Use a subroutine to evaluate exposure at runtime.

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

Aliases exposure

Expose under a different name with as.

    __PACKAGE__->expose('name', as => 'artist');

Documentation

NOT IMPLEMENTED!

Expose documentation with the field. Gets bubbled up when used with Raisin::Plugin::Swagger API documentation systems.

    __PACKAGE__->expose(
        'name', documentation => { type => 'String', desc => 'Artists name' }
    );

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.