Class: Warp::ModelMatchers::ValidationMatcher

Inherits:
Matcher show all
Defined in:
lib/warp/model_matchers/validation_matcher.rb

Constant Summary collapse

VALIDATORS =
{
  acceptance: [ActiveModel::Validations::AcceptanceValidator],
  confirmation: [ActiveModel::Validations::ConfirmationValidator],
  exclusion: [ActiveModel::Validations::ExclusionValidator],
  format: [ActiveModel::Validations::FormatValidator],
  inclusion: [ActiveModel::Validations::InclusionValidator],
  length: [ActiveModel::Validations::LengthValidator],
  numericality: [ActiveModel::Validations::NumericalityValidator],
  presence: [ActiveModel::Validations::PresenceValidator]
}

Instance Attribute Summary collapse

Attributes inherited from Matcher

#model_or_instance

Instance Method Summary collapse

Methods inherited from Warp::Matcher

#description_of, #failure_message_for_should, #failure_message_for_should_not, #values_match?

Constructor Details

#initialize(attr_name, validator_classes, validator_options, display_options) ⇒ ValidationMatcher

Returns a new instance of ValidationMatcher.



36
37
38
39
40
41
# File 'lib/warp/model_matchers/validation_matcher.rb', line 36

def initialize(attr_name, validator_classes, validator_options, display_options)
  @attr_name = attr_name
  @validator_classes = validator_classes
  @validator_options = validator_options
  @display_options = display_options
end

Instance Attribute Details

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



34
35
36
# File 'lib/warp/model_matchers/validation_matcher.rb', line 34

def attr_name
  @attr_name
end

#display_optionsObject (readonly)

Returns the value of attribute display_options.



34
35
36
# File 'lib/warp/model_matchers/validation_matcher.rb', line 34

def display_options
  @display_options
end

#validator_classesObject (readonly)

Returns the value of attribute validator_classes.



34
35
36
# File 'lib/warp/model_matchers/validation_matcher.rb', line 34

def validator_classes
  @validator_classes
end

#validator_optionsObject (readonly)

Returns the value of attribute validator_options.



34
35
36
# File 'lib/warp/model_matchers/validation_matcher.rb', line 34

def validator_options
  @validator_options
end

Instance Method Details

#descriptionObject



63
64
65
66
67
# File 'lib/warp/model_matchers/validation_matcher.rb', line 63

def description
  str = "have validator #{validator_classes.map(&:name).join(", or ")} on :#{attr_name}"
  str << " with options #{description_of(display_options)}" if display_options.present?
  str
end

#failure_messageObject



55
56
57
# File 'lib/warp/model_matchers/validation_matcher.rb', line 55

def failure_message
  "expected #{model_name} to #{description}"
end

#failure_message_when_negatedObject



59
60
61
# File 'lib/warp/model_matchers/validation_matcher.rb', line 59

def failure_message_when_negated
  "expected #{model_name} to not #{description}"
end

#matches?(model_or_instance) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/warp/model_matchers/validation_matcher.rb', line 43

def matches?(model_or_instance)
  @model_or_instance = model_or_instance

  validators.any? do |validator|
    if validator_classes.any? {|validator_class| values_match?(validator_class, validator.class) }
      validator_options.nil? || values_match?(validator_options, validator.options)
    else
      false
    end
  end
end