| 1 | |||
| 2 | package Object::I18n::Storage::CDBI; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | sub new { | ||
| 8 | my $class = shift; | ||
| 9 | my ($obj, $method) = @_; | ||
| 10 | my $self = bless { | ||
| 11 | object => $obj, | ||
| 12 | method => $method, | ||
| 13 | }, $class; | ||
| 14 | $self->init; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub init { | ||
| 18 | shift; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub fetch { | ||
| 22 | my $self = shift; | ||
| 23 | my $i18n = $self->{object}->i18n; | ||
| 24 | |||
| 25 | my $class = $i18n->{class}; | ||
| 26 | my $oid = $i18n->oid; | ||
| 27 | my $method = $self->{method}; | ||
| 28 | my $language= $i18n->language; | ||
| 29 | |||
| 30 | my $cdbi_class = $self->{cdbi_class} or croak "cdbi_class undefined"; | ||
| 31 | my ($obj) = $cdbi_class->search( | ||
| 32 | class => $class, | ||
| 33 | instance => $oid, | ||
| 34 | attr => $method, | ||
| 35 | language => $language, | ||
| 36 | ) or return; | ||
| 37 | return $obj->data; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub store { | ||
| 41 | my $self = shift; | ||
| 42 | my ($data) = @_; | ||
| 43 | my $i18n = $self->{object}->i18n; | ||
| 44 | |||
| 45 | my $class = $i18n->{class}; | ||
| 46 | my $oid = $i18n->oid; | ||
| 47 | my $method = $self->{method}; | ||
| 48 | my $language= $i18n->language; | ||
| 49 | |||
| 50 | my $cdbi_class = $self->{cdbi_class} or croak "cdbi_class undefined"; | ||
| 51 | my ($obj) = $cdbi_class->find_or_create({ | ||
| 52 | class => $class, | ||
| 53 | instance => $oid, | ||
| 54 | attr => $method, | ||
| 55 | language => $language, | ||
| 56 | }); | ||
| 57 | die "could not get a '$cdbi_class' object to store into" unless $obj; | ||
| 58 | $obj->data($data); | ||
| 59 | $obj->update; | ||
| 60 | return $obj->data if defined wantarray; | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; | ||
| 64 |