From: Alexander Hartmaier Date: Thu, 13 Jan 2011 16:32:24 +0000 (+0100) Subject: Renamed Visitor to Validator::Visitor to conform with Data::DPath::Validator and... X-Git-Tag: 2.002004~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Controller-DBIC-API.git;a=commitdiff_plain;h=8175463b4e1e7bb22f7f83f0eaa3e3a0ad1a70ea;hp=ee4d2dac4cbb91b06c70cdb4e83d9ee178e2571a Renamed Visitor to Validator::Visitor to conform with Data::DPath::Validator and moved it into a separate file --- diff --git a/Changes b/Changes index 4e32aa4..26ec556 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,9 @@ Revision history for Catalyst-Controller-DBIC-API: {{ $dist->version }} {{ $NEXT }} +- Renamed Visitor to Validator::Visitor to conform with Data::DPath::Validator + and moved it into a separate file. + 2.002003 2010-09-16 10:44:40 Europe/Vienna - Added REST and RPC delete_bulk tests diff --git a/lib/Catalyst/Controller/DBIC/API/Validator.pm b/lib/Catalyst/Controller/DBIC/API/Validator.pm index 1c9422f..3155020 100644 --- a/lib/Catalyst/Controller/DBIC/API/Validator.pm +++ b/lib/Catalyst/Controller/DBIC/API/Validator.pm @@ -1,6 +1,7 @@ package Catalyst::Controller::DBIC::API::Validator; #ABSTRACT: Provides validation services for inbound requests against whitelisted parameters use Moose; +use Catalyst::Controller::DBIC::API::Validator::Visitor; use namespace::autoclean; BEGIN { extends 'Data::DPath::Validator'; } @@ -9,104 +10,9 @@ has '+visitor' => ( 'builder' => '_build_custom_visitor' ); sub _build_custom_visitor { - return Catalyst::Controller::DBIC::API::Visitor->new(); + return Catalyst::Controller::DBIC::API::Validator::Visitor->new(); } -Catalyst::Controller::DBIC::API::Validator->meta->make_immutable; +__PACKAGE__->meta->make_immutable; -############################################################################### -package Catalyst::Controller::DBIC::API::Visitor; - -use Moose; -use namespace::autoclean; - -BEGIN { extends 'Data::DPath::Validator::Visitor'; } - -use constant DEBUG => $ENV{DATA_DPATH_VALIDATOR_DEBUG} || 0; - -around visit_array => sub -{ - my ($orig, $self, $array) = @_; - $self->dive(); - warn 'ARRAY: '. $self->current_template if DEBUG; - if(@$array == 1 && $array->[0] eq '*') - { - $self->append_text('[reftype eq "HASH" ]'); - $self->add_template($self->current_template); - } - else - { - if($self->current_template =~ /\/$/) - { - my $temp = $self->current_template; - $self->reset_template(); - $temp =~ s/\/$//; - $self->append_text($temp); - } - $self->$orig($array); - } - $self->rise(); -}; - -sub visit_array_entry -{ - my ($self, $elem, $index, $array) = @_; - $self->dive(); - warn 'ARRAYENTRY: '. $self->current_template if DEBUG; - if(!ref($elem)) - { - $self->append_text($elem . '/*'); - $self->add_template($self->current_template); - } - elsif(ref($elem) eq 'HASH') - { - $self->visit($elem); - } - $self->rise(); - $self->value_type('NONE'); -}; - -around visit_hash => sub -{ - my ($orig, $self, $hash) = @_; - $self->dive(); - if($self->current_template =~ /\/$/) - { - my $temp = $self->current_template; - $self->reset_template(); - $temp =~ s/\/$//; - $self->append_text($temp); - } - warn 'HASH: '. $self->current_template if DEBUG; - $self->$orig($hash); - $self->rise(); -}; - -around visit_value => sub -{ - my ($orig, $self, $val) = @_; - - if($self->value_type eq 'NONE') - { - $self->dive(); - $self->append_text($val . '/*'); - $self->add_template($self->current_template); - warn 'VALUE: ' . $self->current_template if DEBUG; - $self->rise(); - } - elsif($self->value_type eq 'HashKey') - { - $self->append_text($val); - warn 'VALUE: ' . $self->current_template if DEBUG; - } - else - { - $self->$orig($val); - } - -}; - - -Catalyst::Controller::DBIC::API::Visitor->meta->make_immutable; - -1; +1; \ No newline at end of file diff --git a/lib/Catalyst/Controller/DBIC/API/Validator/Visitor.pm b/lib/Catalyst/Controller/DBIC/API/Validator/Visitor.pm new file mode 100644 index 0000000..d256647 --- /dev/null +++ b/lib/Catalyst/Controller/DBIC/API/Validator/Visitor.pm @@ -0,0 +1,95 @@ +package Catalyst::Controller::DBIC::API::Validator::Visitor; +#ABSTRACT: Provides validation services for inbound requests against whitelisted parameters +use Moose; +use namespace::autoclean; + +BEGIN { extends 'Data::DPath::Validator::Visitor'; } + +use constant DEBUG => $ENV{DATA_DPATH_VALIDATOR_DEBUG} || 0; + +around visit_array => sub +{ + my ($orig, $self, $array) = @_; + $self->dive(); + warn 'ARRAY: '. $self->current_template if DEBUG; + if(@$array == 1 && $array->[0] eq '*') + { + $self->append_text('[reftype eq "HASH" ]'); + $self->add_template($self->current_template); + } + else + { + if($self->current_template =~ /\/$/) + { + my $temp = $self->current_template; + $self->reset_template(); + $temp =~ s/\/$//; + $self->append_text($temp); + } + $self->$orig($array); + } + $self->rise(); +}; + +sub visit_array_entry +{ + my ($self, $elem, $index, $array) = @_; + $self->dive(); + warn 'ARRAYENTRY: '. $self->current_template if DEBUG; + if(!ref($elem)) + { + $self->append_text($elem . '/*'); + $self->add_template($self->current_template); + } + elsif(ref($elem) eq 'HASH') + { + $self->visit($elem); + } + $self->rise(); + $self->value_type('NONE'); +}; + +around visit_hash => sub +{ + my ($orig, $self, $hash) = @_; + $self->dive(); + if($self->current_template =~ /\/$/) + { + my $temp = $self->current_template; + $self->reset_template(); + $temp =~ s/\/$//; + $self->append_text($temp); + } + warn 'HASH: '. $self->current_template if DEBUG; + $self->$orig($hash); + $self->rise(); +}; + +around visit_value => sub +{ + my ($orig, $self, $val) = @_; + + if($self->value_type eq 'NONE') + { + $self->dive(); + $self->append_text($val . '/*'); + $self->add_template($self->current_template); + warn 'VALUE: ' . $self->current_template if DEBUG; + $self->rise(); + } + elsif($self->value_type eq 'HashKey') + { + $self->append_text($val); + warn 'VALUE: ' . $self->current_template if DEBUG; + } + else + { + $self->$orig($val); + } + +}; + + +__PACKAGE__->meta->make_immutable; + +1; \ No newline at end of file