Class: Recurly::Money

Inherits:
Object
  • Object
show all
Defined in:
lib/recurly/money.rb

Overview

Represents a collection of currencies (in cents).

Instance Method Summary collapse

Constructor Details

#initialize(currencies = {}) ⇒ Object

Returns A money object representing multiple currencies (in cents).

Examples:

# 12 United States dollars.
Recurly::Money.new :USD => 12_00

# $9.99 (or €6.99).
Recurly::Money.new :USD => 9_99, :EUR => 6_99

# Using a default currency.
Recurly.default_currency = 'USD'
Recurly::Money.new(49_00) # => #<Recurly::Money USD: 49_00>

Parameters:

  • currencies (Hash) (defaults to: {})

    A hash of currency codes and amounts.



16
17
18
19
20
21
22
23
24
# File 'lib/recurly/money.rb', line 16

def initialize currencies = {}
  @currencies = Helper.hash_with_indifferent_read_access

  if currencies.respond_to? :each_pair
    currencies.each_pair { |key, value| @currencies[key.to_s] = value }
  else
    @currencies[Recurly.default_currency] = currencies
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/recurly/money.rb', line 91

def method_missing name, *args, &block
  if currencies.respond_to? name
    return currencies.send name, *args, &block
  elsif c = currencies[Recurly.default_currency] and c.respond_to? name
    if currencies.keys.length > 1
      raise TypeError, "can't convert multicurrency into Integer"
    else
      return c.send name, *args, &block
    end
  end

  super
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Implemented so that solitary currencies can be compared and sorted.

Examples:

[Recurly::Money.new(2_00), Recurly::Money.new(1_00)].sort
# => [#<Recurly::Money USD: 1_00>, #<Recurly::Money USD: 2_00>]

Parameters:

Returns:

  • (-1, 0, 1)

See Also:

  • Hash#<=>


52
53
54
55
56
57
58
59
60
# File 'lib/recurly/money.rb', line 52

def <=> other
  if currencies.keys.length == 1 && other.currencies.length == 1
    if currencies.keys == other.currencies.keys
      return currencies.values.first <=> other.currencies.values.first
    end
  end

  currencies <=> other.currencies
end

#eql?(other) ⇒ true, false

instance.

Parameters:

Returns:

  • (true, false)

    Whether or not the currency is equal to another



34
35
36
# File 'lib/recurly/money.rb', line 34

def eql? other
  other.respond_to?(:currencies) && currencies.eql?(other.currencies)
end

#hashInteger

Returns Unique identifier.

Returns:

  • (Integer)

    Unique identifier.

See Also:

  • Hash#hash


40
41
42
# File 'lib/recurly/money.rb', line 40

def hash
  currencies.hash
end

#inspectString Also known as: to_s

Returns:

  • (String)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/recurly/money.rb', line 69

def inspect
  string = "#<#{self.class}"
  if currencies.any?
    string << " %s" % currencies.keys.sort.map { |code|
      value = currencies[code].to_s
      value.gsub!(/^(\d)$/, '0_0\1')
      value.gsub!(/^(\d{2})$/, '0_\1')
      value.gsub!(/(\d)(\d{2})$/, '\1_\2')
      value.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, '\1_')
      "#{code}: #{value}"
    }.join(', ')
  end
  string << '>'
end

#respond_to?(method_name, include_private = false) ⇒ true, false

Returns:

  • (true, false)

See Also:

  • Object#respond_to?


64
65
66
# File 'lib/recurly/money.rb', line 64

def respond_to? method_name, include_private = false
  super || currencies.respond_to?(method_name, include_private)
end

#to_hashHash

Returns A hash of currency codes to amounts.

Returns:

  • (Hash)

    A hash of currency codes to amounts.



27
28
29
# File 'lib/recurly/money.rb', line 27

def to_hash
  currencies.dup
end