Class: DataMapper::Matchers::ValidatePresenceOf

Inherits:
ValidationMatcher show all
Defined in:
lib/dm/matchers/validate_presence_of.rb

Instance Method Summary collapse

Methods inherited from ValidationMatcher

#description, #failure_message, #failure_message_when_negated, #initialize, set_default_msg_reg, set_validation_subject, #with_message

Constructor Details

This class inherits a constructor from DataMapper::Matchers::ValidationMatcher

Instance Method Details

#failure_message_for_presenceObject



22
23
24
# File 'lib/dm/matchers/validate_presence_of.rb', line 22

def failure_message_for_presence
  "expected to validate presence of #{@property}#{with_msg}"
end

#matches?(model) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dm/matchers/validate_presence_of.rb', line 10

def matches?(model)
  model_class = model.is_a?(Class) ? model : model.class

  validators = model_class.validators.contexts[:default]
  presence_of = validators.find do |validator|
    validator.is_a? DataMapper::Validations::PresenceValidator and validator.field_name == @property
  end
  return false unless presence_of
  return false if @msg and @msg != presence_of.options[:message]
  true
end

#negative_failure_messageObject



26
27
28
# File 'lib/dm/matchers/validate_presence_of.rb', line 26

def negative_failure_message
  "expected to not validate presence of #{@property}#{with_msg}"
end

#with_msgObject



30
31
32
# File 'lib/dm/matchers/validate_presence_of.rb', line 30

def with_msg
  " with message \"#{@msg}\"" if @msg
end