Class: DPay::Money

Inherits:
Object
  • Object
show all
Defined in:
lib/dpay/money.rb,
sig/dpay/money.rbs

Constant Summary collapse

DECIMAL =

Returns:

  • (Regexp)
/\A(-?)(\d+)(?:\.(\d{1,2}))?\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minor, currency) ⇒ Money

Returns a new instance of Money.

Parameters:

  • minor (Integer)
  • currency (String)


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

#currencyString (readonly)

Returns the value of attribute currency.

Returns:

  • (String)


7
8
9
# File 'lib/dpay/money.rb', line 7

def currency
  @currency
end

#minorInteger (readonly)

Returns the value of attribute minor.

Returns:

  • (Integer)


7
8
9
# File 'lib/dpay/money.rb', line 7

def minor
  @minor
end

Class Method Details

.from_api_number(value, currency) ⇒ Money

Parameters:

  • value (Object)
  • currency (String)

Returns:



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

Parameters:

  • decimal (String)
  • currency (String)

Returns:

Raises:



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

Parameters:

  • minor (Integer)
  • currency (String)

Returns:



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

Parameters:

  • minor (Integer)

Returns:



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?

Parameters:

  • value (Object)
  • currency (String)

Returns:



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?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


72
73
74
# File 'lib/dpay/money.rb', line 72

def ==(other)
  other.is_a?(Money) && other.minor == @minor && other.currency == @currency
end

#hashInteger

Returns:

  • (Integer)


77
78
79
# File 'lib/dpay/money.rb', line 77

def hash
  [@minor, @currency].hash
end

#negative?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/dpay/money.rb', line 68

def negative?
  @minor.negative?
end

#to_decimalString

Returns:

  • (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_sString

Returns:

  • (String)


81
82
83
# File 'lib/dpay/money.rb', line 81

def to_s
  "#{to_decimal} #{@currency}"
end