Sha256: fcc9b5df6fd6aa24ada0f35ee8f709ebabfdc82b97398141872663d18d761099

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

module Fluent
  module PluginMixin
    class MutateEvent < SimpleDelegator
      def initialize(record, expand_nesting = true)
        super(record)
        @record = record
        @expand_nesting = expand_nesting
      end

      def to_record
        @record
      end

      def dig(*keys)
        item = @record

        keys.each do |key|
          break if item.nil?
          item = item.is_a?(Hash) ? item[key] : nil
        end

        item
      end

      def prune
        delete_proc = proc do |_,v|
          v.delete_if(&delete_proc) if v.respond_to?(:delete_if)
          v.nil? || v.respond_to?(:empty?) && v.empty?
        end

        @record.delete_if(&delete_proc)
      end

      def get(key_or_path, &block)
        item = dig(*expand_key(key_or_path))
        block_given? ? yield(item) : item
      end

      def parent(key_or_path, &block)
        path = expand_key(key_or_path)
        child = path.pop

        item = dig(*path)
        block_given? ? yield(item, child) : item
      end

      def set(key_or_path, value)
        path = expand_key(key_or_path)
        child = path.pop

        item = @record
        path.each do |key|
          item = item[key] ||= {}
        end
        item[child] = value
      end

      def remove(key_or_path)
        path = expand_key(key_or_path)
        child = path.pop

        parent(key_or_path) do |item, child|
          item.delete(child) unless item.nil?
        end
      end

      def include?(key_or_path)
        !get(key_or_path).nil?
      end

      protected

      def expand_key(key_or_path)
        @expand_nesting ? key_or_path.split(".") : [key_or_path]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fluent-plugin-mutate_filter-0.1.2 lib/fluent/plugin_mixin/mutate_event.rb
fluent-plugin-mutate_filter-0.1.1 lib/fluent/plugin_mixin/mutate_event.rb
fluent-plugin-mutate_filter-0.1.0 lib/fluent/plugin_mixin/mutate_event.rb