Module: MoneyWithDate::InstanceMethods

Defined in:
lib/money_with_date/instance_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



5
6
7
# File 'lib/money_with_date/instance_methods.rb', line 5

def date
  @date
end

Instance Method Details

#<=>(other) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/money_with_date/instance_methods.rb', line 59

def <=>(other) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  return super unless ::Money.date_determines_equality

  unless other.is_a?(::Money)
    return unless other.respond_to?(:zero?) && other.zero?

    return other.is_a?(::Money::Arithmetic::CoercedNumeric) ? 0 <=> fractional : fractional <=> 0
  end

  return fractional <=> other.fractional if zero? || other.zero?

  other = other.exchange_to(currency)
  [fractional, date] <=> [other.fractional, other.date]
rescue ::Money::Bank::UnknownRate
  nil
end

#dup_with(options = {}) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/money_with_date/instance_methods.rb', line 39

def dup_with(options = {})
  self.class.new(
    options[:fractional] || fractional,
    options[:currency] || currency,
    bank: options[:bank] || bank,
    date: options[:date] || date
  )
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# File 'lib/money_with_date/instance_methods.rb', line 48

def eql?(other)
  return super unless ::Money.date_determines_equality

  if other.is_a?(::Money)
    (fractional == other.fractional && currency == other.currency && date == other.date) ||
      (fractional.zero? && other.fractional.zero?)
  else
    false
  end
end

#hashObject



19
20
21
22
23
# File 'lib/money_with_date/instance_methods.rb', line 19

def hash
  return super unless ::Money.date_determines_equality

  [fractional.hash, currency.hash, date.hash].hash # rubocop:disable Security/CompoundHash
end

#initialize(obj, currency = ::Money.default_currency, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/money_with_date/instance_methods.rb', line 7

def initialize(obj, currency = ::Money.default_currency, options = {})
  if options.is_a?(::Hash)
    @date = self.class.parse_date(options[:date])
  else
    @date = ::Money.default_date
  end

  raise ArgumentError, "#{@date.inspect} is not an instance of Date" unless @date.is_a?(::Date)

  super
end

#inspectObject



25
26
27
# File 'lib/money_with_date/instance_methods.rb', line 25

def inspect
  "#<#{self.class.name} fractional:#{fractional} currency:#{currency} date:#{date}>"
end

#with_date(new_date) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/money_with_date/instance_methods.rb', line 29

def with_date(new_date)
  new_date = self.class.parse_date(new_date)

  if date == new_date
    self
  else
    dup_with(date: new_date)
  end
end