X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FTempColumns.pm;h=428719ed2fd0a9e85da358d1c18ea86eb33d4d6a;hb=d4daee7b54e38e4b3d3d0a77759bddc1a4ede6e5;hp=6de7fd33e679b6269f1cb044feb108b64476c871;hpb=147dd158cf91465b8a48adce738d56b85f7d1b9b;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/TempColumns.pm b/lib/DBIx/Class/CDBICompat/TempColumns.pm index 6de7fd3..428719e 100644 --- a/lib/DBIx/Class/CDBICompat/TempColumns.pm +++ b/lib/DBIx/Class/CDBICompat/TempColumns.pm @@ -1,37 +1,57 @@ -package DBIx::Class::CDBICompat::TempColumns; +package # hide from PAUSE + DBIx::Class::CDBICompat::TempColumns; use strict; use warnings; use base qw/Class::Data::Inheritable/; +use Carp; + __PACKAGE__->mk_classdata('_temp_columns' => { }); sub _add_column_group { my ($class, $group, @cols) = @_; - if ($group eq 'TEMP') { - $class->_register_column_group($group => @cols); - $class->mk_group_accessors('temp' => @cols); - my %tmp = %{$class->_temp_columns}; - $tmp{$_} = 1 for @cols; - $class->_temp_columns(\%tmp); - } else { - return $class->next::method($group, @cols); + + return $class->next::method($group, @cols) unless $group eq 'TEMP'; + + my %new_cols = map { $_ => 1 } @cols; + my %tmp_cols = %{$class->_temp_columns}; + + for my $existing_col ( grep $new_cols{$_}, $class->columns ) { + # Already been declared TEMP + next if $tmp_cols{$existing_col}; + + carp "Declaring column $existing_col as TEMP but it already exists"; } + + $class->_register_column_group($group => @cols); + $class->mk_group_accessors('temp' => @cols); + + $class->_temp_columns({ %tmp_cols, %new_cols }); } sub new { my ($class, $attrs, @rest) = @_; - my %temp; - foreach my $key (keys %$attrs) { - $temp{$key} = delete $attrs->{$key} if $class->_temp_columns->{$key}; - } + + my $temp = $class->_extract_temp_data($attrs); + my $new = $class->next::method($attrs, @rest); - foreach my $key (keys %temp) { - $new->set_temp($key, $temp{$key}); - } + + $new->set_temp($_, $temp->{$_}) for keys %$temp; + return $new; } +sub _extract_temp_data { + my($self, $data) = @_; + + my %temp; + foreach my $key (keys %$data) { + $temp{$key} = delete $data->{$key} if $self->_temp_columns->{$key}; + } + + return \%temp; +} sub find_column { my ($class, $col, @rest) = @_; @@ -39,10 +59,20 @@ sub find_column { return $class->next::method($col, @rest); } +sub set { + my($self, %data) = @_; + + my $temp_data = $self->_extract_temp_data(\%data); + + $self->set_temp($_, $temp_data->{$_}) for keys %$temp_data; + + return $self->next::method(%data); +} + sub get_temp { my ($self, $column) = @_; - $self->throw( "Can't fetch data as class method" ) unless ref $self; - $self->throw( "No such TEMP column '${column}'" ) unless $self->_temp_columns->{$column} ; + $self->throw_exception( "Can't fetch data as class method" ) unless ref $self; + $self->throw_exception( "No such TEMP column '${column}'" ) unless $self->_temp_columns->{$column} ; return $self->{_temp_column_data}{$column} if exists $self->{_temp_column_data}{$column}; return undef; @@ -50,9 +80,9 @@ sub get_temp { sub set_temp { my ($self, $column, $value) = @_; - $self->throw( "No such TEMP column '${column}'" ) + $self->throw_exception( "No such TEMP column '${column}'" ) unless $self->_temp_columns->{$column}; - $self->throw( "set_temp called for ${column} without value" ) + $self->throw_exception( "set_temp called for ${column} without value" ) if @_ < 3; return $self->{_temp_column_data}{$column} = $value; }