Top Level Namespace

Defined Under Namespace

Modules: ScaleRb, TypeEnforcer Classes: Hash

Instance Method Summary collapse

Instance Method Details

#assert_equal(expected, actual) ⇒ Object



56
57
58
# File 'lib/custom_assign.rb', line 56

def assert_equal(expected, actual)
  raise "Expected #{expected}, but got #{actual}" unless expected === actual
end

#build_assigned_params(method, defaults, args, kwargs) ⇒ Object



60
61
62
63
# File 'lib/custom_assign.rb', line 60

def build_assigned_params(method, defaults, args, kwargs)
  positional_params, keyword_params = get_method_params(method)
  custom_assign(positional_params, keyword_params, args, kwargs, defaults)
end

#custom_assign(positional_params, keyword_params, args, kwargs = {}, defaults = {}) ⇒ Object

Raises:

  • (ArgumentError)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/custom_assign.rb', line 1

def custom_assign(positional_params, keyword_params, args, kwargs = {}, defaults = {})
  assigned = {}

  # Handle positional arguments (with optional defaults)
  raise ArgumentError, 'Too many positional arguments' if args.length > positional_params.length

  positional_params.each_with_index do |param, index|
    if args[index]
      assigned[param] = args[index] # Assign from args if available
    elsif args.length == positional_params.length
      assigned[param] = nil
    elsif defaults.key?(param)
      assigned[param] = defaults[param] # Assign default if no argument is provided
    else
      raise ArgumentError, "Missing required positional argument: #{param}"
    end
  end

  # Handle keyword arguments (with optional defaults)
  kwargs.each_key do |key|
    raise ArgumentError, "Unknown keyword argument: #{key}" unless keyword_params.include?(key)
  end

  keyword_params.each do |key|
    if kwargs.key?(key)
      assigned[key] = kwargs[key] # Assign from kwargs if available
    elsif defaults.key?(key)
      assigned[key] = defaults[key] # Assign default if not provided
    else
      raise ArgumentError, "Missing required keyword argument: #{key}"
    end
  end

  # assert_equal(positional_params.length + keyword_params.length, assigned.length)
  assigned
end

#get_method_params(method) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/custom_assign.rb', line 38

def get_method_params(method)
  params = method.parameters

  positional_params = []
  keyword_params = []

  params.each do |type, name|
    case type
    when :req, :opt # Required, optional positional arguments
      positional_params << name
    when :key, :keyreq # Keyword arguments
      keyword_params << name
    end
  end

  [positional_params, keyword_params]
end