Sha256: 4e4670b83e4f2aa5fac3609a766fc50d261d0eb0d94ac01b91a924b32dca503e

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module RubyJard
  module Commands
    ##
    # Helpers to help validate commands
    module ValidationHelpers
      def validate_positive_integer!(input)
        input = input.to_s.strip
        unless input =~ /^[+\-\d]+$/
          raise ::Pry::CommandError, '`argument is not an integer'
        end

        input = input.to_i
        raise ::Pry::CommandError, 'argument must be positive' if input <= 0

        input
      end

      def validate_non_negative_integer!(input)
        input = input.to_s.strip
        unless input =~ /^[+\-\d]+$/
          raise ::Pry::CommandError, 'argument is not an integer'
        end

        input = input.to_i
        raise ::Pry::CommandError, 'argument must be positive' if input < 0

        input.to_i
      end

      def validate_present!(input)
        input = input.to_s.strip
        if input.empty?
          raise ::Pry::CommandError, 'argument must be present'
        end

        input
      end

      def validate_range!(input, from, to)
        if input < from || input > to
          raise ::Pry::CommandError, "argument must be from #{from} to #{to}"
        end

        input
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby_jard-0.3.1 lib/ruby_jard/commands/validation_helpers.rb
ruby_jard-0.3.0 lib/ruby_jard/commands/validation_helpers.rb
ruby_jard-0.2.3 lib/ruby_jard/commands/validation_helpers.rb