Class: Yoda::Instrument

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/yoda/instrument.rb

Defined Under Namespace

Classes: Progress, Subscription

Instance Attribute Summary collapse

event_name collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstrument

Returns a new instance of Instrument.



86
87
88
# File 'lib/yoda/instrument.rb', line 86

def initialize
  @subscriptions = []
end

Instance Attribute Details

#subscriptionsArray<Subscription> (readonly)

Returns:



61
62
63
# File 'lib/yoda/instrument.rb', line 61

def subscriptions
  @subscriptions
end

Class Method Details

.cleanObject



74
75
76
# File 'lib/yoda/instrument.rb', line 74

def clean
  local.value = Instrument.new
end

.instanceInstrument

Returns Instrument instance (thread local).

Returns:



70
71
72
# File 'lib/yoda/instrument.rb', line 70

def instance
  local.value
end

Instance Method Details

#build_library_registry(message:, name:, version:) ⇒ Object

Parameters:

  • name (String)
  • version (String)
  • message (String)


137
138
139
# File 'lib/yoda/instrument.rb', line 137

def build_library_registry(message:, name:, version:)
  emit(:build_library_registry, name: name, version: version, message: message)
end

#emit(name, **params) ⇒ Object

Parameters:

  • name (String)
  • params (Hash)


108
109
110
111
# File 'lib/yoda/instrument.rb', line 108

def emit(name, **params)
  Logger.trace("#{name}: #{params}")
  subscriptions.select { |subscription| subscription.name === name }.each { |subscription| subscription.call(**params) }
end

#hear(subscription_hash = {}) ⇒ Object

Add subscriptions and eval the given block. these subscriptions are unsubscribed after the block.

Parameters:

  • subscription_hash (Hash{ Symbol, String => #call }) (defaults to: {})


92
93
94
95
96
97
# File 'lib/yoda/instrument.rb', line 92

def hear(subscription_hash = {})
  subscriptions = subscription_hash.map { |key, value| subscribe(key, &value) }
  value = yield
  subscriptions.each(&:unsubscribe)
  value
end

#initialization_progress(phase:, message:, index: nil, length: nil) ⇒ Object

Parameters:

  • phase (Symbol)
  • message (String)
  • index (Integer, nil) (defaults to: nil)
  • length (Integer, nil) (defaults to: nil)


124
125
126
# File 'lib/yoda/instrument.rb', line 124

def initialization_progress(phase:, message:, index: nil, length: nil)
  emit(:initialization_progress, phase: phase, message: message, index: index, length: length)
end

#registry_dump(index: nil, length: nil) ⇒ Object

Parameters:

  • index (Integer, nil) (defaults to: nil)
  • length (Integer, nil) (defaults to: nil)


130
131
132
# File 'lib/yoda/instrument.rb', line 130

def registry_dump(index: nil, length: nil)
  emit(:registry_dump, index: index, length: length)
end

#subscribe(name, &callback) ⇒ Subsctiption

Parameters:

  • name (String, Symbol)
  • callback (#call)

Returns:

  • (Subsctiption)


102
103
104
# File 'lib/yoda/instrument.rb', line 102

def subscribe(name, &callback)
  Subscription.new(instrument: self, name: name, callback: callback).tap { |subscription| subscriptions.push(subscription) }
end

#unsubscribe(subscription) ⇒ Object

Parameters:



114
115
116
# File 'lib/yoda/instrument.rb', line 114

def unsubscribe(subscription)
  subscriptions.delete(subscription)
end