Class: DPay::Money
- Inherits:
-
Object
- Object
- DPay::Money
- Defined in:
- lib/dpay/money.rb,
sig/dpay/money.rbs
Constant Summary collapse
- DECIMAL =
/\A(-?)(\d+)(?:\.(\d{1,2}))?\z/
Instance Attribute Summary collapse
-
#currency ⇒ String
readonly
Returns the value of attribute currency.
-
#minor ⇒ Integer
readonly
Returns the value of attribute minor.
Class Method Summary collapse
- .from_api_number(value, currency) ⇒ Money
- .from_decimal(decimal, currency) ⇒ Money
- .of(minor, currency) ⇒ Money
- .pln(minor) ⇒ Money
- .try_from_api_number(value, currency) ⇒ Money?
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
- #hash ⇒ Integer
-
#initialize(minor, currency) ⇒ Money
constructor
A new instance of Money.
- #negative? ⇒ Boolean
- #to_decimal ⇒ String
- #to_s ⇒ String
Constructor Details
#initialize(minor, currency) ⇒ Money
Returns a new instance of Money.
56 57 58 59 60 |
# File 'lib/dpay/money.rb', line 56 def initialize(minor, currency) @minor = minor @currency = currency freeze end |
Instance Attribute Details
#currency ⇒ String (readonly)
Returns the value of attribute currency.
7 8 9 |
# File 'lib/dpay/money.rb', line 7 def currency @currency end |
#minor ⇒ Integer (readonly)
Returns the value of attribute minor.
7 8 9 |
# File 'lib/dpay/money.rb', line 7 def minor @minor end |
Class Method Details
.from_api_number(value, currency) ⇒ Money
26 27 28 29 30 31 32 33 |
# File 'lib/dpay/money.rb', line 26 def self.from_api_number(value, currency) case value when Integer then of(value * 100, currency) when Float then of(Internal::PHP.round(value * 100), currency) when String then from_decimal(value, currency) else raise InvalidArgumentError, "Money value must be Integer, Float or String" end end |
.from_decimal(decimal, currency) ⇒ Money
18 19 20 21 22 23 24 |
# File 'lib/dpay/money.rb', line 18 def self.from_decimal(decimal, currency) Currency.assert_valid(currency) parsed = parse_decimal(decimal) raise InvalidArgumentError, %(Invalid money amount "#{decimal}") if parsed.nil? new(parsed, currency) end |
.of(minor, currency) ⇒ Money
13 14 15 16 |
# File 'lib/dpay/money.rb', line 13 def self.of(minor, currency) Currency.assert_valid(currency) new(minor, currency) end |
.pln(minor) ⇒ Money
9 10 11 |
# File 'lib/dpay/money.rb', line 9 def self.pln(minor) new(minor, Currency::PLN) end |
.try_from_api_number(value, currency) ⇒ Money?
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dpay/money.rb', line 35 def self.try_from_api_number(value, currency) return nil unless Currency.valid?(currency) case value when Integer then new(value * 100, currency) when Float then new(Internal::PHP.round(value * 100), currency) when String parsed = parse_decimal(value) parsed.nil? ? nil : new(parsed, currency) end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
72 73 74 |
# File 'lib/dpay/money.rb', line 72 def ==(other) other.is_a?(Money) && other.minor == @minor && other.currency == @currency end |
#hash ⇒ Integer
77 78 79 |
# File 'lib/dpay/money.rb', line 77 def hash [@minor, @currency].hash end |
#negative? ⇒ Boolean
68 69 70 |
# File 'lib/dpay/money.rb', line 68 def negative? @minor.negative? end |
#to_decimal ⇒ String
62 63 64 65 66 |
# File 'lib/dpay/money.rb', line 62 def to_decimal absolute = @minor.abs sign = @minor.negative? ? "-" : "" format("%<sign>s%<dollars>d.%<cents>02d", sign: sign, dollars: absolute / 100, cents: absolute % 100) end |
#to_s ⇒ String
81 82 83 |
# File 'lib/dpay/money.rb', line 81 def to_s "#{to_decimal} #{@currency}" end |