Class: Amount::Registry
- Inherits:
-
Object
- Object
- Amount::Registry
- Defined in:
- lib/amount/registry.rb,
lib/amount/registry/generated_constructors.rb
Overview
Defined Under Namespace
Classes: AlreadyRegistered, Entry, GeneratedConstructors, InvalidDisplayUnit, NoDefaultRate, RegistryLocked, UnknownType
Instance Method Summary collapse
- #activate_generated_methods! ⇒ void
- #clear! ⇒ void
- #default_rate(from, to) ⇒ BigDecimal
- #default_rate?(from, to) ⇒ Boolean
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #lock! ⇒ void
- #locked? ⇒ Boolean
- #lookup(symbol) ⇒ Entry
-
#register(symbol, decimals:, display_symbol: symbol.to_s, display_position: :suffix, ui_decimals: decimals, display_units: nil, default_display: nil, trim_zeros: false, class: nil) ⇒ void
Registers a new fungible type.
- #register_default_rate(from, to, rate) ⇒ void
- #registered?(symbol) ⇒ Boolean
- #remove_generated_methods! ⇒ void
- #symbols ⇒ Array<Symbol>
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
42 43 44 45 46 47 48 49 |
# File 'lib/amount/registry.rb', line 42 def initialize @entries = {} @default_rates = {} @locked = false @lock = Mutex.new @generated_constructors = GeneratedConstructors.new end |
Instance Method Details
#activate_generated_methods! ⇒ void
This method returns an undefined value.
214 215 216 217 218 |
# File 'lib/amount/registry.rb', line 214 def activate_generated_methods! @lock.synchronize do @generated_constructors.activate(@entries) end end |
#clear! ⇒ void
This method returns an undefined value.
142 143 144 145 146 147 148 149 |
# File 'lib/amount/registry.rb', line 142 def clear! @lock.synchronize do ensure_unlocked! @generated_constructors.remove_all @entries.clear @default_rates.clear end end |
#default_rate(from, to) ⇒ BigDecimal
178 179 180 181 182 183 184 |
# File 'lib/amount/registry.rb', line 178 def default_rate(from, to) @lock.synchronize do @default_rates.fetch([from.to_sym, to.to_sym]) do raise NoDefaultRate, "no default rate for #{from} -> #{to}; pass rate: explicitly" end end end |
#default_rate?(from, to) ⇒ Boolean
192 193 194 |
# File 'lib/amount/registry.rb', line 192 def default_rate?(from, to) @lock.synchronize { @default_rates.key?([from.to_sym, to.to_sym]) } end |
#lock! ⇒ void
This method returns an undefined value.
199 200 201 202 203 |
# File 'lib/amount/registry.rb', line 199 def lock! @lock.synchronize do @locked = true end end |
#locked? ⇒ Boolean
209 210 211 |
# File 'lib/amount/registry.rb', line 209 def locked? @lock.synchronize { @locked } end |
#lookup(symbol) ⇒ Entry
122 123 124 125 126 127 128 |
# File 'lib/amount/registry.rb', line 122 def lookup(symbol) @lock.synchronize do @entries.fetch(symbol.to_sym) do raise UnknownType, "#{symbol} is not registered" end end end |
#register(symbol, decimals:, display_symbol: symbol.to_s, display_position: :suffix, ui_decimals: decimals, display_units: nil, default_display: nil, trim_zeros: false, class: nil) ⇒ void
This method returns an undefined value.
Registers a new fungible type.
When the symbol is a valid Ruby method name after downcasing, an ergonomic constructor is also generated on ‘Amount` with an `of_` prefix, such as `Amount.of_usdc(“1.50”)`. The prefix avoids collisions with existing methods like `Object#try` (added by ActiveSupport).
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/amount/registry.rb', line 77 def register(symbol, decimals:, display_symbol: symbol.to_s, display_position: :suffix, ui_decimals: decimals, display_units: nil, default_display: nil, trim_zeros: false, class: nil) raise ArgumentError, "symbol must not be blank" if symbol.nil? || symbol.to_s.empty? symbol = symbol.to_sym @lock.synchronize do ensure_unlocked! raise AlreadyRegistered, "#{symbol} already registered" if @entries.key?(symbol) validate_display_units!(display_units, default_display) if display_units entry = Entry.new( symbol:, decimals:, display_symbol:, display_position:, ui_decimals:, display_units:, default_display:, amount_class: binding.local_variable_get(:class) || Amount, trim_zeros: ) @entries[symbol] = entry @generated_constructors.define_for(entry) end end |
#register_default_rate(from, to, rate) ⇒ void
This method returns an undefined value.
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/amount/registry.rb', line 158 def register_default_rate(from, to, rate) from = from.to_sym to = to.to_sym lookup(from) lookup(to) @lock.synchronize do ensure_unlocked! @default_rates[[from, to]] = Amount.coerce_decimal(rate) end end |
#registered?(symbol) ⇒ Boolean
112 113 114 |
# File 'lib/amount/registry.rb', line 112 def registered?(symbol) @lock.synchronize { @entries.key?(symbol.to_sym) } end |
#remove_generated_methods! ⇒ void
This method returns an undefined value.
221 222 223 224 225 |
# File 'lib/amount/registry.rb', line 221 def remove_generated_methods! @lock.synchronize do @generated_constructors.remove_all end end |
#symbols ⇒ Array<Symbol>
134 135 136 |
# File 'lib/amount/registry.rb', line 134 def symbols @lock.synchronize { @entries.keys } end |