Class: MoneyRails::TestHelpers::MonetizeMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/money-rails/test_helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ MonetizeMatcher

Returns a new instance of MonetizeMatcher.



10
11
12
# File 'lib/money-rails/test_helpers.rb', line 10

def initialize(attribute)
  @attribute = attribute
end

Instance Method Details

#allow_nilObject



24
25
26
27
# File 'lib/money-rails/test_helpers.rb', line 24

def allow_nil
  @allow_nil = true
  self
end

#as(virt_attr) ⇒ Object



19
20
21
22
# File 'lib/money-rails/test_helpers.rb', line 19

def as(virt_attr)
  @as = virt_attr
  self
end

#descriptionObject



53
54
55
56
57
58
# File 'lib/money-rails/test_helpers.rb', line 53

def description
  desc = "monetize #{@attribute}"
  desc << " as #{@as}" if @as
  desc << " with currency #{@currency_iso}" if @currency_iso
  desc
end

#failure_messageObject Also known as: failure_message_for_should

RSpec 3.x



60
61
62
63
64
65
# File 'lib/money-rails/test_helpers.rb', line 60

def failure_message # RSpec 3.x
  msg = "expected that #{@attribute} of #{@actual} would be monetized"
  msg << " as #{@as}" if @as
  msg << " with currency #{@currency_iso}" if @currency_iso
  msg
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not, negative_failure_message

RSpec 3.x



68
69
70
71
72
73
# File 'lib/money-rails/test_helpers.rb', line 68

def failure_message_when_negated # RSpec 3.x
  msg = "expected that #{@attribute} of #{@actual} would not be monetized"
  msg << " as #{@as}" if @as
  msg << " with currency #{@currency_iso}" if @currency_iso
  msg
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/money-rails/test_helpers.rb', line 29

def matches?(actual)
  @actual = actual

  money_attr = @as.presence || @attribute.to_s.sub(/_cents$/, "")

  matched = true

  if actual.respond_to?(money_attr)
    if @allow_nil
      matched &&= actual.send(money_attr).nil?
      actual.send("#{money_attr}=", 0)
    end
    matched &&= actual.send(money_attr).instance_of?(Money)

    if @currency_iso
      matched &&= actual.send(money_attr.to_sym).currency.id == @currency_iso
    end
  else
    matched = false
  end

  matched
end

#with_currency(currency) ⇒ Object



14
15
16
17
# File 'lib/money-rails/test_helpers.rb', line 14

def with_currency(currency)
  @currency_iso = currency
  self
end