Sha256: c88339e3638cfbe256fa5aa8f1e53c40c7841984074be6469161f2a2f3ba7c4d

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

module Shoulda
  module Matchers
    module ActiveModel
      class ValidationMatcher
        # @private
        class BuildDescription
          def self.call(matcher, main_description)
            new(matcher, main_description).call
          end

          def initialize(matcher, main_description)
            @matcher = matcher
            @main_description = main_description
          end

          def call
            if description_clauses_for_qualifiers.any?
              "#{main_description}#{clause_for_allow_blank_or_nil},"\
              " #{description_clauses_for_qualifiers.to_sentence}"
            else
              main_description + clause_for_allow_blank_or_nil
            end
          end

          protected

          attr_reader :matcher, :main_description

          private

          def clause_for_allow_blank_or_nil
            if matcher.try(:expects_to_allow_blank?)
              ' as long as it is not blank'
            elsif matcher.try(:expects_to_allow_nil?)
              ' as long as it is not nil'
            else
              ''
            end
          end

          def description_clauses_for_qualifiers
            description_clauses = []

            if matcher.try(:expects_strict?)
              description_clauses <<
                if matcher.try(:expects_custom_validation_message?)
                  'raising a validation exception with a custom message on failure'
                else
                  'raising a validation exception on failure'
                end
            elsif matcher.try(:expects_custom_validation_message?)
              description_clauses <<
                'producing a custom validation error on failure'
            end

            description_clauses
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
shoulda-matchers-6.4.0 lib/shoulda/matchers/active_model/validation_matcher/build_description.rb
shoulda-matchers-6.3.1 lib/shoulda/matchers/active_model/validation_matcher/build_description.rb
shoulda-matchers-6.3.0 lib/shoulda/matchers/active_model/validation_matcher/build_description.rb
shoulda-matchers-6.2.0 lib/shoulda/matchers/active_model/validation_matcher/build_description.rb
shoulda-matchers-6.1.0 lib/shoulda/matchers/active_model/validation_matcher/build_description.rb