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

Constructor Details

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

Returns a new instance of ValidationMatcher.



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

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.



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

def attr_name
  @attr_name
end

#display_optionsObject (readonly)

Returns the value of attribute display_options.



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

def display_options
  @display_options
end

#validator_classesObject (readonly)

Returns the value of attribute validator_classes.



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

def validator_classes
  @validator_classes
end

#validator_optionsObject (readonly)

Returns the value of attribute validator_options.



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

def validator_options
  @validator_options
end

Instance Method Details

#descriptionObject



65
66
67
68
69
# File 'lib/warp/model_matchers/validation_matcher.rb', line 65

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



57
58
59
# File 'lib/warp/model_matchers/validation_matcher.rb', line 57

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

#failure_message_when_negatedObject



61
62
63
# File 'lib/warp/model_matchers/validation_matcher.rb', line 61

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

#matches?(model_or_instance) ⇒ Boolean

Returns:

  • (Boolean)


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

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