X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FTempColumns.pm;h=9783d6ae72e21429bf2c7b72fea3ed73dadcb658;hb=51ec03826afb5b20a686a7303bc55c42f4715945;hp=95be2a8bbdc60c600928124e728698b56a21e9aa;hpb=75d079145a507a0e5ff89b2676d383f4fd1a5511;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/TempColumns.pm b/lib/DBIx/Class/CDBICompat/TempColumns.pm index 95be2a8..9783d6a 100644 --- a/lib/DBIx/Class/CDBICompat/TempColumns.pm +++ b/lib/DBIx/Class/CDBICompat/TempColumns.pm @@ -3,36 +3,56 @@ package # hide from PAUSE use strict; use warnings; -use base qw/DBIx::Class/; +use base 'DBIx::Class'; + +use Carp; +use namespace::clean; __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) = @_; @@ -40,6 +60,16 @@ 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_exception( "Can't fetch data as class method" ) unless ref $self;