Class: ActiveWindow::Signal

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

Defined Under Namespace

Classes: UnknownSignalError

Constant Summary collapse

@@signals =
{}

Class Method Summary collapse

Class Method Details

.define(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/active_window/signal.rb', line 23

def self.define name
  class_eval <<-EOCODE
    def self.emit_#{name}(*args)
      self.emit(:#{name},*args)
    end
    def self.on_#{name}(&block)
      self.on(:#{name}, &block)
    end
  EOCODE
end

.emit(name, *args) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/active_window/signal.rb', line 11

def self.emit(name, *args)
  if @@signals[name].is_a?(Array)
    @@signals[name].each do |block|
      block.call(*args)
    end
  end
end

.method_missing(meth, *args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/active_window/signal.rb', line 34

def self.method_missing(meth,*args,&block)
  case meth.to_s
  when /^emit_(.*)$/
    raise UnknownSignalError, "cannot emit unknown signal '#{$1}'"
  when /^on_(.*)$/
    raise UnknownSignalError, "cannot connect to unknown signal '#{$1}'"
  else
    raise NoMethodError, "unknown method: #{meth}"
  end
end

.on(name, &block) ⇒ Object



6
7
8
9
# File 'lib/active_window/signal.rb', line 6

def self.on(name, &block)
  @@signals[name] ||= []
  @@signals[name] << block
end

.signalsObject



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

def self.signals
  @@signals
end