Class: Signal::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/signal/mock.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



9
10
11
12
13
# File 'lib/signal/mock.rb', line 9

def method_missing(name, *args)
  return super unless respond_to_missing?(name)

  calls << {event: name.to_s.gsub(/^on_/, "").to_sym, args: args}
end

Instance Method Details

#callsObject



5
6
7
# File 'lib/signal/mock.rb', line 5

def calls
  @calls ||= []
end

#on(event) ⇒ Object



28
29
30
# File 'lib/signal/mock.rb', line 28

def on(event)
  proc {|*args| calls << {event: event, args: args} }
end

#received?(event, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/signal/mock.rb', line 19

def received?(event, options = {})
  received_event?(event, options[:times] || -1) &&
    received_with?(event, options[:with])
end

#respond_to_missing?(name, _include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/signal/mock.rb', line 15

def respond_to_missing?(name, _include_all = false)
  name =~ /^on_/
end

#to_procObject



24
25
26
# File 'lib/signal/mock.rb', line 24

def to_proc
  proc {|action| action.add_listener(self) }
end