Fey::Role::SQL::Cloneable - Adds a just-deep-enough clone() method to SQL objects


Fey documentation Contained in the Fey distribution.

Index


Code Index:

NAME

Top

Fey::Role::SQL::Cloneable - Adds a just-deep-enough clone() method to SQL objects

VERSION

Top

version 0.40

SYNOPSIS

Top

  use Moose;

  with 'Fey::Role::SQL::Cloneable';

DESCRIPTION

Top

Classes which do this role have a clone() method which does a just-deep-enough clone of the object.

METHODS

Top

This role provides the following methods:

$query->clone()

Returns a new object which is a clone of the original.

BUGS

Top

See Fey for details on how to report bugs.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Fey documentation Contained in the Fey distribution.

package Fey::Role::SQL::Cloneable;
BEGIN {
  $Fey::Role::SQL::Cloneable::VERSION = '0.40';
}

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

use MooseX::Role::Parameterized;

parameter 'real_class' => ( isa => 'Moose::Meta::Class' );

# Yeah, I could've used MooseX::Clone, but avoiding the meta-API at
# runtime makes this all much faster. Of course, it's probably the
# root of all evil. OTOH, it's encapsulated in a role, so we can
# always replace it with an actual use of MX::Clone easily enough.
role {
    my $p     = shift;
    my %extra = @_;

    my @array_attr;
    my @hash_attr;

    # XXX - hack to allow Fey::Role::SetOperation to get Cloneable
    # applied to the real consuming class.
    my $meta = $p->real_class() ? $p->real_class() : $extra{consumer};

    for my $attr ( grep { $_->has_type_constraint() }
        $meta->get_all_attributes() ) {
        my $type = $attr->type_constraint();

        if ( $type->is_a_type_of('ArrayRef') ) {
            push @array_attr, $attr->name();
        }
        elsif ( $type->is_a_type_of('HashRef') ) {
            push @hash_attr, $attr->name();
        }
    }

    method clone => sub {
        my $self = shift;

        my $clone = bless { %{$self} }, ref $self;

        for my $name (@array_attr) {
            $clone->{$name} = [ @{ $self->{$name} } ];
        }

        for my $name (@hash_attr) {
            $clone->{$name} = { %{ $self->{$name} } };
        }

        return $clone;
    };
};

1;

# ABSTRACT: Adds a just-deep-enough clone() method to SQL objects




__END__