From: Dan Kubb Date: Sat, 13 Aug 2005 18:40:25 +0000 (+0000) Subject: Refactored common code from Validation::Type:* into Validation X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6fb69126f21388952ad7ca1f787549fd1578807b;p=dbsrgits%2FDBIx-Class-Historic.git Refactored common code from Validation::Type:* into Validation Changed PK to use class methods for accessing the list of primary keys, instead of direct hash access --- diff --git a/lib/DBIx/Class/PK.pm b/lib/DBIx/Class/PK.pm index ed0b830..ca1770b 100644 --- a/lib/DBIx/Class/PK.pm +++ b/lib/DBIx/Class/PK.pm @@ -28,28 +28,26 @@ and depending on them. sub _ident_cond { my ($class) = @_; - return join(" AND ", map { "$_ = ?" } keys %{$class->_primaries}); + return join " AND ", map { "$_ = ?" } $class->primary_columns; } sub _ident_values { my ($self) = @_; - return (map { $self->{_column_data}{$_} } keys %{$self->_primaries}); + return map { $self->{_column_data}{$_} } $self->primary_columns; } sub set_primary_key { my ($class, @cols) = @_; - my %pri; - tie %pri, 'Tie::IxHash'; - %pri = map { $_ => {} } @cols; + $class->add_columns(@cols); + tie my %pri, 'Tie::IxHash', map { $_ => {} } @cols; $class->_primaries(\%pri); } sub find { my ($class, @vals) = @_; my $attrs = (@vals > 1 && ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {}); - my @pk = keys %{$class->_primaries}; $class->throw( "Can't find unless primary columns are defined" ) - unless @pk; + unless my @pk = $class->primary_columns; my $query; if (ref $vals[0] eq 'HASH') { $query = $vals[0]; diff --git a/lib/DBIx/Class/Table.pm b/lib/DBIx/Class/Table.pm index 2ed5fd6..ddcb8d7 100644 --- a/lib/DBIx/Class/Table.pm +++ b/lib/DBIx/Class/Table.pm @@ -44,7 +44,7 @@ sub _register_columns { foreach my $name (@cols) { $class->set_field_column_name($name => $name); $class->_columns->{$name}{field} = $class->get_field($name); - } + } } sub _mk_column_accessors { diff --git a/lib/DBIx/Class/Validation.pm b/lib/DBIx/Class/Validation.pm index 7455046..f668ca3 100644 --- a/lib/DBIx/Class/Validation.pm +++ b/lib/DBIx/Class/Validation.pm @@ -124,6 +124,19 @@ use Class::Std; return; } + + sub _add_types_to_field : method { + my ( $class, $field_name, @type_names ) = @_; + + my $field = $class->get_field($field_name); + my $field_class = ref $field; + + no strict 'refs'; + push @{"${field_class}::ISA"}, + grep { !$field->isa($_) } + map { "DBIx::Class::Field::Type::$_" } + @type_names; + } } 1; diff --git a/lib/DBIx/Class/Validation/Type/column.pm b/lib/DBIx/Class/Validation/Type/column.pm index 29a7233..9a07d8b 100644 --- a/lib/DBIx/Class/Validation/Type/column.pm +++ b/lib/DBIx/Class/Validation/Type/column.pm @@ -9,24 +9,10 @@ use Class::Std; sub set_field_column_name : method { my ( $class, $field_name, $column_name ) = @_; - $class->_add_column_type_to_field($field_name); + $class->_add_types_to_field($field_name => 'column'); return shift->get_field($field_name)->set_column_name($column_name); } - - sub _add_column_type_to_field : PRIVATE method { - my ( $class, $field_name, $opt ) = @_; - - my $field = $class->get_field($field_name); - my $field_class = ref $field; - - no strict 'refs'; - push @{"${field_class}::ISA"}, - grep { !$field->isa($_) } - qw( DBIx::Class::Field::Type::column ); - - return; - } } 1; diff --git a/lib/DBIx/Class/Validation/Type/number.pm b/lib/DBIx/Class/Validation/Type/number.pm index 0047528..d83af31 100644 --- a/lib/DBIx/Class/Validation/Type/number.pm +++ b/lib/DBIx/Class/Validation/Type/number.pm @@ -9,13 +9,7 @@ use Class::Std; sub validates_numericality_of : method { my ( $class, $field_name, $opt ) = @_; - my $field = $class->get_field($field_name); - my $field_class = ref $field; - - no strict 'refs'; - push @{"${field_class}::ISA"}, - grep { !$field->isa($_) } - qw( DBIx::Class::Field::Type::number ); + $class->_add_types_to_field($field_name => 'number'); # TODO: set up trigger points based on the $opt passed in. Want # to be able to validate during different DBIx::Class diff --git a/lib/DBIx/Class/Validation/Type/object.pm b/lib/DBIx/Class/Validation/Type/object.pm index 6bfd027..bab6860 100644 --- a/lib/DBIx/Class/Validation/Type/object.pm +++ b/lib/DBIx/Class/Validation/Type/object.pm @@ -9,7 +9,7 @@ use Class::Std; sub validates_roles_of : method { my ( $class, $field_name, $roles, $opt ) = @_; - $class->_add_object_type_to_field($field_name); + $class->_add_types_to_field($field_name => 'object'); $class->get_field($field_name)->set_roles($roles); @@ -23,7 +23,7 @@ use Class::Std; sub validates_classes_of : method { my ( $class, $field_name, $classes, $opt ) = @_; - $class->_add_object_type_to_field($field_name); + $class->_add_types_to_field($field_name => 'object'); $class->get_field($field_name)->set_classes($classes); @@ -33,20 +33,6 @@ use Class::Std; return; } - - sub _add_object_type_to_field : PRIVATE method { - my ( $class, $field_name, $opt ) = @_; - - my $field = $class->get_field($field_name); - my $field_class = ref $field; - - no strict 'refs'; - push @{"${field_class}::ISA"}, - grep { !$field->isa($_) } - qw( DBIx::Class::Field::Type::object ); - - return; - } } 1; diff --git a/lib/DBIx/Class/Validation/Type/string.pm b/lib/DBIx/Class/Validation/Type/string.pm index 9fcc9c2..8ffa202 100644 --- a/lib/DBIx/Class/Validation/Type/string.pm +++ b/lib/DBIx/Class/Validation/Type/string.pm @@ -9,7 +9,7 @@ use Class::Std; sub validates_length_of : method { my ( $class, $field_name, $opt ) = @_; - $class->_add_string_type_to_field($field_name); + $class->_add_types_to_field($field_name => 'string'); my $field = $class->get_field($field_name); @@ -31,7 +31,7 @@ use Class::Std; sub validates_allowed_chars_of : method { my ( $class, $field_name, $allowed_chars, $opt ) = @_; - $class->_add_string_type_to_field($field_name); + $class->_add_types_to_field($field_name => 'string'); $class->get_field($field_name) ->set_allowed_chars($allowed_chars); @@ -46,7 +46,7 @@ use Class::Std; sub validates_disallowed_chars_of : method { my ( $class, $field_name, $disallowed_chars, $opt ) = @_; - $class->_add_string_type_to_field($field_name); + $class->_add_types_to_field($field_name => 'string'); $class->get_field($field_name) ->set_disallowed_chars($disallowed_chars); @@ -61,7 +61,7 @@ use Class::Std; sub validates_format_of : method { my ( $class, $field_name, $format, $opt ) = @_; - $class->_add_string_type_to_field($field_name); + $class->_add_types_to_field($field_name => 'string'); $class->get_field($field_name) ->set_format($format); @@ -72,20 +72,6 @@ use Class::Std; return; } - - sub _add_string_type_to_field : PRIVATE method { - my ( $class, $field_name, $opt ) = @_; - - my $field = $class->get_field($field_name); - my $field_class = ref $field; - - no strict 'refs'; - push @{"${field_class}::ISA"}, - grep { !$field->isa($_) } - qw( DBIx::Class::Field::Type::string ); - - return; - } } 1;