Sha256: dfc7d9b443872bb6c825d0a6b2d1728a4d7e2334456ecc0a774321dac65eaec0

Contents?: true

Size: 807 Bytes

Versions: 2

Compression:

Stored size: 807 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module Mixture
  module Validate
    # Checks that a value is present.
    class Presence < Base
      register_as :presence
      # Performs the validation.
      #
      # @param (see Base#validate)
      # @return (see Base#validate)
      # @raise [ValidationError] If {#empty?} returns true.
      def validate(record, attribute, value)
        super
        error("Value is empty") if empty?
      end

    private

      # Determins if the given value is empty.  If it's not nil,
      # and it responds to `empty?`, it returns the value of `empty?`;
      # otherwise, it returns the value of `nil?`.
      #
      # @return [Boolean]
      def empty?
        @value.nil? || (@value.respond_to?(:empty?) && @value.empty?)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mixture-0.7.1 lib/mixture/validate/presence.rb
mixture-0.7.0 lib/mixture/validate/presence.rb