Class: Typeguard::Validation::Validator
- Inherits:
-
Object
- Object
- Typeguard::Validation::Validator
- Defined in:
- lib/typeguard/validator.rb
Class Method Summary collapse
- .exhaustive_path(mod, method, sig) ⇒ Object
- .param_names(zipped_params) ⇒ Object
- .param_validator(types) ⇒ Object
- .zip_params(method_params, sig_params) ⇒ Object
Class Method Details
.exhaustive_path(mod, method, sig) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/typeguard/validator.rb', line 41 def self.exhaustive_path(mod, method, sig) zipped_params = zip_params(method.parameters, sig.parameters) return_validator = (param_validator(sig.returns.types) if sig.returns && !sig.returns.types.empty?) p_names = param_names(zipped_params) block_params = p_names.map(&:first).join(', ') call_args = p_names.map(&:last).reject { |s| ['*', '**', '&'].include?(s) }.join(', ') locals = method.parameters.map do |s| return nil if s.size == 1 # Ruby 3.1.0 compatible ['*', '**', '&'].include?(s.last.to_s) ? nil : s.last end.compact.join(', ') redefinition = sig.scope == :class ? 'define_singleton_method' : 'define_method' if return_validator mod.module_eval " \#{redefinition}(sig.name) do |\#{block_params}|\n zipped_params.zip([\#{locals}]).each do |(mp, sp, validator), local|\n next if validator.nil? || validator.valid?(local)\n\n Metrics.report_unexpected_argument(sig, sp.types_string, local, mod.name, sp)\n end\n result = method.bind_call(self, \#{call_args})\n unless return_validator.valid?(result)\n Metrics.report_unexpected_return(sig, sig.returns.types_string, result, mod.name)\n end\n result\n end\n RUBY\n else\n mod.module_eval <<~RUBY, __FILE__, __LINE__ + 1\n \#{redefinition}(sig.name) do |\#{block_params}|\n zipped_params.zip([\#{locals}]).each do |(mp, sp, validator), local|\n next if validator.nil? || validator.valid?(local)\n\n Metrics.report_unexpected_argument(sig, sp.types_string, local, mod.name, sp)\n end\n method.bind_call(self, \#{call_args})\n end\n RUBY\n end\nend\n", __FILE__, __LINE__ + 1 |
.param_names(zipped_params) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/typeguard/validator.rb', line 23 def self.param_names(zipped_params) # Tuples of [parameter, invocation] names zipped_params.map do |(type, name), sp, _| name = name.to_s case type when :req then [name, name] # foo when :keyreq then ["#{name}:", "#{name}: #{name}"] # foo: when :keyrest then [name == '**' ? name : "**#{name}"] * 2 # **foo when :rest then [name == '*' ? name : "*#{name}"] * 2 # *foo when :block then [name == '&' ? name : "&#{name}"] * 2 # &foo when :opt then ["#{name} = (#{sp.default})", name] # foo = (bar) when :key then ["#{name}: (#{sp.default})", "#{name}: #{name}"] # foo: (bar) when :nokey then ['**nil'] * 2 # **nil else raise type.to_s end end end |
.param_validator(types) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/typeguard/validator.rb', line 6 def self.param_validator(types) children = types.map { |node| Base.from(node) } if children.size == 1 children.first else UnionOf.new(children) end end |
.zip_params(method_params, sig_params) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/typeguard/validator.rb', line 15 def self.zip_params(method_params, sig_params) method_params.map do |mp| sig_param = sig_params.find { |sp| sp.name.to_s.gsub(/[*:]/, '').to_sym == mp.last } validator = param_validator(sig_param.types) if sig_param [mp, sig_param, validator] end end |