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


Fey-ORM documentation Contained in the Fey-ORM distribution.

Index


Code Index:

NAME

Top

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

VERSION

Top

version 0.43

SYNOPSIS

Top

  package MyApp::Song;

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

DESCRIPTION

Top

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

Top

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

* select

This must do the Fey::Role::SQL::ReturnsData role. 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

Top

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

$attr->select()

Returns the query object associated with this attribute.

$attr->bind_params()

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

ArrayRef TYPES

Top

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

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Fey-ORM documentation Contained in the Fey-ORM distribution.

package Fey::Meta::Attribute::FromSelect;
BEGIN {
  $Fey::Meta::Attribute::FromSelect::VERSION = '0.43';
}

use strict;
use warnings;
use namespace::autoclean;

use Moose;

extends 'Moose::Meta::Attribute';

with 'Fey::Meta::Role::FromSelect';

sub _process_options {
    my $class   = shift;
    my $name    = shift;
    my $options = shift;

    $options->{lazy} = 1;

    $options->{default} = $class->_make_sub_from_select(
        $options->{select},
        $options->{bind_params},
        $options->{multi_column},
        $options->{isa},
    );

    return $class->SUPER::_process_options( $name, $options );
}

sub _new {
    my $class = shift;
    my $options = @_ == 1 ? $_[0] : {@_};

    my $self = $class->SUPER::_new($options);

    $self->{select}          = $options->{select};
    $self->{bind_params}     = $options->{bind_params};
    $self->{is_multi_column} = $options->{multi_column};

    return $self;
}

# The parent class's constructor is not a Moose::Object-based
# constructor, so we don't want to inline one that is.
__PACKAGE__->meta()->make_immutable( inline_constructor => 0 );

package    # hide from PAUSE
    Moose::Meta::Attribute::Custom::FromSelect;
sub register_implementation {'Fey::Meta::Attribute::FromSelect'}

1;

# ABSTRACT: An attribute metaclass for SELECT-based attributes




__END__