Skip to content

Commit

Permalink
Update documentation to show how to use a bank instance as a coder.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 6, 2023
1 parent abbe5d1 commit 6bb5e71
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion guides/activerecord-integration/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Latinum can be easily used in a [ActiveRecord](https://github.com/rails/rails/tr

~~~ ruby
class Transaction < ActiveRecord::Base
serialize :total, Latinum::Resource
serialize :total, coder: Latinum::Resource
end
~~~

Expand All @@ -33,6 +33,23 @@ bank.format(transaction.total, symbol: nil)
# => "20.00 NZD"
~~~

## Bank Serialization

If you want to accept ambiguous input, e.g. `$5` and have the bank determine the currency, you can use the `Latinum::Bank` as the serializer:

~~~ ruby
require 'latinum/bank'
require 'latinum/currencies/global'

class Transaction < ActiveRecord::Base
BANK = Latinum::Bank.new(Latinum::Currency::Global)

serialize :total, coder: BANK
end
~~~

This will store the same format as the `Latinum::Resource` serializer, but will allow you to use the bank to determine the currency. When several symbols share the same prefix (e.g. `$`), the bank will choose the currency with the highest priority.

## Conversion To and From Integers

For storage in traditional databases, you may prefer to use integers. Based on the precision of the currency, you can store integer representations:
Expand Down

0 comments on commit 6bb5e71

Please sign in to comment.