NeuronCheck has a simple plug-in mechanism, and users can add original keywords for declaration in addition to built-in keywords.
For example, if you want to add the new keyword boolean
, please describe in the following manner.
require 'neuroncheck'
NeuronCheckSystem::Plugin.add_keyword(:boolean) do
# Method that is called when you use the keyword
# if it is called a keyword in with arguments like `respondable(:each)`, is it also passed as an argument to the on_call method
def on_call
end
# Method to perform the actual check processing. There is a need to return either true or false
def match?(value)
value.equal?(true) or value.equal?(false)
end
# String that represents the contents of the keyword. It is used in the error message
def expected_caption
"boolean value"
end
end
Among the NeuronCheckSystem::Plugin.add_keyword
block, like a normal class definition of Ruby, you can define a method. Then, by defining the above-mentioned three methods in which you can define the operation of the keyword.
Keyword registered in the manner described above, it is possible to use in the same way as other keywords in the ndecl
block.
module Foo
extend NeuronCheck
ndecl {
args boolean
}
def self.foo_func(flag: false)
end
end
Foo.foo_func
Foo.foo_func(flag: 'invalid')
#=> script.rb:30:in `<main>': argument `flag' of `Foo.foo_func' must be boolean value, but was "invalid" (NeuronCheckError)
# got: "invalid"
# signature: Foo.foo_func(flag:boolean value)
# declared at: script.rb:22:in `block in <module:Foo>'
#
Built-in keywords such as respondable
, any
and except
have been defined by using the plug-in mechanism. (However, there is also a place where you are using the method is not described in the above) If you want to see the contents of the implementation, please refer to the source file neuroncheck/builtin_keyword.rb.
It should be noted that, in addition to the NeuronCheckSystem::Plugin.add_keyword
, It can also be used two methods below.
require 'neuroncheck'
# As a `boolean` keyword of aliases, define a new `bool` keyword
NeuronCheckSystem::Plugin.alias_keyword(:bool, :boolean)
# To remove a `boolean` keyword previously defined
NeuronCheckSystem::Plugin.remove_keyword(:boolean)