Class: Fluent::PluginMixin::MutateEvent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, expand_nesting: true) ⇒ MutateEvent

Returns a new instance of MutateEvent.



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

def initialize(record, expand_nesting: true)
  super(record)
  @record = record
  @event_time = nil
  @event_tag  = nil
  @expand_nesting = expand_nesting
end

Instance Attribute Details

#event_tagObject

Returns the value of attribute event_tag.



13
14
15
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 13

def event_tag
  @event_tag
end

#event_timeObject

Returns the value of attribute event_time.



12
13
14
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 12

def event_time
  @event_time
end

Instance Method Details

#dig(*keys) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 19

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



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

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)


72
73
74
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 72

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

#parent(key_or_path, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 44

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



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

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



63
64
65
66
67
68
69
70
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 63

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



52
53
54
55
56
57
58
59
60
61
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 52

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



15
16
17
# File 'lib/fluent/plugin_mixin/mutate_event.rb', line 15

def to_record
  @record
end