lib/simplecheck.rb in simplecheck-1.0 vs lib/simplecheck.rb in simplecheck-2.0

- old
+ new

@@ -1,43 +1,43 @@ require 'simplecheck/version' require 'simplecheck/check_failed' module Simplecheck - def check( *arguments, &block ) - error_message = if block_given? - Simplecheck.check_arguments_with_block( arguments, block ) - else - Simplecheck.check_arguments( arguments ) - end - - error_message ? Simplecheck.handle_failure( error_message ) : true + 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 Simplecheck.check_arguments( arguments ) + def self.check_arguments(arguments) case arguments.size when 1 - Simplecheck.check_expression( arguments[ 0 ]) + check_expression(arguments[0]) else - Simplecheck.check_case_equality( *arguments ) + check_case_equality(*arguments) end end - def Simplecheck.check_arguments_with_block( arguments, block ) - Simplecheck.check_arguments(( arguments + [ block ])) + def self.check_arguments_with_block(arguments, block) + check_arguments(arguments + [block]) end - def Simplecheck.check_expression( expression ) - if !expression - 'Condition is not satisfied' - end + def self.check_expression(expression_satisfied) + 'Condition is not satisfied' unless expression_satisfied end - def Simplecheck.check_case_equality( *arguments, check_argument ) - if invalid_argument = arguments.find{ |argument| !( check_argument === argument )} - "#{ invalid_argument } does not satisfy #{ check_argument }" + 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 Simplecheck.handle_failure( message ) - raise Simplecheck::CheckFailed.new( message ) + def self.handle_failure(message) + fail(Simplecheck::CheckFailed, message) end end