Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Toru Yamaguchi
committed
Dec 6, 2011
1 parent
3facbee
commit a04f9fd
Showing
4 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package Data::RuledFactory::Rule::Callback; | ||
|
||
use strict; | ||
use warnings; | ||
use parent qw(Data::RuledFactory::Rule); | ||
|
||
our $VERSION = '0.01'; | ||
|
||
sub new { | ||
my $class = shift; | ||
my $args = ref $_[0] ? $_[0] : { @_ }; | ||
|
||
%$args = ( | ||
callback => undef, | ||
rows => undef, | ||
cursor => 0, | ||
%$args, | ||
); | ||
|
||
bless $args => $class; | ||
} | ||
|
||
sub has_next { | ||
my $self = shift; | ||
|
||
unless ($self->{callback}) { | ||
return 0; | ||
} | ||
|
||
unless ( defined $self->{rows} ) { | ||
return 1; | ||
} | ||
|
||
return $self->{cursor} < $self->{rows} ? 1 : 0; | ||
} | ||
|
||
sub _next { | ||
my $self = shift; | ||
return $self->{callback}->($self); | ||
} | ||
|
||
1; | ||
__END__ | ||
=head1 NAME | ||
Data::RuledFactory::Rule::Callback - | ||
=head1 SYNOPSIS | ||
use Data::RuledFactory::Rule::Callback; | ||
=head1 DESCRIPTION | ||
Data::RuledFactory::Rule::Callback is | ||
=head1 AUTHOR | ||
Toru Yamaguchi E<lt>zigorou@cpan.orgE<gt> | ||
=head1 SEE ALSO | ||
=head1 LICENSE | ||
This library is free software; you can redistribute it and/or modify | ||
it under the same terms as Perl itself. | ||
=cut | ||
# Local Variables: | ||
# mode: perl | ||
# perl-indent-level: 4 | ||
# indent-tabs-mode: nil | ||
# coding: utf-8-unix | ||
# End: | ||
# | ||
# vim: expandtab shiftwidth=4: | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package Data::RuledFactory::Rule::Constant; | ||
|
||
use strict; | ||
use warnings; | ||
use parent qw(Data::RuledFactory::Rule); | ||
|
||
our $VERSION = '0.01'; | ||
|
||
sub new { | ||
my $class = shift; | ||
my $args = ref $_[0] ? $_[0] : { @_ }; | ||
|
||
%$args = ( | ||
const => undef, | ||
rows => undef, | ||
cursor => 0, | ||
%$args, | ||
); | ||
|
||
bless $args => $class; | ||
} | ||
|
||
sub has_next { | ||
my $self = shift; | ||
|
||
unless ( defined $self->{rows} ) { | ||
return 1; | ||
} | ||
|
||
return $self->{cursor} < $self->{rows} ? 1 : 0; | ||
} | ||
|
||
sub _next { | ||
$_[0]->{const}; | ||
} | ||
|
||
1; | ||
__END__ | ||
=head1 NAME | ||
Data::RuledFactory::Rule::Constant - | ||
=head1 SYNOPSIS | ||
use Data::RuledFactory::Rule::Constant; | ||
=head1 DESCRIPTION | ||
Data::RuledFactory::Rule::Constant is | ||
=head1 AUTHOR | ||
Toru Yamaguchi E<lt>zigorou@cpan.orgE<gt> | ||
=head1 SEE ALSO | ||
=head1 LICENSE | ||
This library is free software; you can redistribute it and/or modify | ||
it under the same terms as Perl itself. | ||
=cut | ||
# Local Variables: | ||
# mode: perl | ||
# perl-indent-level: 4 | ||
# indent-tabs-mode: nil | ||
# coding: utf-8-unix | ||
# End: | ||
# | ||
# vim: expandtab shiftwidth=4: | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use Data::RuledFactory::Rule::Callback; | ||
|
||
subtest 'default' => sub { | ||
my $r = Data::RuledFactory::Rule::Callback->new( | ||
callback => sub { | ||
my $rule = shift; | ||
return $rule->cursor * 5; | ||
}, | ||
rows => 5, | ||
); | ||
|
||
my $i; | ||
|
||
for $i (1..5) { | ||
my $expect = 5 * $i; | ||
ok $r->has_next, sprintf('has_next() is true (times: %d)', $i); | ||
is $r->next, $expect, sprintf('next() equals %d (times: %d)', $expect, $i); | ||
} | ||
|
||
$i++; | ||
|
||
ok !$r->has_next, sprintf('has_next() is false (times: %d)', $i); | ||
is $r->next, undef, sprintf('next() is undef (times: %d)', $i); | ||
}; | ||
|
||
done_testing; | ||
|
||
# Local Variables: | ||
# mode: perl | ||
# perl-indent-level: 4 | ||
# indent-tabs-mode: nil | ||
# coding: utf-8-unix | ||
# End: | ||
# | ||
# vim: expandtab shiftwidth=4: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use Data::RuledFactory::Rule::Constant; | ||
|
||
subtest 'default' => sub { | ||
my $r = Data::RuledFactory::Rule::Constant->new( | ||
const => 10, | ||
rows => 5, | ||
); | ||
|
||
my $i; | ||
|
||
for $i (1..5) { | ||
ok $r->has_next, sprintf('has_next() is true (times: %d)', $i); | ||
is $r->next, 10, 'next() equals 10'; | ||
} | ||
|
||
$i++; | ||
|
||
ok !$r->has_next, sprintf('has_next() is false (times: %d)', $i); | ||
is $r->next, undef, sprintf('next() is undef (times: %d)', $i); | ||
}; | ||
|
||
done_testing; | ||
|
||
# Local Variables: | ||
# mode: perl | ||
# perl-indent-level: 4 | ||
# indent-tabs-mode: nil | ||
# coding: utf-8-unix | ||
# End: | ||
# | ||
# vim: expandtab shiftwidth=4: | ||
|