Sha256: 6f7f1158c732061ed1afd18a9fd5aa5457f60b0358cf8eefd820406de935bb3a

Contents?: true

Size: 559 Bytes

Versions: 3

Compression:

Stored size: 559 Bytes

Contents

# frozen_string_literal: true

module SimpleValidate
  class ValidatesTypeOf < ValidatesBase
    SUPPORTED_TYPES = %i[string integer float].freeze

    def initialize(attribute, options)
      @type = options[:as]

      raise ArgumentError unless @type && SUPPORTED_TYPES.include?(@type)

      @klass = Utils.classify(options[:as])

      super(attribute, options[:message] ||
        "must be #{Utils.article(@type)} #{@type}", options[:if] || proc { true })
    end

    def valid?(instance)
      instance.send(attribute).is_a?(@klass)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_validate-2.0.2 lib/simple_validate/validates_type_of.rb
simple_validate-2.0.1 lib/simple_validate/validates_type_of.rb
simple_validate-2.0.0 lib/simple_validate/validates_type_of.rb