Class: Fluent::PluginMixin::MutateEvent

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/fluent/plugin_mixin/mutate_event.rb

Instance Method Summary collapse

Constructor Details

#initialize(record, expand_nesting = true) ⇒ MutateEvent

Returns a new instance of MutateEvent.



4
5
6
7
8
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 4

def initialize(record, expand_nesting = true)
  super(record)
  @record = record
  @expand_nesting = expand_nesting
end

Instance Method Details

#dig(*keys) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 14

def dig(*keys)
  item = @record

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

  item
end

#get(key_or_path, &block) ⇒ Object



34
35
36
37
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 34

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

#include?(key_or_path) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 67

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

#parent(key_or_path, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 39

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

#pruneObject



25
26
27
28
29
30
31
32
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 25

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

#remove(key_or_path) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 58

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

#set(key_or_path, value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 47

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

#to_recordObject



10
11
12
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 10

def to_record
  @record
end