Class: Low::Redefiner

Inherits:
Object show all
Defined in:
lib/definitions/redefiner.rb

Overview

Redefine methods to have their arguments and return values type checked. ┌────────┐ ┌─────────┐ ┌─────────────┐ ┌─────────┐ ┌─────────┐│ Lowkey │ │ Proxies │ │ Expressions │ │ LowType │ │ Methods │└────┬───┘ └────┬────┘ └──────┬──────┘ └────┬────┘ └────┬────┘

│              │                 │                 │               │
│ Parses AST   │                 │                 │               │
├─────────────►│                 │                 │               │
│              │                 │                 │               │
│              │ Stores          │                 │               │
│              ├────────────────►│                 │               │
│              │                 │                 │               │
│              │                 │ Evaluates       │               │
│              │                 │◄────────────────┤               │
│              │                 │                 │               │
│              │                 │                 │ Redefines <-- YOU ARE HERE.
│              │                 │                 ├──────────────►│
│              │                 │                 │               │
│              │                 │ Validates       │               │
│              │                 │◄┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┤
│              │                 │                 │               │

Class Method Summary collapse

Class Method Details

.redefine(method_proxies:, class_proxy:) ⇒ Object

TODO: Pass in “klass” and use it to class_eval/eval methods in the binding of the class that included LowType.



30
31
32
33
34
35
36
# File 'lib/definitions/redefiner.rb', line 30

def redefine(method_proxies:, class_proxy:)
  if LowType.config.type_checking
    typed_methods(method_proxies:, class_proxy:)
  else
    untyped_methods(method_proxies:, class_proxy:)
  end
end

.untyped_args(args:, kwargs:, method_proxy:) ⇒ Object

rubocop:disable Metrics/AbcSize



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/definitions/redefiner.rb', line 38

def untyped_args(args:, kwargs:, method_proxy:) # rubocop:disable Metrics/AbcSize
  method_proxy.params_with_expressions.each do |param_proxy|
    value = param_proxy.position ? args[param_proxy.position] : kwargs[param_proxy.name]

    next unless value.nil?
    raise param_proxy.error_type, param_proxy.error_message(value:) if param_proxy.expression.required?

    value = param_proxy.expression.default_value # Default value can still be `nil`.
    value = value.value if value.is_a?(ValueExpression)
    param_proxy.position ? args[param_proxy.position] = value : kwargs[param_proxy.name] = value
  end

  [args, kwargs]
end