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.



8
9
10
# File 'lib/money-rails/test_helpers.rb', line 8

def initialize(attribute)
  @attribute = attribute
end

Instance Method Details

#allow_nilObject



27
28
29
30
# File 'lib/money-rails/test_helpers.rb', line 27

def allow_nil
  @allow_nil = true
  self
end

#as(virt_attr) ⇒ Object



22
23
24
25
# File 'lib/money-rails/test_helpers.rb', line 22

def as(virt_attr)
  @as = virt_attr
  self
end

#descriptionObject



50
51
52
53
54
55
# File 'lib/money-rails/test_helpers.rb', line 50

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



57
58
59
60
61
62
# File 'lib/money-rails/test_helpers.rb', line 57

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



65
66
67
68
69
70
# File 'lib/money-rails/test_helpers.rb', line 65

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)


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

def matches?(actual)
  if actual.is_a?(Class)
    @actual = actual.new
  else
    @actual = actual.class.new
  end

  @money_attribute = @as.presence || @attribute.to_s.sub(/_cents$/, "")
  @money_attribute_setter = "#{@money_attribute}="

  object_responds_to_attributes? &&
    test_allow_nil &&
    is_monetized? &&
    test_currency_iso &&
    test_currency_attribute
end

#with_currency(currency) ⇒ Object



12
13
14
15
# File 'lib/money-rails/test_helpers.rb', line 12

def with_currency(currency)
  @currency_iso = currency
  self
end

#with_model_currency(attribute) ⇒ Object



17
18
19
20
# File 'lib/money-rails/test_helpers.rb', line 17

def with_model_currency(attribute)
  @currency_attribute = attribute
  self
end