Update to add myself to contributors and to hide Modules from the PAUSE Indexer.
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / TempColumns.pm
1 package # hide from PAUSE 
2     DBIx::Class::CDBICompat::TempColumns;
3
4 use strict;
5 use warnings;
6 use base qw/DBIx::Class/;
7
8 __PACKAGE__->mk_classdata('_temp_columns' => { });
9
10 sub _add_column_group {
11   my ($class, $group, @cols) = @_;
12   if ($group eq 'TEMP') {
13     $class->_register_column_group($group => @cols);
14     $class->mk_group_accessors('temp' => @cols);
15     my %tmp = %{$class->_temp_columns};
16     $tmp{$_} = 1 for @cols;
17     $class->_temp_columns(\%tmp);
18   } else {
19     return $class->next::method($group, @cols);
20   }
21 }
22
23 sub new {
24   my ($class, $attrs, @rest) = @_;
25   my %temp;
26   foreach my $key (keys %$attrs) {
27     $temp{$key} = delete $attrs->{$key} if $class->_temp_columns->{$key};
28   }
29   my $new = $class->next::method($attrs, @rest);
30   foreach my $key (keys %temp) {
31     $new->set_temp($key, $temp{$key});
32   }
33   return $new;
34 }
35
36
37 sub find_column {
38   my ($class, $col, @rest) = @_;
39   return $col if $class->_temp_columns->{$col};
40   return $class->next::method($col, @rest);
41 }
42
43 sub get_temp {
44   my ($self, $column) = @_;
45   $self->throw_exception( "Can't fetch data as class method" ) unless ref $self;
46   $self->throw_exception( "No such TEMP column '${column}'" ) unless $self->_temp_columns->{$column} ;
47   return $self->{_temp_column_data}{$column}
48     if exists $self->{_temp_column_data}{$column};
49   return undef;
50 }
51
52 sub set_temp {
53   my ($self, $column, $value) = @_;
54   $self->throw_exception( "No such TEMP column '${column}'" )
55     unless $self->_temp_columns->{$column};
56   $self->throw_exception( "set_temp called for ${column} without value" )
57     if @_ < 3;
58   return $self->{_temp_column_data}{$column} = $value;
59 }
60
61 sub has_real_column {
62   return 1 if shift->has_column(shift);
63 }
64
65 1;