6 Moose::Cookbook::Recipe6 - The Moose::Role example
15 has 'value' => (isa => 'Int', is => 'ro');
17 around 'validate' => sub {
19 my ($self, $field) = @_;
20 if ($c->($self, $self->validation_value($field))) {
24 return $self->error_message;
28 sub validation_value {
29 my ($self, $field) = @_;
33 sub error_message { confess "Abstract method!" }
35 package Constraint::OnLength;
40 has 'units' => (isa => 'Str', is => 'ro');
42 override 'validation_value' => sub {
43 return length(super());
46 override 'error_message' => sub {
48 return super() . ' ' . $self->units;
51 package Constraint::AtLeast;
59 my ($self, $field) = @_;
60 ($field >= $self->value);
63 sub error_message { 'must be at least ' . (shift)->value; }
65 package Constraint::NoMoreThan;
73 my ($self, $field) = @_;
74 ($field <= $self->value);
77 sub error_message { 'must be no more than ' . (shift)->value; }
79 package Constraint::LengthNoMoreThan;
84 extends 'Constraint::NoMoreThan';
85 with 'Constraint::OnLength';
87 package Constraint::LengthAtLeast;
92 extends 'Constraint::AtLeast';
93 with 'Constraint::OnLength';
99 (the other 4 recipes kinda burned me out a bit)
103 Stevan Little E<lt>stevan@iinteractive.comE<gt>
105 =head1 COPYRIGHT AND LICENSE
107 Copyright 2006 by Infinity Interactive, Inc.
109 L<http://www.iinteractive.com>
111 This library is free software; you can redistribute it and/or modify
112 it under the same terms as Perl itself.