module MethodObject::ClassMethods

Public Instance Methods

__check_for_unknown_options(*args) click to toggle source
# File lib/method_object.rb, line 32
def __check_for_unknown_options(*args)
  return if __defined_options.empty?

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

  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_options() click to toggle source
# File lib/method_object.rb, line 43
def __defined_options
  dry_initializer.options.map(&:source)
end
__defined_params() click to toggle source
# File lib/method_object.rb, line 47
def __defined_params
  dry_initializer.params.map(&:source)
end
assign(variable, to:) click to toggle source

DEPRECATED

# File lib/method_object.rb, line 26
def assign(variable, to:)
  warn 'MethodObject.assign is deprecated. ' \
       "Please use this instead: `option #{variable.inspect}, default: ...`"
  option variable, default: to
end
call(*args, &block) click to toggle source
# File lib/method_object.rb, line 11
def call(*args, &block)
  __check_for_unknown_options(*args)
  new(*args).call(&block)
end
param(name, type = nil, **opts, &block) click to toggle source

Because of the positioning of multiple params, params can never be omitted.

# File lib/method_object.rb, line 17
def param(name, type = nil, **opts, &block)
  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)

  dry_initializer.param(name, type, **opts, &block)
  self
end