Sha256: 2f9146fe7f8d7102ac5ad70255d7f659e055dd89a1c3657cd6242a7e7efac7fb

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'simplecheck/version'
require 'simplecheck/check_failed'

module Simplecheck
  def check(*arguments, error_message: nil, &block)
    default_error_message = if block_given?
                              Simplecheck.check_arguments_with_block(arguments, block)
                            else
                              Simplecheck.check_arguments(arguments)
                            end
    
    default_error_message ?  Simplecheck.handle_failure(error_message || default_error_message) : true
  end

  def self.check_arguments(arguments)
    case arguments.size
    when 1
      check_expression(arguments[0])
    else
      check_case_equality(*arguments)
    end
  end

  def self.check_arguments_with_block(arguments, block)
    check_arguments(arguments + [block])
  end

  def self.check_expression(expression_satisfied)
    'Condition is not satisfied' unless expression_satisfied
  end

  def self.check_case_equality(*arguments, check_argument)
    invalid_argument_index = arguments.index{ |argument| !(check_argument === argument) }

    if invalid_argument_index
      "#{ arguments[invalid_argument_index] } does not satisfy #{ check_argument }"
    end
  end

  def self.handle_failure(message)
    fail(Simplecheck::CheckFailed, message)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simplecheck-2.0 lib/simplecheck.rb