Class: MiniTest::Matchers::ActiveModel::ValidateUniquenessMatcher

Inherits:
ValidationMatcher
  • Object
show all
Defined in:
lib/matchers/validate_uniqueness_matcher.rb

Instance Method Summary collapse

Methods inherited from ValidationMatcher

#failure_message, #negative_failure_message, #on, #with_message

Methods included from Helpers

#class_of, #to_sentence

Constructor Details

#initialize(attr) ⇒ ValidateUniquenessMatcher

Returns a new instance of ValidateUniquenessMatcher


22
23
24
25
26
27
# File 'lib/matchers/validate_uniqueness_matcher.rb', line 22

def initialize attr
  @scope = nil
  @case_insensitive = false

  super attr, :uniqueness
end

Instance Method Details

#case_insensitiveObject


34
35
36
37
# File 'lib/matchers/validate_uniqueness_matcher.rb', line 34

def case_insensitive
  @case_insensitive = true
  self
end

#descriptionObject


48
49
50
51
52
53
# File 'lib/matchers/validate_uniqueness_matcher.rb', line 48

def description
  desc = []
  desc  << " scoped to #{to_sentence(@scope)}" if @scope
  desc << ' allowing case insensitive values'  if @case_insensitive
  super << desc.to_sentence
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)

39
40
41
42
43
44
45
46
# File 'lib/matchers/validate_uniqueness_matcher.rb', line 39

def matches? subject
  return false unless @result = super(subject)

  check_scope if @scope
  check_case_sensivity if @case_insensitive

  @result
end

#scoped_to(*scope) ⇒ Object


29
30
31
32
# File 'lib/matchers/validate_uniqueness_matcher.rb', line 29

def scoped_to *scope
  @scope = [scope].flatten.map(&:to_s)
  self
end