Sha256: 8f001247b98b0f997102d5ad6a13dec31f76331076ed81d518aed68643edb79d

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

require File.join(File.dirname(__FILE__), 'allow_values_for_matcher')

module Remarkable
  module ActiveRecord
    module Matchers
      class ValidateInclusionOfMatcher < AllowValuesForMatcher #:nodoc:

        default_options :message => :inclusion

        protected

          def valid_values
            @options[:in]
          end

          def invalid_values
            if @in_range
              [ @options[:in].first - 1, @options[:in].last + 1 ]
            elsif @options[:in].empty?
              []
            else
              [ @options[:in].map(&:to_s).max.to_s.next ]
            end
          end

      end

      # Ensures that given values are valid for the attribute. If a range
      # is given, ensures that the attribute is valid in the given range.
      #
      # If you give that :size accepts ["S", "M", "L"], it will test that "T"
      # (the next of the array max value) is not allowed.
      #
      # == Options
      #
      # * <tt>:in</tt> - values to test inclusion.
      # * <tt>:allow_nil</tt> - when supplied, validates if it allows nil or not.
      # * <tt>:allow_blank</tt> - when supplied, validates if it allows blank or not.
      # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
      #   Regexp, string or symbol.  Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
      #
      # == Examples
      #
      #   should_validate_inclusion_of :size, :in => ["S", "M", "L", "XL"]
      #   should_validate_inclusion_of :age, :in => 18..100
      #
      #   it { should validate_inclusion_of(:size, :in => ["S", "M", "L", "XL"]) }
      #   it { should validate_inclusion_of(:age, :in => 18..100) }
      #
      def validate_inclusion_of(*args)
        ValidateInclusionOfMatcher.new(*args).spec(self)
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
remarkable_activerecord-3.0.4 lib/remarkable_activerecord/matchers/validate_inclusion_of_matcher.rb
remarkable_activerecord-3.0.6 lib/remarkable_activerecord/matchers/validate_inclusion_of_matcher.rb
remarkable_activerecord-3.0.5 lib/remarkable_activerecord/matchers/validate_inclusion_of_matcher.rb
remarkable_activerecord-3.0.7 lib/remarkable_activerecord/matchers/validate_inclusion_of_matcher.rb
remarkable_activerecord-3.0.8 lib/remarkable_activerecord/matchers/validate_inclusion_of_matcher.rb