Created
August 28, 2020 12:02
-
-
Save mikdiet/a9681ac1949dd54bed419f21c9b20f87 to your computer and use it in GitHub Desktop.
Using GlobalID for custom classes (to pass into Rails ActiveJob)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ``` | |
| class Currency | |
| include GlobalID::Identification | |
| attr_accessor :a, :b | |
| def initialize(a, b) | |
| @a = BigDecimal(a.to_f, 2) | |
| @b = b.to_s | |
| end | |
| def id | |
| "#{a}--#{b}" | |
| end | |
| def self.find(id) | |
| new(*id.split('--', 2)) | |
| end | |
| end | |
| money = Currency.new(10, 'USD') | |
| # => #<Currency:0x00007fe220581bf0 @a=10, @b="USD"> | |
| gid = GlobalID.create(money) | |
| # => #<GlobalID:0x00007fe216c2e778 @uri=#<URI::GID gid://my_app/Currency/10.0--USD>> | |
| money_in_active_job = GlobalID.find(gid) | |
| # => #<Currency:0x00007fe21f65bb30 @a=10, @b="USD"> | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment