Fix class name typo
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / CDBICompat / TempColumns.pm
CommitLineData
75d07914 1package # hide from PAUSE
c0e7b4e5 2 DBIx::Class::CDBICompat::TempColumns;
a1373054 3
4use strict;
5use warnings;
75a23b3e 6use base qw/DBIx::Class/;
a1373054 7
8__PACKAGE__->mk_classdata('_temp_columns' => { });
9
10sub _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 {
147dd158 19 return $class->next::method($group, @cols);
a1373054 20 }
21}
22
23sub 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 }
147dd158 29 my $new = $class->next::method($attrs, @rest);
a1373054 30 foreach my $key (keys %temp) {
31 $new->set_temp($key, $temp{$key});
32 }
33 return $new;
34}
35
36
37sub find_column {
38 my ($class, $col, @rest) = @_;
39 return $col if $class->_temp_columns->{$col};
147dd158 40 return $class->next::method($col, @rest);
a1373054 41}
42
43sub get_temp {
44 my ($self, $column) = @_;
701da8c4 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} ;
a1373054 47 return $self->{_temp_column_data}{$column}
48 if exists $self->{_temp_column_data}{$column};
49 return undef;
50}
51
52sub set_temp {
53 my ($self, $column, $value) = @_;
701da8c4 54 $self->throw_exception( "No such TEMP column '${column}'" )
a1373054 55 unless $self->_temp_columns->{$column};
701da8c4 56 $self->throw_exception( "set_temp called for ${column} without value" )
a1373054 57 if @_ < 3;
58 return $self->{_temp_column_data}{$column} = $value;
59}
60
61sub has_real_column {
103647d5 62 return 1 if shift->has_column(shift);
a1373054 63}
64
651;