Class: SwissKnife::RSpec::Matchers::Allow

Inherits:
Object
  • Object
show all
Defined in:
lib/swiss_knife/rspec/allow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Allow

Returns a new instance of Allow.



11
12
13
14
# File 'lib/swiss_knife/rspec/allow.rb', line 11

def initialize(values)
  @failed = []
  @values = values
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



9
10
11
# File 'lib/swiss_knife/rspec/allow.rb', line 9

def attribute
  @attribute
end

#failedObject

Returns the value of attribute failed.



9
10
11
# File 'lib/swiss_knife/rspec/allow.rb', line 9

def failed
  @failed
end

#recordObject

Returns the value of attribute record.



9
10
11
# File 'lib/swiss_knife/rspec/allow.rb', line 9

def record
  @record
end

#valuesObject

Returns the value of attribute values.



9
10
11
# File 'lib/swiss_knife/rspec/allow.rb', line 9

def values
  @values
end

Instance Method Details

#descriptionObject



26
27
28
# File 'lib/swiss_knife/rspec/allow.rb', line 26

def description
  "allow #{values.inspect} as #{attribute}"
end

#does_not_match?(record) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/swiss_knife/rspec/allow.rb', line 21

def does_not_match?(record)
  @record = record
  matches_against?(true)
end

#failure_messageObject



30
31
32
# File 'lib/swiss_knife/rspec/allow.rb', line 30

def failure_message
  "expected #{record.inspect} to allow each of #{values.inspect} as #{attribute.inspect} value (didn't accept #{failed.inspect})"
end

#for(attribute) ⇒ Object



38
39
40
41
# File 'lib/swiss_knife/rspec/allow.rb', line 38

def for(attribute)
  @attribute = attribute
  self
end

#matches?(record) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/swiss_knife/rspec/allow.rb', line 16

def matches?(record)
  @record = record
  matches_against?(false)
end

#matches_against?(compare) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/swiss_knife/rspec/allow.rb', line 43

def matches_against?(compare)
  raise "The allow matcher requires an attribute; use subject.should allow(*values).as(attribute)" unless attribute

  values.each do |value|
    record.__send__("#{attribute}=", value)
    record.valid?
    failed << value if record.errors[attribute].empty? == compare
  end

  failed.empty?
end

#negative_failure_messageObject



34
35
36
# File 'lib/swiss_knife/rspec/allow.rb', line 34

def negative_failure_message
  "expected #{record.inspect} to reject each of #{values.inspect} as #{attribute.inspect} value (accepted #{failed.inspect})"
end