Class: Informer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reporter) ⇒ Informer

Returns a new instance of Informer.



15
16
17
18
# File 'lib/informer.rb', line 15

def initialize(reporter)
  @reporter = get_reporter(reporter)
  @watchers = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



45
46
47
48
49
50
51
52
53
54
# File 'lib/informer.rb', line 45

def method_missing(method, *args, &block)
  begin
    watcher = Watcher.const_get("#{method.to_s.camelize}")
    @watchers << watcher.new(*args, &block)
  rescue NameError => e
    puts "No watcher named #{method.to_s}. Error was: #{e}"
    super
  end
  
end

Instance Attribute Details

#watchersObject (readonly)

Returns the value of attribute watchers.



13
14
15
# File 'lib/informer.rb', line 13

def watchers
  @watchers
end

Class Method Details

.watch(reporter = "standard_output", &block) ⇒ Object



28
29
30
31
32
# File 'lib/informer.rb', line 28

def self.watch(reporter = "standard_output", &block)
  informer = Informer.new(reporter)
  informer.instance_eval(&block)
  informer.report
end

Instance Method Details

#messagesObject



20
21
22
# File 'lib/informer.rb', line 20

def messages
  watchers.map {|watcher| watcher.report}
end

#reportObject



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

def report
  @reporter.report(messages)
end