File Coverage

blib/lib/Karas/Row.pm
Criterion Covered Total %
statement 45 56 80.3
branch 13 28 46.4
condition n/a
subroutine 12 17 70.5
pod 5 10 50.0
total 75 111 67.5


line stmt bran cond sub pod time code
1             package Karas::Row;
2 9     9   60 use strict;
  9         20  
  9         336  
3 9     9   52 use warnings;
  9         16  
  9         258  
4 9     9   2648 use utf8;
  9         24  
  9         78  
5 9     9   209 use Carp ();
  9         20  
  9         4207  
6              
7             sub new {
8 18     18 0 36 my ($class, $values) = @_;
9 18         291 bless {
10             __private_dirty_column => +{},
11             %$values,
12             }, $class;
13             }
14              
15             # Abstract methods.
16 0     0 1 0 sub table_name { Carp::croak("Abstract method"); }
17 0     0 1 0 sub primary_key { Carp::croak("Abstract method"); }
18 0     0 0 0 sub column_names { Carp::croak("Abstract method"); }
19             sub has_column {
20 2     2 0 2862 my ($self, $column) = @_;
21 2         51 return (grep { $_ eq $column } $self->column_names) > 0;
  4         28  
22             }
23              
24 1     1 0 8 sub get_dirty_columns { $_[0]->{__private_dirty_column} }
25              
26             sub mk_column_accessors {
27 9     9 1 2658 my ($class, @cols) = @_;
28 9 50       50 Carp::croak("mk_column_accessors is class method.") if ref $class;
29              
30 9         23 for my $col (@cols) {
31 21 50       60 Carp::croak("Column is undefined") unless defined $col;
32 21 50       55 Carp::croak("Invalid column name: $col") if $col =~ /^__private/;
33 9     9   1498 no strict 'refs';
  9         16  
  9         5743  
34 21         174 *{"${class}::${col}"} = sub {
35 12 100   12   52 if (@_==1) {
    50          
36             # my ($self) = @_;
37 11 50       60 Carp::croak("You don't selected $col in SQL.") unless exists $_[0]->{$col};
38 11         65 return $_[0]->{$col};
39             } elsif (@_==2) {
40             # my ($self, $val) = @_;
41 1 50       5 Carp::croak("You can't set non scalar value as column data: $col") if ref $_[1];
42 1         5 $_[0]->{$col} = ($_[0]->{__private_dirty_column}->{$col} = $_[1]);
43             } else {
44 0         0 Carp::croak("Too many arguments for ${class}::${col}");
45             }
46 21         108 };
47             }
48             }
49              
50             sub make_where_condition {
51 2     2 0 3 my $self = shift;
52 2 50       8 unless ($self->primary_key()) {
53 0         0 Carp::croak("You can't get WHERE clause for @{[ $self->table_name ]}. There is no primary key settings.");
  0         0  
54             }
55 2         3 my %cond;
56 2         5 for my $key ($self->primary_key) {
57 2         9 $cond{$key} = $self->get_column($key);
58             }
59 2         8 return \%cond;
60             }
61              
62             sub get_column {
63 2     2 1 5 my ($self, $col) = @_;
64 2 50       6 Carp::croak("Usage: Karas::Row#get_column(\$col)") unless @_==2;
65 2 50       5 Carp::croak("Column is undefined") unless defined $col;
66 2 50       7 Carp::croak("You don't selected $col") unless exists $self->{$col};
67 2 50       6 Carp::croak("Invalid column name: $col") if $col =~ /^__private/;
68 2         8 return $self->{$col};
69             }
70              
71             sub set_column {
72 0     0 1   my ($self, $col, $val) = @_;
73 0 0         Carp::croak("Usage: Karas::Row#set_column(\$col, \$val)") unless @_==3;
74 0 0         Carp::croak("You can't set non scalar value as column data: $col") if ref $val;
75 0           $self->{__private_dirty_column}->{$col} = $val;
76             }
77              
78 0     0     sub DESTROY { }
79              
80             1;
81             __END__