Permalink
Browse files

Implement custom normalization

. add the Abstract class
. changed code to avoid using eval("")
. require custom class only if needed.
. added tests
  • Loading branch information...
1 parent aad2544 commit 3fff5a6ea6c9ae1f6f1c4dfb2cfd66e78952d90e @dams committed Oct 13, 2010
@@ -45,8 +45,13 @@ if (defined $conf->{method} && $conf->{method} ne 'passthrough') {
$method = sub { my ($h) = @_; $apply_on_keys->($h, sub { ucfirst($_[0]) } ) };
} else {
my $class = $conf->{method};
- eval("require $class");
- $@ and die "error while requiring custom normalization class '$class' : $@";
+ my $class_name = $class;
+ $class_name =~ s!::|'!/!g;
+ $class_name .= '.pm';
+ if ( ! $class->can('new') ) {
+ eval { require $class_name };
+ $@ and die "error while requiring custom normalization class '$class' : $@";
+ }
my $abstract_classname = __PACKAGE__ . '::Abstract';
$class->isa(__PACKAGE__ . '::Abstract')
or die "custom normalization class '$class' doesn't inherit from '$abstract_classname'";
@@ -0,0 +1,75 @@
+package Dancer::Plugin::Params::Normalization::Abstract;
+use strict;
+use warnings;
+
+use base 'Dancer::Engine';
+
+# args: ($class)
+# Overload this method in your normalization class if you have some init stuff to do,
+# such as a database connection or making sure a directory exists...
+# sub init { return 1; }
+
+# args: ($self, $hashref)
+# receives a hashref of parameters names/values. It should return a hashrefs of
+# the normalized (modified) parameters.
+sub normalize {
+ die "retrieve not implemented";
+}
+
+# Methods below this this line should not be overloaded.
+
+# The constructor is inherited from Dancer::Engine, itself inherited from Dancer::Object.
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+L<Dancer::Plugin::Params::Normalization::Abstract> - abstract class for custom parameters normalization
+
+=head1 DESCRIPTION
+
+This virtual class describes how to build a custom normalization object for
+L<Dancer::Plugin::Params::Normalization>. This is done in order to allow
+custom transformation of the parameters with a common interface.
+
+Any custom normalization package must inherits from
+L<Dancer::Plugin::Params::Normalization::Abstract> and implement the following
+abstract methods.
+
+=head2 Abstract Methods
+
+=over
+
+=item B<init()>
+
+Is called once, on initialization of the class. Can be used to create needed
+initialization objects, like a database connection, etc.
+
+=item B<normalize($hashref)>
+
+Receives a hashref that contains the parameters keys/value. It should return a
+hashref (it can be the same), containing modified parameters.
+
+=back
+
+=head2 Inherited Methods
+
+None for now.
+
+=head1 LICENCE
+
+This module is released under the same terms as Perl itself.
+
+=head1 AUTHORS
+
+This module has been written by Damien Krotkine <dams@cpan.org>.
+
+=head1 SEE ALSO
+
+L<Dancer>, L<Dancer::Engine>
+
+=cut
@@ -0,0 +1,60 @@
+use strict;
+use warnings;
+use lib qw(../lib);
+use Test::More;
+
+{
+package My::Normalization1;
+use strict;
+use warnings;
+use base qw(Dancer::Plugin::Params::Normalization::Abstract);
+
+# shorten to 3 first caracters
+sub normalize {
+ my ($self, $params) = @_;
+ $params->{substr($_, 0, 3)} = delete $params->{$_} foreach keys %$params;
+ return $params;
+}
+
+}
+plan tests => 2;
+
+{
+ package Webservice;
+ use Dancer;
+
+ BEGIN {
+ set plugins => {
+ 'Params::Normalization' => {
+ method => 'My::Normalization1',
+ },
+ };
+ }
+ use Dancer::Plugin::Params::Normalization;
+
+ # no normalization in this route
+ get '/foo' => sub {
+ return params->{tes};
+ };
+
+ # this route normalizes its parameters names
+ get '/bar' => sub {
+ return params->{ABC};
+ };
+
+
+}
+
+use lib 't';
+use TestUtils;
+
+# 'testing' should be shortened to 'tes'
+my $response = get_response_for_request(GET => '/foo', { testing => 5 });
+is($response->{content}, 5);
+
+# 'ABCLONGNAME' should be shortened to 'ABC'
+$response = get_response_for_request(GET => '/bar', { ABCLONGNAME => 6});
+is($response->{content}, 6);
+
+
+1;
@@ -0,0 +1,43 @@
+use strict;
+use warnings;
+use lib qw(../lib);
+use Test::More;
+use lib 't';
+
+plan tests => 2;
+
+{
+ package Webservice;
+ use Dancer;
+
+ BEGIN {
+ set plugins => {
+ 'Params::Normalization' => {
+ method => 'MyNormalization2',
+ },
+ };
+ }
+ use Dancer::Plugin::Params::Normalization;
+
+ # no normalization in this route
+ get '/foo' => sub {
+ return params->{ing};
+ };
+
+ # this route normalizes its parameters names
+ get '/bar' => sub {
+ return params->{AME};
+ };
+
+
+}
+
+use TestUtils;
+
+# 'testing' should be shortened to 'ing'
+my $response = get_response_for_request(GET => '/foo', { testing => 5 });
+is($response->{content}, 5);
+
+# 'ABCLONGNAME' should be shortened to 'AME'
+$response = get_response_for_request(GET => '/bar', { ABCLONGNAME => 6});
+is($response->{content}, 6);
View
@@ -0,0 +1,13 @@
+package MyNormalization2;
+use strict;
+use warnings;
+use base qw(Dancer::Plugin::Params::Normalization::Abstract);
+
+# shorten to 3 last caracters
+sub normalize {
+ my ($self, $params) = @_;
+ $params->{substr($_, -3, 3)} = delete $params->{$_} foreach keys %$params;
+ return $params;
+}
+
+1;

0 comments on commit 3fff5a6

Please sign in to comment.