Module: Booth::MethodObject::ClassMethods

Defined in:
lib/booth/method_object.rb

Instance Method Summary collapse

Instance Method Details

#__check_for_unknown_options(*args, **kwargs) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/booth/method_object.rb', line 51

def __check_for_unknown_options(*args, **kwargs)
  return if __defined_options.empty?

  # Checking params
  opts = args.drop(__defined_params.length).first || kwargs
  raise ArgumentError, "Unexpected argument #{opts}" unless opts.is_a? Hash

  # Checking options
  unknown_options = opts.keys - __defined_options
  message = "Key(s) #{unknown_options} not found in #{__defined_options}"
  raise KeyError, message if unknown_options.any?
end

#__defined_optionsObject



64
65
66
# File 'lib/booth/method_object.rb', line 64

def __defined_options
  dry_initializer.options.map(&:source)
end

#__defined_paramsObject



68
69
70
# File 'lib/booth/method_object.rb', line 68

def __defined_params
  dry_initializer.params.map(&:source)
end

#call(*args, **kwargs) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/booth/method_object.rb', line 31

def call(*args, **kwargs, &)
  __check_for_unknown_options(*args, **kwargs)

  if kwargs.empty?
    # Preventing `Passing the keyword argument as the last hash parameter is deprecated`
    new(*args).call(&)
  else
    new(*args, **kwargs).call(&)
  end
end

#param(name, type = nil, **opts) ⇒ Object

Overriding the implementation of #param in the dry-initializer gem. Because of the positioning of multiple params, params can never be omitted in a method object.

Raises:

  • (ArgumentError)


44
45
46
47
48
49
# File 'lib/booth/method_object.rb', line 44

def param(name, type = nil, **opts, &)
  raise ArgumentError, "Default value for param not allowed - #{name}" if opts.key? :default
  raise ArgumentError, "Optional params not supported - #{name}" if opts.fetch(:optional, false)

  super
end