Class: Observed::ObservedTaskFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/observed/observed_task_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ObservedTaskFactory

Returns a new instance of ObservedTaskFactory.



43
44
45
# 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

Instance Method Details

#convert_to_task(underlying) ⇒ Object

Convert the observer/translator/reporter to a task



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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