File: Potential.pm

package info (click to toggle)
libalzabo-perl 0.87-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,124 kB
  • ctags: 771
  • sloc: perl: 14,590; makefile: 46
file content (125 lines) | stat: -rw-r--r-- 2,479 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package Alzabo::Runtime::RowState::Potential;

use strict;

use Alzabo::Runtime;

use base qw(Alzabo::Runtime::RowState::Live);

sub _init
{
    my $class = shift;
    my $row = shift;
    my %p = @_;

    # Can't just call ->update here cause with MethodMaker there may
    # be update hooks that probably shouldn't be invoked here.
    foreach ( keys %{ $p{values} } )
    {
        # This will throw an exception if the column doesn't exist.
        my $c = $row->table->column($_);

        Alzabo::Exception::Params->throw( error => "Column " . $c->name . " cannot be null." )
            unless defined $p{values}->{$_} || $c->nullable || defined $c->default;

        $row->{data}{$_} = $p{values}->{$_};
    }

    foreach my $c ( $row->table->columns )
    {
        if ( defined $c->default )
        {
            my $name = $c->name;
            $row->{data}{$name} = $c->default unless defined $row->{data}{$name};
        }
    }

    return 1;
}

sub _get_data
{
    my $class = shift;
    my $row = shift;

    my %data;
    @data{@_} = @{ $row->{data} }{@_};

    return %data;
}

sub update
{
    my $class = shift;
    my $row = shift;
    my %data = @_;

    foreach my $k (keys %data)
    {
        # This will throw an exception if the column doesn't exist.
        my $c = $row->table->column($k);

        Alzabo::Exception::NotNullable->throw
            ( error => $c->name . " column in " . $row->table->name . " table cannot be null.",
              column_name => $c->name,
            )
                unless defined $data{$k} || $c->nullable || defined $c->default;
    }

    my $changed = 0;
    while ( my ( $k, $v ) = each %data )
    {
        next if $row->_cached_data_is_same( $k, $data{$k} );

        $row->{data}{$k} = $v;
        $changed = 1;
    }

    return $changed;
}

# doesn't need to do anything
sub refresh { }

sub delete
{
    $_[1]->set_state( 'Alzabo::Runtime::RowState::Deleted' );
}

sub id_as_string { '' }

sub is_potential { 1 }

sub is_live { 0 }

sub is_deleted { 0 }


1;

__END__

=head1 NAME

Alzabo::Runtime::RowState::Potential - Row objects that are not in the database

=head1 SYNOPSIS

  my $row = $table->potential_row;

  $row->make_live;  # $row is now a _real_ row object!

=head1 DESCRIPTION

This state is used for potential rows, rows which do not yet exist in
the database.

=head1 METHODS

See L<C<Alzabo::Runtime::Row>|Alzabo::Runtime::Row>.

=head1 AUTHOR

Dave Rolsky, <autarch@urth.org>

=cut