Sha256: 42aa4fee60dab844402d12398fdd63ded23079f3e7b922c997383bd079241f2c

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

%w[base
   presence_of
   format_of
   type_of
   length_of
   inclusion_of
   exclusion_of].each do |validation|
     require "simple_validate/validates_#{validation}"
   end

require "simple_validate/errors"
require "simple_validate/version"
require "simple_validate/utils"

module SimpleValidate
  def self.included(klass)
    klass.extend(ClassMethods)
    klass.include(InstanceMethods)
  end

  module InstanceMethods
    def valid?
      self.class.validate(self)
    end

    def invalid?
      !valid?
    end

    def errors
      @errors ||= Errors.new
    end
  end

  module ClassMethods
    def method_missing(method, *args, &block)
      if respond_to?(method)
        add_validations(args, const_get(Utils.camelize(method)))
      else
        super
      end
    end

    def respond_to_missing?(method, include_private = false)
      method.to_s =~ /(validates_
                       (format|
                        presence|
                        type|
                        inclusion|
                        exclusion|
                        length)_of)
      /x || super
    end

    def add_validations(args, klass)
      options = Utils.extract_options! args
      args.each do |attr|
        validations << klass.new(attr, options)
      end
    end

    def validations
      @validations ||= []
    end

    def validate(instance)
      instance.errors.clear!

      validations.each do |validation|
        if validation.condition.call && !validation.valid?(instance)
          instance.errors.add(validation.attribute, validation.message)
        end
      end
      instance.errors.empty?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_validate-2.2.2 lib/simple_validate.rb