Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated the object keyword and functionality

  • Loading branch information...
commit d4c8caef9480052b65230f233947bff15447e84c 1 parent d98cf8a
@alnewkirk authored
View
3  CHANGES
@@ -5,6 +5,9 @@ TODO (TBD)
? write new POD about cooperating with existing OO systems
? added a "TODO" section of POD to the main module
+7.79 (2012-07-28)
+ * updated the object keyword and functionality (untested)
+
7.78 (2012-07-28)
* changed flatten/unflatten functionality, now only happens during validation
* fixed unneccessary serialization, field and values are always as expected
View
2  Makefile.PL
@@ -27,7 +27,7 @@ my %WriteMakefileArgs = (
"Module::Runtime" => 0,
"utf8" => 0
},
- "VERSION" => "7.78",
+ "VERSION" => "7.79",
"test" => {
"TESTS" => "t/*.t t/regression/*.t t/regression/filters/*.t t/regression/validators/*.t"
}
View
60 README
@@ -3,7 +3,7 @@ NAME
Framework
VERSION
- version 7.78
+ version 7.79
SYNOPSIS
package MyApp::User;
@@ -486,51 +486,41 @@ KEYWORDS
class attributes, this method does not cache or otherwise store the
returned class object it constructs.
- package MyApp::Database;
+ package MyApp::User::Input;
- use DBI;
- use Validation::Class;
+ use MyApp::User; # a Moose class
- field name => {
- required => 1,
- };
-
- field host => {
- required => 1,
- };
+ use Validation::Class;
- field port => {
+ field login => {
required => 1,
};
- field user => {
+ field password => {
required => 1,
};
- field pass => {
- # ...
- };
+ object _build_user => {
+
+ type => 'MyApp::User',
+
+ # init => 'new', # defaults to new
+ # args => [qw/login password/], # defaults to all fields
+ # list => 'hash' # defaults to list
- object connect => {
- type => 'DBI',
- init => 'connect', # defaults to new
- args => sub {
- join(':', 'dbi', 'mysql', @{$_[0]}{qw/name host port/}),
- @{$_[0]}{qw/user pass/})
- ;
- }
};
package main;
- my $database = MyApp::Database->new(
- name => 'test',
- host => 'localhost',
- port => '3306',
- user => 'root'
- );
+ my $user_input = MyApp::User::Input->new(params => $params);
- $database->connect or die $DBI::errstr;
+ if ($user_input->validate('login', 'password')) {
+
+ my $user = $user_input->_build_user;
+
+ # ...
+
+ }
The object keyword takes two arguments, an object builder name and a
hashref of key/value pairs which are used to instruct the builder on how
@@ -543,8 +533,14 @@ KEYWORDS
# optional: constructor name (defaults to new)
init => 'new',
- # optional: coderef which returns arguments for the constructor
+ # optional: arrayref of field names or a coderef which returns arguments for
+ # the constructor (defaults to all fields)
args => sub {}
+
+ # optional: if supplying an arrayref of fields to the args option, this
+ # option determines how the parameters will be supplied to the constructor,
+ # valid options are 'hash' or 'list' (defaults to list)
+ list => 'hash'
profile
The profile keyword (or pro) stores a validation profile (coderef) which
View
65 README.mkdn
@@ -4,7 +4,7 @@ Validation::Class - Self-Validating Object System and Data Validation Framework
# VERSION
-version 7.78
+version 7.79
# SYNOPSIS
@@ -587,61 +587,55 @@ the calling class which invokes the builder. Unlike class attributes, this
method does not cache or otherwise store the returned class object it
constructs.
- package MyApp::Database;
+ package MyApp::User::Input;
- use DBI;
- use Validation::Class;
+ use MyApp::User; # a Moose class
- field name => {
- required => 1,
- };
+ use Validation::Class;
- field host => {
+ field login => {
required => 1,
};
- field port => {
+ field password => {
required => 1,
};
- field user => {
- required => 1,
- };
-
+ object _build_user => {
+
- field pass => {
- # ...
- };
+ type => 'MyApp::User',
+
+
+ # init => 'new', # defaults to new
+ # args => [qw/login password/], # defaults to all fields
+ # list => 'hash' # defaults to list
- object connect => {
- type => 'DBI',
- init => 'connect', # defaults to new
- args => sub {
- join(':', 'dbi', 'mysql', @{$_[0]}{qw/name host port/}),
- @{$_[0]}{qw/user pass/})
- ;
- }
};
package main;
- my $database = MyApp::Database->new(
- name => 'test',
- host => 'localhost',
- port => '3306',
- user => 'root'
- );
+ my $user_input = MyApp::User::Input->new(params => $params);
- $database->connect or die $DBI::errstr;
+ if ($user_input->validate('login', 'password')) {
+
+
+ my $user = $user_input->_build_user;
+
+
+ # ...
+
+
+ }
The object keyword takes two arguments, an object builder name and a hashref
of key/value pairs which are used to instruct the builder on how to construct
@@ -655,8 +649,15 @@ the object. The supplied hashref should be configured as follows:
init => 'new',
- # optional: coderef which returns arguments for the constructor
+ # optional: arrayref of field names or a coderef which returns arguments for
+ # the constructor (defaults to all fields)
args => sub {}
+
+
+ # optional: if supplying an arrayref of fields to the args option, this
+ # option determines how the parameters will be supplied to the constructor,
+ # valid options are 'hash' or 'list' (defaults to list)
+ list => 'hash'
## profile
View
2  dist.ini
@@ -4,7 +4,7 @@ author = Al Newkirk <anewkirk@ana.io>
license = Perl_5
copyright_holder = Al Newkirk
copyright_year = 2011
-version = 7.78
+version = 7.79
[MetaResources]
homepage = https://github.com/alnewkirk/Validation-Class
View
97 lib/Validation/Class.pm
@@ -1323,51 +1323,41 @@ the calling class which invokes the builder. Unlike class attributes, this
method does not cache or otherwise store the returned class object it
constructs.
- package MyApp::Database;
+ package MyApp::User::Input;
- use DBI;
- use Validation::Class;
-
- field name => {
- required => 1,
- };
+ use MyApp::User; # a Moose class
- field host => {
- required => 1,
- };
+ use Validation::Class;
- field port => {
+ field login => {
required => 1,
};
- field user => {
+ field password => {
required => 1,
};
- field pass => {
- # ...
- };
+ object _build_user => {
+
+ type => 'MyApp::User',
+
+ # init => 'new', # defaults to new
+ # args => [qw/login password/], # defaults to all fields
+ # list => 'hash' # defaults to list
- object connect => {
- type => 'DBI',
- init => 'connect', # defaults to new
- args => sub {
- join(':', 'dbi', 'mysql', @{$_[0]}{qw/name host port/}),
- @{$_[0]}{qw/user pass/})
- ;
- }
};
package main;
- my $database = MyApp::Database->new(
- name => 'test',
- host => 'localhost',
- port => '3306',
- user => 'root'
- );
+ my $user_input = MyApp::User::Input->new(params => $params);
- $database->connect or die $DBI::errstr;
+ if ($user_input->validate('login', 'password')) {
+
+ my $user = $user_input->_build_user;
+
+ # ...
+
+ }
The object keyword takes two arguments, an object builder name and a hashref
of key/value pairs which are used to instruct the builder on how to construct
@@ -1379,8 +1369,14 @@ the object. The supplied hashref should be configured as follows:
# optional: constructor name (defaults to new)
init => 'new',
- # optional: coderef which returns arguments for the constructor
+ # optional: arrayref of field names or a coderef which returns arguments for
+ # the constructor (defaults to all fields)
args => sub {}
+
+ # optional: if supplying an arrayref of fields to the args option, this
+ # option determines how the parameters will be supplied to the constructor,
+ # valid options are 'hash' or 'list' (defaults to list)
+ list => 'hash'
=cut
@@ -1425,12 +1421,45 @@ sub object {
my $validator;
my $type = $data->{'type'};
- my $init = $data->{'init'} ||= 'new';
my $args = $data->{'args'};
+ my $init = $data->{'init'} ||= 'new';
+ my $list = $data->{'list'} ||= 'list';
- my @params = ($args->($self)) if "CODE" eq ref $args;
+ my @params = ();
- # maybe merge @params with @args or vice versa ???
+ if ("CODE" eq ref $args) {
+
+ @params = ($args->($self));
+
+ }
+
+ else {
+
+ my $params = {};
+
+ my $fields = "ARRAY" eq ref $args ? $args :
+ [$proto->fields->keys]
+ ;
+
+ foreach my $field (@{$fields}) {
+
+ $params->{$field} = $proto->get_value($field);
+
+ }
+
+ if ($list eq 'hash') {
+
+ push @params, $params;
+
+ }
+
+ else {
+
+ push @params, %{$params};
+
+ }
+
+ }
if (my $instance = $type->$init(@params)) {
View
6 lib/Validation/Class/Intro.pod
@@ -589,11 +589,7 @@ classes. The following is an example of this:
my @conn_str_parts =
('dbi', 'mysql', $self->name, $self->host, $self->port);
- return (
- join(':', @conn_str_parts),
- $self->user,
- $self->pass
- )
+ return (join(':', @conn_str_parts), $self->user, $self->pass)
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.