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

#respond_to_missing?(name, include_all) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(name, include_all)
  subscribers.any? { |s| s.respond_to?(name) } || super
end

#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



50
51
52
# File 'lib/logcast/broadcaster.rb', line 50

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