Code Monkey home page Code Monkey logo

Comments (5)

stalcottsmith avatar stalcottsmith commented on May 27, 2024

I found that I was able to do this with write_attribute(). But I would still like to know the canonical or expected usage.

from latinum.

ioquatix avatar ioquatix commented on May 27, 2024

I'm still using this gem and I suppose you could call it maintained, it's just been stable enough that no changes were required.

The following code was implemented for support of serialize:

# Load a string representation of a resource.
# @parameter string [String | Nil] e.g. "5 NZD" or nil.
# @returns [Resource | Nil] The Resource that represents the parsed string.
def self.load(string)
if string
# Remove any whitespaces
string = string.strip
parse(string) unless string.empty?
end
end
# Dump a string representation of a resource.
# @parameter resource [Resource] The resource to dump.
# @returns [String | Nil] A string that represents the {Resource}.
def self.dump(resource)
resource.to_s if resource
end

A bank object is only needed for formatting and converting resources, it should not be needed for basic interactions with "I have X units of Y."

For actual values, you need to write:

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

If you want rich input, you do need to use the bank configured with the currencies you want. Otherwise, how can you tell $5 is USD, NZD, AUD, etc.

In order to use the bank object, you should configure it in the controller to map the user input before creating the model with attribute values.

e.g.

params['amount'] = bank.parse(params['amount'])

I can write updated documentation with working examples if that helps.

from latinum.

ioquatix avatar ioquatix commented on May 27, 2024

I wonder if we should make Resource.load also check for Resource instances and return it directly to support the above use case if it isn't already handled by AR.

from latinum.

ioquatix avatar ioquatix commented on May 27, 2024

Okay, so with a few minor changes, we can make this work a bit more nicely:

Loading development environment (Rails 7.1.1)
irb(main):001> t = T.new
=> #<T:0x00007f2fb8b88ff0 id: nil, data: nil, amount: nil, created_at: nil, updated_at: nil>
irb(main):002> t.amount = "$5 USD"
=> "$5 USD"
irb(main):003> t.amount
=> #<Latinum::Resource "5.0 USD">

The code is:

require 'latinum/currencies/global'

class T < ApplicationRecord
  BANK = Latinum::Bank.new.tap do |bank|
    bank.import(Latinum::Currencies::Global)
  end
  
  serialize :amount, coder: BANK
end

This uses the bank as a coder, which accepts a wider range of inputs. However, if there is ambiguity, it is resolved according to the bank's internal priority, e.g. $ is used by several currencies.

from latinum.

ioquatix avatar ioquatix commented on May 27, 2024

I've released v1.8.0 with the required changes and added documentation: https://ioquatix.github.io/latinum/guides/activerecord-integration/index.html

from latinum.

Related Issues (3)

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.