Class: Blabbermouth::Blabber

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



119
120
121
# File 'lib/blabbermouth/blabber.rb', line 119

def method_missing(meth, *args, &block)
  blab meth, *args, &block
end

Instance Attribute Details

#bystandersObject (readonly)

Returns the value of attribute bystanders.



3
4
5
# File 'lib/blabbermouth/blabber.rb', line 3

def bystanders
  @bystanders
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/blabbermouth/blabber.rb', line 3

def options
  @options
end

Class Method Details

.count(key, total, *args) ⇒ Object



28
29
30
31
# File 'lib/blabbermouth/blabber.rb', line 28

def count(key, total, *args)
  bystanders, opts = parse_args(*args)
  new(*bystanders).count(key, total, opts)
end

.error(key, e, *args) ⇒ Object



13
14
15
16
# File 'lib/blabbermouth/blabber.rb', line 13

def error(key, e, *args)
  bystanders, opts = parse_args(*args)
  new(*bystanders).error(key, e, opts)
end

.flush(*args) ⇒ Object



38
39
40
41
# File 'lib/blabbermouth/blabber.rb', line 38

def flush(*args)
  bystanders, opts = parse_args(*args)
  new(*bystanders).flush
end

.increment(key, by = 1, *args) ⇒ Object



23
24
25
26
# File 'lib/blabbermouth/blabber.rb', line 23

def increment(key, by=1, *args)
  bystanders, opts = parse_args(*args)
  new(*bystanders).increment(key, by, opts)
end

.info(key, msg = nil, *args) ⇒ Object



18
19
20
21
# File 'lib/blabbermouth/blabber.rb', line 18

def info(key, msg=nil, *args)
  bystanders, opts = parse_args(*args)
  new(*bystanders).info(key, msg, opts)
end

.parse_args(*args) ⇒ Object



6
7
8
9
10
11
# File 'lib/blabbermouth/blabber.rb', line 6

def parse_args(*args)
  opts = args.extract_options!
  args = Blabbermouth.configuration.bystanders if args.empty?
  bystanders = args + [opts.slice!(:data)]
  [bystanders, opts]
end

.time(key, duration = nil, *args, &block) ⇒ Object



33
34
35
36
# File 'lib/blabbermouth/blabber.rb', line 33

def time(key, duration=nil, *args, &block)
  bystanders, opts = parse_args(*args)
  new(*bystanders).time(key, duration, opts, &block)
end

Instance Method Details

#add_bystander(bystander) ⇒ Object



52
53
54
55
56
# File 'lib/blabbermouth/blabber.rb', line 52

def add_bystander(bystander)
  add_bystander! bystander
rescue => e
  false
end

#add_bystander!(bystander) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/blabbermouth/blabber.rb', line 44

def add_bystander!(bystander)
  @bystanders ||= []
  unless bystander_exists?(bystander)
    @bystanders << "Blabbermouth::Bystanders::#{bystander.to_s.camelize}".constantize.new(@options[bystander] || {})
  end
  @bystanders
end

#add_bystanders(*bystdrs) ⇒ Object



58
59
60
61
62
# File 'lib/blabbermouth/blabber.rb', line 58

def add_bystanders(*bystdrs)
  @options.merge!(bystdrs.extract_options!)
  bystdrs.concat(options.keys).uniq
  bystdrs.each { |bystander| add_bystander! bystander }
end

#blab(meth, key, *args, &block) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/blabbermouth/blabber.rb', line 103

def blab(meth, key, *args, &block)
  opts = args.extract_options!
  bystanders.map do |bystander|
    next unless bystander.respond_to?(meth)
    bystander_args = args.dup.push(bystander_options(bystander, opts))
    bystander.send meth, key, *bystander_args, &block unless Blabbermouth.configuration.gagged == true
  end
end

#count(key, total, *args) ⇒ Object



87
88
89
# File 'lib/blabbermouth/blabber.rb', line 87

def count(key, total, *args)
  blab :count, key, total, *args
end

#error(key, e, *args) ⇒ Object



75
76
77
# File 'lib/blabbermouth/blabber.rb', line 75

def error(key, e, *args)
  blab :error, key, e, *args
end

#flushObject



112
113
114
115
116
117
# File 'lib/blabbermouth/blabber.rb', line 112

def flush
  bystanders.map do |bystander|
    next unless bystander.respond_to?(:flush)
    bystander.flush
  end
end

#increment(key, by = 1, *args) ⇒ Object



83
84
85
# File 'lib/blabbermouth/blabber.rb', line 83

def increment(key, by=1, *args)
  blab :increment, key, by, *args
end

#info(key, msg = nil, *args) ⇒ Object



79
80
81
# File 'lib/blabbermouth/blabber.rb', line 79

def info(key, msg=nil, *args)
  blab :info, key, msg, *args
end

#remove_bystander(bystander) ⇒ Object



69
70
71
72
73
# File 'lib/blabbermouth/blabber.rb', line 69

def remove_bystander(bystander)
  remove_bystander! bystander
rescue => e
  false
end

#remove_bystander!(bystander) ⇒ Object



64
65
66
67
# File 'lib/blabbermouth/blabber.rb', line 64

def remove_bystander!(bystander)
  return if @bystanders.nil?
  @bystanders.slice!(bystander_index(bystander), 1)
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/blabbermouth/blabber.rb', line 123

def respond_to_missing?(meth, include_private=false)
  bystanders.any? { |bystander| bystander.respond_to?(meth, include_private) }
end

#time(key, duration = nil, *args, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/blabbermouth/blabber.rb', line 91

def time(key, duration=nil, *args, &block)
  raise "Blabbermouth::Blabber#time requires a duration or block" if duration.nil? && !block_given?

  if block_given?
    start_time = ::Time.now
    yielded = yield
    duration = (::Time.now - start_time).to_f
  end

  blab :time, key, duration, *args
end