lib/observed/builtin_plugins/file.rb in observed-0.1.1 vs lib/observed/builtin_plugins/file.rb in observed-0.2.0.rc1
- old
+ new
@@ -1,49 +1,40 @@
require 'observed/hash/fetcher'
require 'observed/reporter'
require 'observed/reporter/regexp_matching'
+require 'observed/reporter/report_formatting'
module Observed
module BuiltinPlugins
class File < Observed::Reporter
include Observed::Reporter::RegexpMatching
+ include Observed::Reporter::ReportFormatting
UNDEFINED = Object.new
- attribute :format, default: -> tag, time, data { "#{Time.at(time)} #{tag} #{data}" }
attribute :path
attribute :mode, default: :append
# @param [String] tag
# @param [Time] time
# @param [Hash] data
def report(tag, time, data)
- num_params = format.parameters.size
- formatted_data = case num_params
- when 3
- format.call(tag, time, data)
- when 4
- format.call(tag, time, data, Observed::Hash::Fetcher.new(data))
- else
- fail "Number of parameters for the function for the key :format must be 3 or 4, but was #{num_params}(#{format.parameters}"
- end
+ formatted_data = format_report(tag, time, data)
mode = case self.mode
when :append, 'a'
'a'
when :overwrite, 'w'
'w'
else
fail "Unsupported value for the parameter `:mode`: Supported values are :append, :overwrite, " +
- "'a', 'w'. The specified value is #{self.mode.inspect}"
+ "'a', 'w'. The specified value is #{self.mode.inspect}"
end
::File.open(path, mode) do |f|
f.puts formatted_data
end
end
- def self.plugin_name
- 'file'
- end
+ plugin_name 'file'
end
end
end