Class: Remarkable::Mongoid::Matchers::ValidateUniquenessOfMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/remarkable/mongoid/validate_uniqueness_of.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr) ⇒ ValidateUniquenessOfMatcher

Returns a new instance of ValidateUniquenessOfMatcher.



19
20
21
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 19

def initialize(attr)
  self.attr = attr.to_sym
end

Instance Attribute Details

#attrObject

Returns the value of attribute attr.



17
18
19
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 17

def attr
  @attr
end

#messageObject

Returns the value of attribute message.



17
18
19
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 17

def message
  @message
end

#scopeObject

Returns the value of attribute scope.



17
18
19
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 17

def scope
  @scope
end

#validation_typeObject

Returns the value of attribute validation_type.



17
18
19
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 17

def validation_type
  @validation_type
end

Instance Method Details

#descriptionObject



44
45
46
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 44

def description
  "validates that :#{attr} is unique"
end

#failure_message_for_shouldObject



48
49
50
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 48

def failure_message_for_should
  "\nUniqueness validation failure\nExpected: '#{attr}' to be unique"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 33

def matches?(subject)
  @subject    = subject
  validations = @subject._validators[attr]
  if validations
    validation = validations.detect { |k| k.class == ::Mongoid::Validations::UniquenessValidator }
    validation && check_option(validation, :message) && check_option(validation, :scope)
  else
    false
  end
end

#scoped_to(scope) ⇒ Object



28
29
30
31
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 28

def scoped_to(scope)
  self.scope = scope.is_a?(Array) ? scope.map(&:to_sym) : scope.to_sym
  self
end

#with_message(message) ⇒ Object



23
24
25
26
# File 'lib/remarkable/mongoid/validate_uniqueness_of.rb', line 23

def with_message(message)
  self.message = message
  self
end