6 Moose::Cookbook::Recipe6 - The Moose::Role example
15 has 'value' => (isa => 'Int', is => 'ro');
17 around 'validate' => sub {
19 my ($self, $field) = @_;
20 return undef if $c->($self, $self->validation_value($field));
21 return $self->error_message;
24 sub validation_value {
25 my ($self, $field) = @_;
29 sub error_message { confess "Abstract method!" }
31 package Constraint::OnLength;
36 has 'units' => (isa => 'Str', is => 'ro');
38 override 'validation_value' => sub {
39 return length(super());
42 override 'error_message' => sub {
44 return super() . ' ' . $self->units;
47 package Constraint::AtLeast;
55 my ($self, $field) = @_;
56 ($field >= $self->value);
59 sub error_message { 'must be at least ' . (shift)->value; }
61 package Constraint::NoMoreThan;
69 my ($self, $field) = @_;
70 ($field <= $self->value);
73 sub error_message { 'must be no more than ' . (shift)->value; }
75 package Constraint::LengthNoMoreThan;
80 extends 'Constraint::NoMoreThan';
81 with 'Constraint::OnLength';
83 package Constraint::LengthAtLeast;
88 extends 'Constraint::AtLeast';
89 with 'Constraint::OnLength';
95 (the other 4 recipes kinda burned me out a bit)
99 Stevan Little E<lt>stevan@iinteractive.comE<gt>
101 =head1 COPYRIGHT AND LICENSE
103 Copyright 2006 by Infinity Interactive, Inc.
105 L<http://www.iinteractive.com>
107 This library is free software; you can redistribute it and/or modify
108 it under the same terms as Perl itself.