Class: ValidAttribute::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/valid_attribute/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr) ⇒ Matcher

Returns a new instance of Matcher.



6
7
8
9
# File 'lib/valid_attribute/matcher.rb', line 6

def initialize(attr)
  self.attr = attr
  @clone = nil
end

Instance Attribute Details

#attrObject

Returns the value of attribute attr.



3
4
5
# File 'lib/valid_attribute/matcher.rb', line 3

def attr
  @attr
end

#failed_valuesObject

Returns the value of attribute failed_values.



3
4
5
# File 'lib/valid_attribute/matcher.rb', line 3

def failed_values
  @failed_values
end

#passed_valuesObject

Returns the value of attribute passed_values.



3
4
5
# File 'lib/valid_attribute/matcher.rb', line 3

def passed_values
  @passed_values
end

#subjectObject

Returns the value of attribute subject.



3
4
5
# File 'lib/valid_attribute/matcher.rb', line 3

def subject
  @subject
end

#values=(value) ⇒ Object

Sets the attribute values

Parameters:

  • value

    the value to set the attribute values to.



4
5
6
# File 'lib/valid_attribute/matcher.rb', line 4

def values=(value)
  @values = value
end

Instance Method Details

#cloneValidAttribute::Matcher

Force the matcher to clone the subject in between testing each value.

Warning This could lead to unintended results. The clone is not a deep copy



56
57
58
# File 'lib/valid_attribute/matcher.rb', line 56

def clone
  @clone = true
end

#clone?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/valid_attribute/matcher.rb', line 45

def clone?
  !!@clone
end

#descriptionObject



31
32
33
# File 'lib/valid_attribute/matcher.rb', line 31

def description
  "be valid when #{attr} is: #{quote_values(values)}"
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/valid_attribute/matcher.rb', line 40

def does_not_match?(subject)
  check_values(subject)
  !failed_values.empty? && passed_values.empty?
end

#failure_messageObject



22
23
24
# File 'lib/valid_attribute/matcher.rb', line 22

def failure_message
  message(failed_values, 'accept')
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/valid_attribute/matcher.rb', line 35

def matches?(subject)
  check_values(subject)
  failed_values.empty?
end

#negative_failure_messageObject Also known as: failure_message_when_negated



26
27
28
# File 'lib/valid_attribute/matcher.rb', line 26

def negative_failure_message
  message(passed_values, 'reject')
end

#when(*values) ⇒ ValidAttribute::Matcher

The collection of values to test against for the given attribute



17
18
19
20
# File 'lib/valid_attribute/matcher.rb', line 17

def when(*values)
  self.values = values
  self
end