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

Fey::Meta::Attribute::FromSelect - an attribute metaclass for SELECT-based attributes

SYNOPSIS

  package MyApp::Song;

  has 'average_rating' =>
      ( metaclass   => 'FromSelect',
        is          => 'ro',
        isa         => 'Float',
        select      => $select,
        bind_params => sub { $_[0]->song_id() },
      );

DESCRIPTION

This attribute metaclass allows you to set an attribute's default based on a SELECT query and optional bound parameters. This is a fairly common need when writing ORM-based classes.

OPTIONS

This metaclass accepts two additional parameters in addition to the normal Moose attribute options.

  • select

    This must be a Fey::SQL::Select object (or subclass). It is required.

  • bind_params

    This must be a subroutine reference, which when called will return an array of bind parameters for the query. This subref will be called as a method on the object which has the attribute. This is an optional parameter.

Note that this metaclass overrides any value you provide for "default" with a subroutine that executes the query and gets the value it returns.

METHODS

This class adds a few methods to those provided by Moose::Meta::Attribute:

$attr->select()

Returns the Fey::SQL::Select object associated with this attribute.

$attr->bind_params()

Returns the bind_params subroutine reference associated with this attribute, if any.

ArrayRef TYPES

By default, the SELECT is expected to return just a single row with one column. However, if you set the type of the attribute to ArrayRef (or a subtype), then the select can return multiple rows, still with a single column.

AUTHOR

Dave Rolsky, <autarch@urth.org>

BUGS

See Fey::ORM for details.

COPYRIGHT & LICENSE

Copyright 2006-2009 Dave Rolsky, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module.