Sha256: 395249ac585e1865ee02e7cd3d1c12433e7c2521ab001d44a0d9f1648197108f

Contents?: true

Size: 1.19 KB

Versions: 13

Compression:

Stored size: 1.19 KB

Contents

module Mongoid
  module Matchers
    module Validations
      class ValidateExclusionOfMatcher < HaveValidationMatcher
        def initialize(name)
          super(name, :exclusion)
        end

        def to_not_allow(*values)
          @not_allowed_values = [values].flatten
          self
        end

        def matches?(actual)
          return false unless result = super(actual)

          if @not_allowed_values
            allowed_values = @not_allowed_values - @validator.options[:in]
            if allowed_values.empty?
              @positive_result_message = @positive_result_message << " not allowing all values mentioned"
            else
              @negative_result_message = @negative_result_message << " allowing the following the ff. values: #{allowed_values.inspect}"
              result = false
            end
          end

          result
        end

        def description
          options_desc = []
          options_desc << " not allowing the ff. values: #{@not_allowed_values}" if @not_allowed_values
          super << options_desc.to_sentence
        end
      end

      def validate_exclusion_of(field)
        ValidateExclusionOfMatcher.new(field)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
mongoid-rspec-1.8.2 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.8.1 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.8.0 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.7.0 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.6.0 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.5.6 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.5.5 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.5.4 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.5.3 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.5.1 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.5.0 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.4.6 lib/matchers/validations/exclusion_of.rb
mongoid-rspec-1.4.5 lib/matchers/validations/exclusion_of.rb