Skip to content

Commit

Permalink
Refactor ExecutionJobFactory to make it less complex
Browse files Browse the repository at this point in the history
  • Loading branch information
KUOKA Yusuke committed Dec 5, 2013
1 parent 4d27b63 commit 876be73
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions lib/observed/execution_job_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,54 +54,42 @@ def convert_to_job(underlying)
# For 0.1.0 compatibility
underlying.configure(system: fake_system)
underlying.configure(tag: options[:tag]) unless underlying.get_attribute_value(:tag)
result = case m.parameters.size
when 0
underlying.observe
when 1
underlying.observe(data)
when 2
underlying.observe(data, options)
end
result = dispatch_method m, data, options
fake_system.reported || result
}
elsif underlying.is_a? Observed::Reporter
@job_factory.job {|data, options|
options ||= {}
m = underlying.method(:report)
num_parameters = m.parameters.size
case num_parameters
when 1
underlying.report(data)
when 2
underlying.report(data, options)
when 3
# Deprecated. This is here for backward compatiblity
underlying.report(options[:tag], options[:time], data)
else
fail "Unexpected number of parameters for the method `report`: #{num_parameters}"
end
dispatch_method m, data, options
}
elsif underlying.is_a? Observed::Translator
@job_factory.job {|data, options|
options ||= {}
m = underlying.method(:translate)
num_parameters = m.parameters.size
case num_parameters
when 1
underlying.translate(data)
when 2
underlying.translate(data, options)
when 3
# Deprecated. This is here for backward compatiblity
underlying.translate(options[:tag], options[:time], data)
else
fail "Unexpected number of parameters for the method `translate`: #{num_parameters}"
end
dispatch_method m, data, options
}
else
fail "Unexpected type of object which can not be converted to a job: #{underlying}"
end
end

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

end
end

0 comments on commit 876be73

Please sign in to comment.