class Observed::ObservedTaskFactory

Public Class Methods

new(args={}) click to toggle source
# File lib/observed/observed_task_factory.rb, line 43
def initialize(args={})
  @task_factory = args[:task_factory] || Observed::TaskFactory.new(executor: Observed::BlockingExecutor.new)
end

Public Instance Methods

convert_to_task(underlying) click to toggle source

Convert the observer/translator/reporter to a task

# File lib/observed/observed_task_factory.rb, line 48
def convert_to_task(underlying)
  if underlying.is_a? Observed::Observer
    @task_factory.task {|data, options|
      options ||= {}
      m = underlying.method(:observe)
      fake_system = FakeSystem.new(time: options[:time])
      # For 0.1.0 compatibility
      underlying.configure(system: fake_system)
      underlying.configure(tag: options[:tag]) unless underlying.get_attribute_value(:tag)
      result = dispatch_method m, data, options
      fake_system.reported || result
    }
  elsif underlying.is_a? Observed::Reporter
    @task_factory.task {|data, options|
      options ||= {}
      m = underlying.method(:report)
      dispatch_method m, data, options
    }
  elsif underlying.is_a? Observed::Translator
    @task_factory.task {|data, options|
      options ||= {}
      m = underlying.method(:translate)
      dispatch_method m, data, options
    }
  else
    fail "Unexpected type of object which can not be converted to a task: #{underlying}"
  end
end
dispatch_method(m, data, options) click to toggle source
# File lib/observed/observed_task_factory.rb, line 77
def dispatch_method(m, data, options)
  num_parameters = m.parameters.size
  case num_parameters
  when 0
    m.call
  when 1
    m.call data
  when 2
    m.call data, options
  when 3
    # Deprecated. This is here for backward compatiblity
    m.call options[:tag], options[:time], data
  else
    fail "Unexpected number of parameters for the method `#{m}`: #{num_parameters}"
  end
end