Module: Hintable

Extended by:
Decoratable
Defined in:
lib/decoratable/hintable.rb

Instance Method Summary collapse

Methods included from Decoratable

extended

Instance Method Details

#hintable(*classes, require_block: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/decoratable/hintable.rb', line 6

def hintable(*classes, require_block: false)
  argument_names = __decorated_method__.parameters.map { |(_, param)| param }
  checks = __args__.zip(classes, argument_names)

  # check args for types
  failed_check = checks.find { |arg, klass, _| !arg.is_a?(klass) }
  if failed_check
    raise ArgumentError, "#{failed_check[2]} expected argument of type #{failed_check[1]}, was #{failed_check[0].class} (#{failed_check[0].inspect})"
  end

  if require_block && __block__.nil?
    raise ArgumentError, "#{__decorated_method__.name} requires a block"
  end

  yield
end