Just so I could 'set it and forget it' off of my plate, I finally
cleaned up test coverage and released a version of the currency
inflation component for DBIx::Class.

It should be available on a CPAN mirror near you:
http://search.cpan.org/dist/DBIx::Class::InflateColumn::Currency/

This component inflates any column labeled as a currency column into
Data::Currency objects much like the DateTime component inflates columns
to DateTime objects:

package Item;
__PACKAGE__->load_components(qw/InflateColumn::Currency/);
__PACKAGE__->add_columns(
  price => {
    data_type       => 'decimal',
    size            => [9,2],
    is_nullable     => 0,
    default_value   => '0.00',
    is_currency     => 1,
    currency_code   => 'CAD',
    currency_format => 'FMT_SYMBOL',
    currency_class  => 'Custom::data::Currency'
  }
);

Currency columns can be of any type, provided they return something that
looks like a number. The options can be specified per column, or
globally at the result source level:

package Item;
__PACKAGE__->load_components(qw/InflateColumn::Currency/);
__PACKAGE__->currency_code('CAD');
__PACKAGE__->currency_format('FMT_SYMBOL');
__PACKAGE__->currency_class('Custom::data::Currency');
__PACKAGE__->add_columns(
  price => {
    data_type       => 'decimal',
    size            => [9,2],
    is_nullable     => 0,
    default_value   => '0.00',
    is_currency     => 1,
  }
);

Just as an added twist, you can also override the currency code setting
on a per row basis using currency_code_column:

package Item;
__PACKAGE__->load_components(qw/InflateColumn::Currency/);
__PACKAGE__->currency_code('CAD');
__PACKAGE__->currency_format('FMT_SYMBOL');
__PACKAGE__->currency_class('Custom::data::Currency');
__PACKAGE__->currency_code_column('ccode');
__PACKAGE__->add_columns(
  price => {
    data_type       => 'decimal',
    size            => [9,2],
    is_nullable     => 0,
    default_value   => '0.00',
    is_currency     => 1,
  },
  cdode => {
    data_type => 'varchar',
    size      => 3
  }
);

## row 1: returns the default source CAD currency
1.23, undef

## row 2: returns USD for this row
1.95, 'USD'


Happy currency-ing!
-=Chris

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive: http://www.mail-archive.com/[email protected]/

Reply via email to