lib/rubype.rb in rubype-0.2.3 vs lib/rubype.rb in rubype-0.2.4

- old
+ new

@@ -25,10 +25,18 @@ def type_info if methods_hash = Rubype.typed_method_info[owner] methods_hash[name] end end + + def arg_types + type_info.first.first if type_info + end + + def return_type + type_info.first.last if type_info + end end module Rubype class ArgumentTypeError < ::TypeError; end class ReturnTypeError < ::TypeError; end @@ -41,19 +49,19 @@ private # @param caller [Object] # @param type_info_hash [Hash] { [ArgInfo_1, ArgInfo_2, ... ArgInfo_n] => RtnInfo } # @param module [Module] - def define_typed_method(meth_caller, meth, type_info_hash, __rubype__) + def define_typed_method(owner, meth, type_info_hash, __rubype__) arg_types, rtn_type = *strip_type_info(type_info_hash) - @@typed_method_info[meth_caller][meth] = { + @@typed_method_info[owner][meth] = { arg_types => rtn_type } __rubype__.send(:define_method, meth) do |*args, &block| - ::Rubype.send(:assert_arg_type, meth_caller, meth, args, arg_types) + ::Rubype.send(:assert_arg_type, self, meth, args, arg_types) rtn = super(*args, &block) - ::Rubype.send(:assert_trn_type, meth_caller, meth, rtn, rtn_type) + ::Rubype.send(:assert_trn_type, self, meth, rtn, rtn_type) rtn end end # @param type_info_hash [Hash] { [ArgInfo_1, ArgInfo_2, ... ArgInfo_n] => RtnInfo } @@ -65,33 +73,33 @@ # @param caller [Module] # @param meth [Symbol] # @param args [Array<Object>] # @param type_infos [Array<Class, Symbol>] - def assert_arg_type(caller, meth, args, type_infos) + def assert_arg_type(meth_caller, meth, args, type_infos) args.zip(type_infos).each.with_index(1) do |(arg, type_info), i| case type_check(arg, type_info) when :need_correct_class raise ArgumentTypeError, - "Expected #{caller.class}##{meth}'s #{i}th argument to be #{type_info} but got #{arg.inspect} instead" + "Expected #{meth_caller.class}##{meth}'s #{i}th argument to be #{type_info} but got #{arg.inspect} instead" when :need_correct_method raise ArgumentTypeError, - "Expected #{caller.class}##{meth}'s #{i}th argument to have method ##{type_info} but got #{arg.inspect} instead" + "Expected #{meth_caller.class}##{meth}'s #{i}th argument to have method ##{type_info} but got #{arg.inspect} instead" end end end # @param caller [Module] # @param rtn [Object] # @param type_info [Class, Symbol] - def assert_trn_type(caller, meth, rtn, type_info) + def assert_trn_type(meth_caller, meth, rtn, type_info) case type_check(rtn, type_info) when :need_correct_class raise ReturnTypeError, - "Expected #{caller.class}##{meth} to return #{type_info} but got #{rtn.inspect} instead" + "Expected #{meth_caller.class}##{meth} to return #{type_info} but got #{rtn.inspect} instead" when :need_correct_method raise ReturnTypeError, - "Expected #{caller.class}##{meth} to return object which has method ##{type_info} but got #{rtn.inspect} instead" + "Expected #{meth_caller.class}##{meth} to return object which has method ##{type_info} but got #{rtn.inspect} instead" end end # @param obj [Object] # @param type_info [Class, Symbol]