Code Monkey home page Code Monkey logo

latinum's Introduction

Latinum

Latinum is a library for resource and money calculations. It provides immutable {ruby Latinum::Resource} objects for dealing with quantities of named resources with an arbitrary number of decimal places, and {ruby Latinum::Bank} objects for converting resources and formatting them for output. Latinum doesn't include any global state by design and thus is ideal for integration with other frameworks/libraries.

Development Status

Motivation

I was originally planning on using the Money gem, but it's dependency on global state makes it hard to use if you want to deal with money as an immutable value type.

Additionally, I wanted to support BitCoin, Japanese Yen, etc. The money gem was heavily biased towards decimal currency. It had (~2012) fields like dollars and cents which don't really make sense and don't really align with the real world. These days they have fixed parts of the API, but it's a bit of a mess now, supporting both decimal and non-decimal values.

Another problem I had at the time was the concept of zero. It should be possible to have an additive (e.g. 0) and multiplicative identity (e.g. 1) do the right thing. In fact, in Latinum, you can multiply Latinum::Resource instances by a scalar and get a useful result (e.g. for computing discounts).

Finally, because of the above problem, it was not obvious at the time how to sum up a collection of money instances correctly. In fact, this is still a problem and a separate gem, based on the Latinum::Collection concept, was made. However, this all fits together in a rather haphazard way.

Latinum addresses all these issues. It has an immutable value type Latinum::Resource which has a robust definition: A value (e.g. 5.0025) and a resource name (USD). The semantics of resources are well defined without the need for "Currency" state like the symbol, how many decimal places, etc. So, it suits well for serialization into a database, and for formatting to the user, there is Latinum::Bank which gives you the choice of how you decide to format things or exchange them, whether you want to round something off, etc.

Usage

Please see the project documentation.

Contributing

We welcome contributions to this project.

  1. Fork it.
  2. Create your feature branch (git checkout -b my-new-feature).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin my-new-feature).
  5. Create new Pull Request.

Developer Certificate of Origin

This project uses the Developer Certificate of Origin. All contributors to this project must agree to this document to have their contributions accepted.

Contributor Covenant

This project is governed by Contributor Covenant. All contributors and participants agree to abide by its terms.

latinum's People

Contributors

adam12 avatar ioquatix avatar madleech avatar timcraft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

latinum's Issues

Brazilian currency support

Hi @ioquatix. We're considering migrating from money-rails to latinum gem and we faced some issues during this transition. Here's what I have on my latinum.rb initializer. By the way, it seems that latinum/currencies/global isn't required by default.

require "latinum/currencies/global"

Latinum::Currencies::Global[:BRL] = {
  precision: 2,
  symbol: "R$",
  name: "BRL",
  description: "Brazilian Real",
  formatter: Latinum::Formatters::DecimalCurrencyFormatter,
  delimeter: ".",
  separator: ","
}

First problem is about parsing values. For example:

bank = Latinum::Bank.new(Latinum::Currencies::Global)
bank.parse("9,90", default_name: :BRL) # returns #<Latinum::Resource "990.0 BRL">

In this example, shouldn't it return #<Latinum::Resource "9.9 BRL">? Second problem: In our database we're storing values as integer and only in cents. Is there any configuration available to handle values only in cents by default?

Thanks.

What is the best way to assign a resource to an attribute?

I am exploring whether to use this gem for a multi currency accounting system. It is old and presumably no longer developed but I liked the concept of being able to store the value and currency together and ensure that I would not be summing unlike quantities.

I tried using the ActiveRecord integration and setting the amount column to type string on my line_items model.

Then I tried creating with something like:

entry.line_items.create!(amount: '$40145.00')

In the LineItem class, I have:

serialize :amount, Latinum::Resource

This did not work. Amount is nil. I tried overriding amount=(value) and parsing:

  def amount=(value)
    if value.kind_of?(String)
      bank = Latinum::Bank.new(Latinum::Currencies::Global)
      attributes['amount'] = bank.parse(value)
      byebug
    end
  end

At this point the attribute is still nil. I tried setting it a variety of ways. None seemed to work.

What is the recommended way to do this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.