mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-18 20:59:39 +02:00
Rubocop updates (#1118)
* Minimal code style enforcement * Formatting and lint code updates (no change in functionality)
This commit is contained in:
parent
359bceb58e
commit
eef4c2643b
41 changed files with 529 additions and 519 deletions
|
@ -1,62 +1,62 @@
|
|||
module Money::Arithmetic
|
||||
CoercedNumeric = Struct.new(:value)
|
||||
CoercedNumeric = Struct.new(:value)
|
||||
|
||||
def +(other)
|
||||
if other.is_a?(Money)
|
||||
self.class.new(amount + other.amount, currency)
|
||||
else
|
||||
value = other.is_a?(CoercedNumeric) ? other.value : other
|
||||
self.class.new(amount + value, currency)
|
||||
end
|
||||
def +(other)
|
||||
if other.is_a?(Money)
|
||||
self.class.new(amount + other.amount, currency)
|
||||
else
|
||||
value = other.is_a?(CoercedNumeric) ? other.value : other
|
||||
self.class.new(amount + value, currency)
|
||||
end
|
||||
end
|
||||
|
||||
def -(other)
|
||||
if other.is_a?(Money)
|
||||
self.class.new(amount - other.amount, currency)
|
||||
else
|
||||
value = other.is_a?(CoercedNumeric) ? other.value : other
|
||||
self.class.new(amount - value, currency)
|
||||
end
|
||||
def -(other)
|
||||
if other.is_a?(Money)
|
||||
self.class.new(amount - other.amount, currency)
|
||||
else
|
||||
value = other.is_a?(CoercedNumeric) ? other.value : other
|
||||
self.class.new(amount - value, currency)
|
||||
end
|
||||
end
|
||||
|
||||
def -@
|
||||
self.class.new(-amount, currency)
|
||||
end
|
||||
def -@
|
||||
self.class.new(-amount, currency)
|
||||
end
|
||||
|
||||
def *(other)
|
||||
raise TypeError, "Can't multiply Money by Money, use Numeric instead" if other.is_a?(self.class)
|
||||
value = other.is_a?(CoercedNumeric) ? other.value : other
|
||||
self.class.new(amount * value, currency)
|
||||
end
|
||||
def *(other)
|
||||
raise TypeError, "Can't multiply Money by Money, use Numeric instead" if other.is_a?(self.class)
|
||||
value = other.is_a?(CoercedNumeric) ? other.value : other
|
||||
self.class.new(amount * value, currency)
|
||||
end
|
||||
|
||||
def /(other)
|
||||
if other.is_a?(self.class)
|
||||
amount / other.amount
|
||||
else
|
||||
raise TypeError, "can't divide Numeric by Money" if other.is_a?(CoercedNumeric)
|
||||
self.class.new(amount / other, currency)
|
||||
end
|
||||
def /(other)
|
||||
if other.is_a?(self.class)
|
||||
amount / other.amount
|
||||
else
|
||||
raise TypeError, "can't divide Numeric by Money" if other.is_a?(CoercedNumeric)
|
||||
self.class.new(amount / other, currency)
|
||||
end
|
||||
end
|
||||
|
||||
def abs
|
||||
self.class.new(amount.abs, currency)
|
||||
end
|
||||
def abs
|
||||
self.class.new(amount.abs, currency)
|
||||
end
|
||||
|
||||
def zero?
|
||||
amount.zero?
|
||||
end
|
||||
def zero?
|
||||
amount.zero?
|
||||
end
|
||||
|
||||
def negative?
|
||||
amount.negative?
|
||||
end
|
||||
def negative?
|
||||
amount.negative?
|
||||
end
|
||||
|
||||
def positive?
|
||||
amount.positive?
|
||||
end
|
||||
def positive?
|
||||
amount.positive?
|
||||
end
|
||||
|
||||
# Override Ruby's coerce method so the order of operands doesn't matter
|
||||
# Wrap in Coerced so we can distinguish between Money and other types
|
||||
def coerce(other)
|
||||
[ self, CoercedNumeric.new(other) ]
|
||||
end
|
||||
# Override Ruby's coerce method so the order of operands doesn't matter
|
||||
# Wrap in Coerced so we can distinguish between Money and other types
|
||||
def coerce(other)
|
||||
[ self, CoercedNumeric.new(other) ]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,69 +1,69 @@
|
|||
class Money::Currency
|
||||
include Comparable
|
||||
include Comparable
|
||||
|
||||
class UnknownCurrencyError < ArgumentError; end
|
||||
class UnknownCurrencyError < ArgumentError; end
|
||||
|
||||
CURRENCIES_FILE_PATH = Rails.root.join("config", "currencies.yml")
|
||||
CURRENCIES_FILE_PATH = Rails.root.join("config", "currencies.yml")
|
||||
|
||||
# Cached instances by iso code
|
||||
@@instances = {}
|
||||
# Cached instances by iso code
|
||||
@@instances = {}
|
||||
|
||||
class << self
|
||||
def new(object)
|
||||
iso_code = case object
|
||||
when String, Symbol
|
||||
object.to_s.downcase
|
||||
when Money::Currency
|
||||
object.iso_code.downcase
|
||||
else
|
||||
raise ArgumentError, "Invalid argument type"
|
||||
end
|
||||
class << self
|
||||
def new(object)
|
||||
iso_code = case object
|
||||
when String, Symbol
|
||||
object.to_s.downcase
|
||||
when Money::Currency
|
||||
object.iso_code.downcase
|
||||
else
|
||||
raise ArgumentError, "Invalid argument type"
|
||||
end
|
||||
|
||||
@@instances[iso_code] ||= super(iso_code)
|
||||
end
|
||||
|
||||
def all
|
||||
@all ||= YAML.load_file(CURRENCIES_FILE_PATH)
|
||||
end
|
||||
|
||||
def all_instances
|
||||
all.values.map { |currency_data| new(currency_data["iso_code"]) }
|
||||
end
|
||||
|
||||
def popular
|
||||
all.values.sort_by { |currency| currency["priority"] }.first(12).map { |currency_data| new(currency_data["iso_code"]) }
|
||||
end
|
||||
@@instances[iso_code] ||= super(iso_code)
|
||||
end
|
||||
|
||||
attr_reader :name, :priority, :iso_code, :iso_numeric, :html_code,
|
||||
:symbol, :minor_unit, :minor_unit_conversion, :smallest_denomination,
|
||||
:separator, :delimiter, :default_format, :default_precision
|
||||
|
||||
def initialize(iso_code)
|
||||
currency_data = self.class.all[iso_code]
|
||||
raise UnknownCurrencyError if currency_data.nil?
|
||||
|
||||
@name = currency_data["name"]
|
||||
@priority = currency_data["priority"]
|
||||
@iso_code = currency_data["iso_code"]
|
||||
@iso_numeric = currency_data["iso_numeric"]
|
||||
@html_code = currency_data["html_code"]
|
||||
@symbol = currency_data["symbol"]
|
||||
@minor_unit = currency_data["minor_unit"]
|
||||
@minor_unit_conversion = currency_data["minor_unit_conversion"]
|
||||
@smallest_denomination = currency_data["smallest_denomination"]
|
||||
@separator = currency_data["separator"]
|
||||
@delimiter = currency_data["delimiter"]
|
||||
@default_format = currency_data["default_format"]
|
||||
@default_precision = currency_data["default_precision"]
|
||||
def all
|
||||
@all ||= YAML.load_file(CURRENCIES_FILE_PATH)
|
||||
end
|
||||
|
||||
def step
|
||||
(1.0/10**default_precision)
|
||||
def all_instances
|
||||
all.values.map { |currency_data| new(currency_data["iso_code"]) }
|
||||
end
|
||||
|
||||
def <=>(other)
|
||||
return nil unless other.is_a?(Money::Currency)
|
||||
@iso_code <=> other.iso_code
|
||||
def popular
|
||||
all.values.sort_by { |currency| currency["priority"] }.first(12).map { |currency_data| new(currency_data["iso_code"]) }
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :name, :priority, :iso_code, :iso_numeric, :html_code,
|
||||
:symbol, :minor_unit, :minor_unit_conversion, :smallest_denomination,
|
||||
:separator, :delimiter, :default_format, :default_precision
|
||||
|
||||
def initialize(iso_code)
|
||||
currency_data = self.class.all[iso_code]
|
||||
raise UnknownCurrencyError if currency_data.nil?
|
||||
|
||||
@name = currency_data["name"]
|
||||
@priority = currency_data["priority"]
|
||||
@iso_code = currency_data["iso_code"]
|
||||
@iso_numeric = currency_data["iso_numeric"]
|
||||
@html_code = currency_data["html_code"]
|
||||
@symbol = currency_data["symbol"]
|
||||
@minor_unit = currency_data["minor_unit"]
|
||||
@minor_unit_conversion = currency_data["minor_unit_conversion"]
|
||||
@smallest_denomination = currency_data["smallest_denomination"]
|
||||
@separator = currency_data["separator"]
|
||||
@delimiter = currency_data["delimiter"]
|
||||
@default_format = currency_data["default_format"]
|
||||
@default_precision = currency_data["default_precision"]
|
||||
end
|
||||
|
||||
def step
|
||||
(1.0/10**default_precision)
|
||||
end
|
||||
|
||||
def <=>(other)
|
||||
return nil unless other.is_a?(Money::Currency)
|
||||
@iso_code <=> other.iso_code
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue