module Taipo::Result::ClassMethods
A helper module for holding the DSL methods
@since 1.5.0 @api private
Public Instance Methods
result(method_name, type)
click to toggle source
The DSL method used to declare a type check on the return value of a method. The intention is that a user will declare the type of result expected from a method by calling this method in the body of a class definition.
For purposes of readability, the convention is that {Taipo::Result::ClassMethods#result} will be called near the beginning of the class definition but this is not a requirement and the user is free to call the method immediately before or after the relevant method definition.
@param method_name [Symbol] the name of the instance method @param type [String] a type definition
@since 1.5.0 @api public
@example
require 'taipo' class A include Taipo::Result result :foo, 'String' result :bar, 'Integer' def foo(arg) arg.to_s end def bar(arg) arg.to_s end end a = A.new a.foo 'Hello world!' #=> "Hello world!" a.bar 42 #=> Taipo::TypeError
Calls superclass method
# File lib/taipo/result.rb, line 82 def result(method_name, type) base = self checker = const_get "#{base.class.name}Checker" checker.class_eval do define_method(method_name) do |*args, &block| method_return_value = super(*args, &block) if Taipo::Utilities.match?(object: method_return_value, definition: type) method_return_value else Taipo::Utilities.throw_error(object: method_return_value, name: "#{base.name}##{method_name}", definition: type, result: true) end end end end