Sha256: 6f97c37a714d201c8b651c9295bdee440c9cff6b80c862406135658e9a715d10

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'observed/hash/fetcher'
require 'observed/reporter'
require 'observed/reporter/regexp_matching'

module Observed
  module BuiltinPlugins
    class File < Observed::Reporter

      include Observed::Reporter::RegexpMatching

      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
        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}"
               end
        ::File.open(path, mode) do |f|
          f.puts formatted_data
        end
      end

      def self.plugin_name
        'file'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
observed-0.1.1 lib/observed/builtin_plugins/file.rb