class ViewComponent::Storybook::MethodArgs::MethodArgs

Class representing arguments passed to a method which can be validated against the args of the target method

Attributes

args[R]
kwargs[R]
target_method[R]
target_method_params_names[R]

Public Class Methods

new(target_method, *args, **kwargs) click to toggle source
# File lib/view_component/storybook/method_args/method_args.rb, line 16
def initialize(target_method, *args, **kwargs)
  @target_method = target_method
  @args = args
  @kwargs = kwargs
  @target_method_params_names = MethodParametersNames.new(target_method)
end

Private Instance Methods

validate_args() click to toggle source
# File lib/view_component/storybook/method_args/method_args.rb, line 25
def validate_args
  arg_count = args.count

  if arg_count > target_method_params_names.max_arg_count
    errors.add(:args, :too_many, max: target_method_params_names.max_arg_count, count: arg_count)
  elsif arg_count < target_method_params_names.min_arg_count
    errors.add(:args, :too_few, min: target_method_params_names.min_arg_count, count: arg_count)
  end
end
validate_kwargs() click to toggle source
# File lib/view_component/storybook/method_args/method_args.rb, line 35
def validate_kwargs
  kwargs.each_key do |kwarg|
    unless target_method_params_names.include_kwarg?(kwarg)
      errors.add(:kwargs, :invalid_arg, kwarg: kwarg)
    end
  end

  return if target_method_params_names.covers_required_kwargs?(kwargs.keys)

  expected_keys = target_method_params_names.req_kwarg_names.join(', ')
  actual_keys = kwargs.keys.join(', ')

  errors.add(:kwargs, :invalid, expected_keys: expected_keys, actual_keys: actual_keys)
end