Skip to content

Instantly share code, notes, and snippets.

@benvanstaveren
Created September 28, 2011 15:49
Show Gist options
  • Save benvanstaveren/1248298 to your computer and use it in GitHub Desktop.
Save benvanstaveren/1248298 to your computer and use it in GitHub Desktop.
use Moose::Util::TypeConstraints;
use Moose::Util ();
my $TYPE_REGISTRY = Moose::Util::TypeConstraints->get_type_constraint_registry;
$TYPE_REGISTRY->add_type_constraint(
Moose::Meta::TypeConstraint::Parameterizable->new(
name => 'DBref',
package_defined_in => __PACKAGE__,
parent => Moose::Util::TypeConstraints::find_type_constraint('Item'),
constraint => sub {
# they can be blank, basically
return 1 unless(defined($_));
# should be a hashref with at least those two keys present
return (
ref($_) eq 'HASH' &&
(defined($_->{'$id'}) && defined($_->{'$ref'}))
) ? 1 : 0;
},
constraint_generator => sub {
my $type_constraint = shift;
my $coll_name = ($type_constraint) ? $type_constraint->name : undef;
if(defined($coll_name)) {
return sub {
return unless($_->{'$ref'} eq $coll_name);
return 1;
};
} else {
return sub { 1; };
}
},
)
);
Moose::Util::TypeConstraints::add_parameterizable_type($TYPE_REGISTRY->get_type_constraint('DBref'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment