class Observed::ConfigBuilder

Public Class Methods

new(args) click to toggle source
# File lib/observed/config_builder.rb, line 49
def initialize(args)
  @group_mutex = ::Mutex.new
  @context = args[:context]
  @observer_plugins = args[:observer_plugins] if args[:observer_plugins]
  @reporter_plugins = args[:reporter_plugins] if args[:reporter_plugins]
  @translator_plugins = args[:translator_plugins] if args[:translator_plugins]
  @system = args[:system] || fail("The key :system must be in #{args}")
  configure args
end

Public Instance Methods

build() click to toggle source
# File lib/observed/config_builder.rb, line 83
def build
  Observed::Config.new(
      observers: observers,
      reporters: reporters
  )
end
emit(tag) click to toggle source
# File lib/observed/config_builder.rb, line 200
def emit(tag)
  e = @context.event_bus.emit(tag)
  e.name = "emit to #{tag}"
  e
end
group(name, observations=nil) click to toggle source

Updates or get the observations belongs to the group named `name`

# File lib/observed/config_builder.rb, line 211
def group(name, observations=nil)
  @group_mutex.synchronize do
    @observations ||= {}
    @observations[name] = observations if observations
    @observations[name] || []
  end
end
observe(tag=nil, args={}, &block) click to toggle source

@param [String] tag The tag which is assigned to data which is generated from this observer, and is sent to reporters later @param [Hash] args The configuration for each observer which may or may not contain (1) which observer plugin to use or which reader plugin to use (in combination with the default observer plugin) (2) initialization parameters to instantiate the observer/reader plugin

# File lib/observed/config_builder.rb, line 148
def observe(tag=nil, args={}, &block)
  if tag.is_a? ::Hash
    args = tag
    tag = nil
  end
  observer = if args[:via] || args[:using]
               via = args[:via] || args[:using] ||
                   fail(RuntimeError, %Q|Missing observer plugin name for the tag "#{tag}" in "#{args}"|)
               with = args[:with] || args[:which] || {}
               plugin = observer_plugins[via] ||
                   fail(RuntimeError, %Q|The observer plugin named "#{via}" is not found in "#{observer_plugins}"|)
               observer = plugin.new(({logger: logger}).merge(with).merge(tag: tag, system: system))
               ObserverCompatibilityAdapter.new(
                 system: system,
                 observer: observer,
                 tag: tag
               )
             elsif block_given?
               Observed::ProcObserver.new &block
             else
               fail "No args valid args (in args=#{args}) or a block given"
             end
  observe_that = convert_to_task(observer)
  result = if tag
    a = observe_that.then(emit(tag))
    group tag, (group(tag) + [a])
    a
  else
    observe_that
  end
  observers << result
  result.name = "observe"
  result
end
observer_plugins() click to toggle source
# File lib/observed/config_builder.rb, line 63
def observer_plugins
  @observer_plugins || select_named_plugins_of(Observed::Observer)
end
observers() click to toggle source
# File lib/observed/config_builder.rb, line 227
def observers
  @observers ||= []
end
receive(pattern) click to toggle source
# File lib/observed/config_builder.rb, line 206
def receive(pattern)
  @context.event_bus.receive(pattern)
end
report(tag_pattern=nil, args={}, &block) click to toggle source

@param [Regexp] tag_pattern The pattern to match tags added to data from observers @param [Hash] args The configuration for each reporter which may or may not contain (1) which reporter plugin to use or which writer plugin to use (in combination with the default reporter plugin) (2) initialization parameters to instantiate the reporter/writer plugin

# File lib/observed/config_builder.rb, line 94
def report(tag_pattern=nil, args={}, &block)
  if tag_pattern.is_a? ::Hash
    args = tag_pattern
    tag_pattern = nil
  end
  reporter = if args[:via] || args[:using]
               via = args[:via] || args[:using]
               with = args[:with] || args[:which] || {}
               with = ({logger: @logger}).merge(with).merge({tag_pattern: tag_pattern, system: system})
               plugin = reporter_plugins[via] ||
                   fail(RuntimeError, %Q|The reporter plugin named "#{via}" is not found in "#{reporter_plugins}"|)
               plugin.new(with)
             elsif block_given?
               Observed::ProcReporter.new tag_pattern, &block
             else
               fail "Invalid combination of arguments: #{tag_pattern} #{args}"
             end

  reporters << reporter
  report_it = convert_to_task(reporter)
  if tag_pattern
    receive(tag_pattern).then(report_it)
  end
  report_it
end
reporter_plugins() click to toggle source
# File lib/observed/config_builder.rb, line 67
def reporter_plugins
  @reporter_plugins || select_named_plugins_of(Observed::Reporter)
end
reporters() click to toggle source
# File lib/observed/config_builder.rb, line 223
def reporters
  @reporters ||= []
end
run_group(name) click to toggle source
# File lib/observed/config_builder.rb, line 219
def run_group(name)
  @context.task_factory.parallel(group(name))
end
select_named_plugins_of(klass) click to toggle source
# File lib/observed/config_builder.rb, line 75
def select_named_plugins_of(klass)
  plugins = {}
  klass.select_named_plugins.each do |plugin|
    plugins[plugin.plugin_name] = plugin
  end
  plugins
end
system() click to toggle source
# File lib/observed/config_builder.rb, line 59
def system
  @system
end
translate(args={}, &block) click to toggle source
# File lib/observed/config_builder.rb, line 183
def translate(args={}, &block)
  translator = if args[:via] || args[:using]
                 #tag_pattern || fail("Tag pattern missing: #{tag_pattern} where args: #{args}")
                 via = args[:via] || args[:using]
                 with = args[:with] || args[:which] || {}
                 with = ({logger: logger}).merge(with).merge({system: system})
                 plugin = translator_plugins[via] ||
                     fail(RuntimeError, %Q|The reporter plugin named "#{via}" is not found in "#{translator_plugins}"|)
                 plugin.new(with)
               else
                 Observed::ProcTranslator.new &block
             end
  task = convert_to_task(translator)
  task.name = "translate"
  task
end
translator_plugins() click to toggle source
# File lib/observed/config_builder.rb, line 71
def translator_plugins
  @translator_plugins || select_named_plugins_of(Observed::Translator)
end

Private Instance Methods

convert_to_task(underlying) click to toggle source
# File lib/observed/config_builder.rb, line 233
def convert_to_task(underlying)
  @observed_task_factory ||= @context.observed_task_factory
  @observed_task_factory.convert_to_task(underlying)
end