| 1 | package DBIx::Class::InflateColumn::Currency; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | our $VERSION = '0.02005'; | ||
| 5 | |||
| 6 | BEGIN { | ||
| 7 | use base qw/DBIx::Class Class::Accessor::Grouped/; | ||
| 8 | __PACKAGE__->mk_group_accessors('inherited', qw/ | ||
| 9 | currency_class currency_code currency_format currency_code_column | ||
| 10 | /); | ||
| 11 | }; | ||
| 12 | |||
| 13 | __PACKAGE__->currency_class('Data::Currency'); | ||
| 14 | |||
| 15 | sub register_column { | ||
| 16 | my ($self, $column, $info, @rest) = @_; | ||
| 17 | $self->next::method($column, $info, @rest); | ||
| 18 | |||
| 19 | return unless defined $info->{'is_currency'}; | ||
| 20 | |||
| 21 | my $currency_class = $info->{'currency_class'} || $self->currency_class || 'Data::Currency'; | ||
| 22 | my $currency_code = $info->{'currency_code'} || $self->currency_code; | ||
| 23 | my $currency_code_column = $info->{'currency_code_column'} || $self->currency_code_column; | ||
| 24 | my $currency_format = $info->{'currency_format'} || $self->currency_format; | ||
| 25 | |||
| 26 | eval "use $currency_class"; | ||
| 27 | $self->throw_exception("Error loading $currency_class: $@") if $@; | ||
| 28 | |||
| 29 | $self->inflate_column( | ||
| 30 | $column => { | ||
| 31 | inflate => sub { | ||
| 32 | my ($value, $obj) = @_; | ||
| 33 | my $code = $currency_code_column ? | ||
| 34 | $obj->$currency_code_column || $currency_code : | ||
| 35 | $currency_code; | ||
| 36 | |||
| 37 | return $currency_class->new($value, $code, $currency_format); | ||
| 38 | }, | ||
| 39 | deflate => sub { | ||
| 40 | return shift->value; | ||
| 41 | }, | ||
| 42 | } | ||
| 43 | ); | ||
| 44 | }; | ||
| 45 | |||
| 46 | 1; | ||
| 47 | __END__ |