NeuronCheck is a library for following 2 functions with declarative syntax

  1. Checking method parameters and return value
  2. Checking pre-conditions and post-conditions

It has 2 features. First one is that it can be carried out various types of checks with a simple description. The other one is that it can be introduced with little adverse effect on the production environment performance. In addition, in the form incorporated into other library, you can also use only the check function part.

NeuronCheck has been made is inspired by Rubype gem and by Contract Programming of the concept of Eiffel and D language. We aim to “in the form of an executable document, describes the type information and pre-conditions, so as to perform the check in accordance with its description” thing.


The code example below, will help to understand what can be done in a specific NeuronCheck.

require 'neuroncheck'

class Converter
  # Activate NeuronCheck on Converter class
  extend NeuronCheck

  # NeuronCheck check declaration block
  ndecl {
    # Arguments declaration (this method receives 3 arguments - 1st is String, 2nd is any object has #each method, and 3rd is Numeric or nil.)
    args String, respondable(:each), [Numeric, nil]

    # Return value declaration (this method returns self)
    returns :self

    # Precondition check
    precond do
      assert(threshold >= 0)
    end
  }

  # Actual method definition
  def convert(text, keywords, threshold = nil)
    # (main process)
  end
end

conv = Converter.new
conv.convert(100, ['Blog', 'Learning'], 0.5)

# => main.rb:28:in `<main>': 1st argument `text' of `Converter#convert' must be String, but was 100 (NeuronCheckError)
#            got: 100
#      signature: Converter#convert(text:String, keywords:respondable(:each), threshold:[Numeric, nil]) -> self
#    declared at: main.rb:10:in `block in <class:Converter>'

By that you declare the type and pre-conditions of the arguments and return values with ndecl, you can run a check against defined below convert method.

Also, if only type checking of arguments and return value is required, by a combination of Refinement function that has been officially introduced in Ruby 2.1 or later, you can also write to shorten in the following manner.

require 'neuroncheck'
using NeuronCheckSyntax # To enable check declaration by only this file

class Converter
  # The same operation as ndecl of the previous example. Check three arguments and return value at the same time
  sig String, respondable(:each), [Numeric, nil] => self

  def convert(text, keywords, threshold = nil)
    # (Main Process)
  end
end

If you want to know more about the actual use, please refer to the basic usage.

Features

NeuronCheck has been made with the aim that the following.

That can be carried out check in a natural description as much as possible

Ruby is the language that emphasizes “can be written without stress,” but, especially for type checking and basic description, such as the condition check, briefly as possible, intuitive to write that is important. Otherwise, because itself to check is the stress, resulting in skived a willingness to check.

NeuronCheck is as short as possible, in a declarative description, I try to be to allow the check.

It does not affect the performance

Check processing is usually not a treatment that may affect the performance. In particular, type checking and such as basic range checking, is even more so as long as it is simple. It’s not only takes such time as good almost ignored in the top of the processing time.

However, can have is to understand in so head, still performance is what would in the mood. We do not want to realize that “I wonder this checking process have an impact on how much performance” every time you write a check processing. It becomes our stress.

Approach NeuronCheck was selected for this problem is the same as the Eiffel and D language. You can be in production environment (release environment) In turn off all of the check.

This ensures that you do not almost exacerbated by performance check processing in a production environment, and we also in such check processing of a little just time, you will be able to run without having to worry about.

The following when you create a Web application in Sinatra, it is an example to disable all of the check only in the production environment.

configure :production
  NeuronCheck.disable
end
Others
  • As with an inspired source Rubype, it is possible introduction of progressive check. Only the methods required, you can partially to add a check.

  • It has a simple plug-in mechanism, it allows users to add keywords for your own check. Added keyword, it is possible to use the check in arguments and return values.
    Please refer to [Add user keywords][../add-user-keyword/] for more information.

  • By using the NeuronCheck.get_declarations_as_json method, the definition information for all the methods that have been declared so far, you can get in bulk.
    Please refer to [API to get informations of declarations][../get-declarations-api/] for more information.