Class: Logcast::Broadcaster

Inherits:
Object
  • Object
show all
Defined in:
lib/logcast/broadcaster.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object


36
37
38
39
40
41
42
43
# File 'lib/logcast/broadcaster.rb', line 36

def method_missing(name, *args, &block)
  responding = subscribers.select { |s| s.respond_to?(name) }
  if responding.any?
    responding.map { |s| s.send(name, *args, &block) }.last
  else
    super
  end
end

Instance Method Details

#subscribe(subscriber, &block) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/logcast/broadcaster.rb', line 4

def subscribe(subscriber, &block)
  unless subscriber.respond_to?(:add)
    original_subscriber = subscriber
    subscriber = Logger.new(subscriber)
    subscriber.instance_variable_set(:@logcast_original_subscriber, original_subscriber)
  end

  # Apply existing level to the new subscriber.
  if subscribers.any?
    subscriber.level = subscribers.first.level
  end

  if block
    if already_subscribed?(subscriber)
      yield
    else
      begin
        subscribers << subscriber
        yield
      ensure
        subscribers.pop
      end
    end
  else
    subscribers << subscriber unless already_subscribed?(subscriber)
  end
end

#subscribersObject


32
33
34
# File 'lib/logcast/broadcaster.rb', line 32

def subscribers
  @subscribers ||= []
end

#warn(*args, &block) ⇒ Object

Kernel#warn is defined by Ruby


46
47
48
# File 'lib/logcast/broadcaster.rb', line 46

def warn(*args, &block)
  method_missing("warn", *args, &block)
end