Class: KO::Signals::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/ko/signals/validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(signal) ⇒ Validator

Returns a new instance of Validator.



6
7
8
# File 'lib/ko/signals/validator.rb', line 6

def initialize(signal)
  @signal = signal
end

Instance Method Details

#validate_args!(args) ⇒ Object

Raises:



10
11
12
13
14
15
16
# File 'lib/ko/signals/validator.rb', line 10

def validate_args!(args)
  types = args.map(&:class)

  return if types.count == @signal.arg_types.count && types_match?(types)

  raise KO::EmitError, "expected args: #{@signal.arg_types}. given: #{types}"
end

#validate_receiver!(receiver) ⇒ Object



18
19
20
21
# File 'lib/ko/signals/validator.rb', line 18

def validate_receiver!(receiver)
  validate_receiver_type!(receiver)
  validate_signal_arity!(receiver) if receiver.is_a?(Signal)
end

#validate_receiver_type!(receiver) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
# File 'lib/ko/signals/validator.rb', line 23

def validate_receiver_type!(receiver)
  name = @signal.receiver_name
  return if receiver.respond_to?(:call) || receiver.respond_to?(name)

  raise ArgumentError, "receiver must be a Signal, callable or respond to #{name}: #{receiver.class}"
end