Permalink
Browse files

cleanup and update version

  • Loading branch information...
1 parent 17068d1 commit d3f4a554f622eb98fbfc0ef9eb2851e61be2766b @yuki-kimoto committed Oct 15, 2013
Showing with 1 addition and 281 deletions.
  1. +1 −1 lib/Validator/Custom.pm
  2. +0 −182 lib/Validator/Custom/Rules.pm
  3. +0 −98 t/rules.t
View
2 lib/Validator/Custom.pm
@@ -1,7 +1,7 @@
package Validator::Custom;
use Object::Simple -base;
use 5.008001;
-our $VERSION = '0.21';
+our $VERSION = '0.22';
use Carp 'croak';
use Validator::Custom::Constraint;
View
182 lib/Validator/Custom/Rules.pm
@@ -1,182 +0,0 @@
-package Validator::Custom::Rules;
-use Object::Simple -base;
-
-use Validator::Custom::Rule;
-
-has rules => sub { {} };
-
-sub add {
- my ($self, $name, $rule) = @_;
-
- my $rules = $self->rules;
-
- if (ref $rule eq 'Validator::Custom::Rule') {
- $rules->{$name} = $rule;
- }
- else {
- my $rule_obj = Validator::Custom::Rule->new;
- $rule_obj->parse($rule);
- $rules->{$name} = $rule_obj;
- }
-
- return $self;
-}
-
-sub filter {
- my ($self, $name, $types) = @_;
-
- $self->{_filter} ||= {};
- $self->{_filter}{$name} = $types;
-
- return $self;
-}
-
-sub get {
- my ($self, $name, $type) = @_;
-
- if (defined $type) {
- my $filter = $self->{_filter} || {};
- my $types = $self->{_filter}->{$name}{$type};
-
- my $rule_all = $self->rules->{$name};
- my $rule = [];
- for my $key (@$types) {
- for my $item (@{$rule_all->rule}) {
- my $result_key;
- if (ref $item->{key} eq 'HASH') {
- $result_key = (keys %{$item->{key}})[0];
- }
- else {
- $result_key = $item->{key};
- }
- if ($result_key eq $key) {
- push @$rule, $item;
- }
- }
- }
- my $rule_obj = Validator::Custom::Rule->new;
- $rule_obj->rule($rule);
- }
- else {
- return $self->rules->{$name};
- }
-}
-
-1;
-
-=head1 NAME
-
-Validator::Custom::Rules - Rules (EXPERIMENTAL)
-
-=head1 SYNOPSYS
-
- # Rules object
- my $rules = Validator::Custom::Rules->new;
-
- # Add rule
- $rules->add(user => [
- id => [
- 'not_blank',
- 'ascii'
- ],
- name => [
- 'not_blank',
- {length => [1, 30]}
- ],
- age => [
- 'uint'
- ]
- ]);
-
- # Get rule
- my $rule_user = $rules->get('user');
-
- # Filter
- $rules->filter('user' => {
- insert => ['name', 'age'],
- update => ['id', 'name', 'age'],
- delete => ['id']
- });
-
- # Get rule with filtering
- my $rule_user_insert = $rules->get('user', 'insert');
- my $rule_user_update = $rules->get('user', 'update');
- my $rule_user_delete = $rules->get('user', 'delete');
-
-=head1 DESCRIPTION
-
-Validation::Custom::Rules is class to store and retreive rule and provide filter.
-
-=head1 ATTRIBUTES
-
-=head2 rules
-
- my $content = $rules->rules;
- $rules = $rules->rules($content);
-
-Content of rules object.
-
-=head1 METHODS
-
-=head1 add
-
- $rules = $rules->add($name => $rule);
-
-Add rule.
-
-You can add rule or rule object.
-
- # Rule
- my $rule = [
- id => [
- 'not_blank',
- 'ascii'
- ],
- name => [
- 'not_blank',
- {length => [1, 30]}
- ],
- age => [
- 'uint'
- ]
- ]
- $rules->add(user => $rule);
-
- # Rule object
- my $rule_obj = Validator::Custom::Rule->new;
- $rule_obj->parse($rule);
- $rules->add(user => $rule_obj);
-
-=head1 filter
-
- $rules = $rules->filter($name => $filtering);
-
-Resister filtering.
-
- # Filter
- $rules->filter('user' => {
- insert => ['name', 'age'],
- update => ['id', 'name', 'age'],
- delete => ['id']
- });
-
-C<get> method use these filtering.
-
- my $rule_user_insert = $rules->get('user', 'insert');
-
-=head1 get
-
- $rules = $rules->get($name);
- $rules = $rules->get($name, $filter_name);
-
-Get rule.
-
- # Get rule
- my $rule_user = $rules->get('user');
-
-You can specify filtering.
-
- # Get rule with filtering
- my $rule_user_insert = $rules->get('user', 'insert');
- my $rule_user_update = $rules->get('user', 'update');
- my $rule_user_delete = $rules->get('user', 'delete');
View
98 t/rules.t
@@ -1,98 +0,0 @@
-use Test::More 'no_plan';
-
-use strict;
-use warnings;
-use Validator::Custom::Rules;
-use Validator::Custom::Rule;
-
-$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED!/ };
-
-# add and get method
-{
- my $rules = Validator::Custom::Rules->new;
-
- # Add
- $rules->add(user => [
- id => [
- 'ascii'
- ],
- name => [
- 'not_blank'
- ],
- age => [
- 'uint'
- ],
- {password_check => ['password1', 'password2']} => [
- 'duplication'
- ]
- ]);
-
- $rules->add(book => [
- id => [
- 'ascii'
- ],
- title => [
- 'not_blank'
- ]
- ]);
-
- # Add rule object
- my $rule_obj = Validator::Custom::Rule->new;
- $rule_obj->parse(
- [
- id2 => [
- 'ascii'
- ]
- ]
- );
- $rules->add(user2 => $rule_obj);
-
- my $rule_user = $rules->get('user');
- is($rule_user->rule->[1]->{key}, 'name');
-
- my $rule_book = $rules->get('book');
- is($rule_book->rule->[1]->{key}, 'title');
-
- my $rule_user2 = $rules->get('user2');
- is($rule_user2->rule->[0]->{key}, 'id2');
-}
-
-# filter
-{
- my $rules = Validator::Custom::Rules->new;
-
- # Add
- $rules->add(user => [
- id => [
- 'ascii'
- ],
- name => [
- 'not_blank'
- ],
- age => [
- 'uint'
- ],
- {password_check => ['password1', 'password2']} => [
- 'duplication'
- ]
- ]);
-
- $rules->filter(user => {
- insert => ['name', 'age'],
- update => ['id', 'name', 'password_check']
- });
-
- # User insert
- my $rule_user_insert = $rules->get('user', 'insert');
- is(scalar @{$rule_user_insert->rule}, 2);
- is($rule_user_insert->rule->[0]->{key}, 'name');
- is($rule_user_insert->rule->[1]->{key}, 'age');
-
- # User update
- $DB::single = 1;
- my $rule_user_update = $rules->get('user', 'update');
- is(scalar @{$rule_user_update->rule}, 3);
- is($rule_user_update->rule->[0]->{key}, 'id');
- is($rule_user_update->rule->[1]->{key}, 'name');
- is((keys %{$rule_user_update->rule->[2]->{key}})[0], 'password_check');
-}

0 comments on commit d3f4a55

Please sign in to comment.