Class: Rack::WebProfiler::Collectors

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/web_profiler/collectors.rb

Overview

Collectors.

Container of Collector objects.

Defined Under Namespace

Classes: RackCollector, RequestCollector, RubyCollector, TimeCollector

Instance Method Summary collapse

Constructor Details

#initializeCollectors

Initialize.



13
14
15
16
# File 'lib/rack/web_profiler/collectors.rb', line 13

def initialize
  @collectors        = {}
  @sorted_collectors = {}
end

Instance Method Details

#add_collector(collector_class) ⇒ Object

Add a collector.

Parameters:

  • collector_class (Array, Class)

Raises:

  • (ArgumentError)

    if ‘collector_class’ is not a Class or is not an instance of Rack::WebProfiler::Collector::DSL or a collector with this identifier is already registrered.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rack/web_profiler/collectors.rb', line 41

def add_collector(collector_class)
  return collector_class.each { |c| add_collector(c) } if collector_class.is_a? Array


  raise ArgumentError, "`collector_class' must be a class" unless collector_class.is_a? Class

  unless collector_class.included_modules.include?(Rack::WebProfiler::Collector::DSL)
    raise ArgumentError, "#{collector_class.class.name} must be an instance of \"Rack::WebProfiler::Collector::DSL\""
  end

  definition = collector_class.definition

  if definition_by_identifier(definition.identifier)
    raise ArgumentError, "A collector with identifier \“#{definition.identifier}\" already exists"
  end

  return false unless definition.is_enabled?

  @collectors[collector_class] = definition

  sort_collectors!
end

#allHash<Symbol, Rack::WebProfiler::Collector::DSL::Definition>

Returns all collectors definition.

Returns:

  • (Hash<Symbol, Rack::WebProfiler::Collector::DSL::Definition>)


31
32
33
# File 'lib/rack/web_profiler/collectors.rb', line 31

def all
  @sorted_collectors
end

#definition_by_identifier(identifier) ⇒ Rack::WebProfiler::Collector::DSL::Definition?

Get a collector definition by his identifier.

Parameters:

  • identifier (String)

Returns:

  • (Rack::WebProfiler::Collector::DSL::Definition, nil)


23
24
25
26
# File 'lib/rack/web_profiler/collectors.rb', line 23

def definition_by_identifier(identifier)
  identifier = identifier.to_sym
  @sorted_collectors[identifier] unless @sorted_collectors[identifier].nil?
end

#remove_collector(collector_class) ⇒ Object

Remove a collector.

Parameters:

  • collector_class (Array, Class)

Raises:

  • (ArgumentError)

    if ‘collector_class’ is not a Class or if this collector is not registered.



69
70
71
72
73
74
75
76
77
78
# File 'lib/rack/web_profiler/collectors.rb', line 69

def remove_collector(collector_class)
  return collector_class.each { |c| remove_collector(c) } if collector_class.is_a? Array

  raise ArgumentError, "`collector_class' must be a class" unless collector_class.is_a? Class
  raise ArgumentError, "No collector found with class \“#{collector_class}\"" unless @collectors[collector_class]

  @collectors.delete(collector_class)

  sort_collectors!
end