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



29
30
31
32
# File 'lib/money-rails/test_helpers.rb', line 29

def allow_nil
  @allow_nil = true
  self
end

#as(virt_attr) ⇒ Object



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

def as(virt_attr)
  @as = virt_attr
  self
end

#descriptionObject



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

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



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

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



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

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)


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

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



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

def with_currency(currency)
  @currency_iso = currency
  self
end

#with_model_currency(attribute) ⇒ Object



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

def with_model_currency(attribute)
  @currency_attribute = attribute
  self
end