Class: Fluent::NestedHashFilter::NestedObject

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/nested_hash_filter/nested_object.rb

Constant Summary collapse

DEFAULT_CONNECTOR =
"."

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NestedObject

Returns a new instance of NestedObject.



15
16
17
18
19
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 15

def initialize options = {}
  @output      = {}
  @output_keys = []
  @connector   = options[:connector] || DEFAULT_CONNECTOR
end

Instance Attribute Details

#connectorObject (readonly)

Returns the value of attribute connector.



13
14
15
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 13

def connector
  @connector
end

#outputObject

Returns the value of attribute output.



12
13
14
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 12

def output
  @output
end

#output_keysObject

Returns the value of attribute output_keys.



12
13
14
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 12

def output_keys
  @output_keys
end

Class Method Details

.convert(object, options = {}) ⇒ Object



6
7
8
9
10
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 6

def self.convert object, options = {}
  converter = new options
  converter.select object
  converter.output
end

Instance Method Details

#add_key(key) ⇒ Object



32
33
34
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 32

def add_key key
  @output_keys.push key
end

#convert_array(array) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 62

def convert_array array
  if array.empty?
    update nil
  end

  array.each_with_index do |value, index|
    add_key index
    select  value
    pop_key
  end
end

#convert_hash(hash) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 50

def convert_hash hash
  if hash.keys.empty?
    update nil
  end

  hash.each do |key, value|
    add_key key
    select  value
    pop_key
  end
end

#current_keyObject



40
41
42
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 40

def current_key
  @output_keys.join connector
end

#pop_keyObject



36
37
38
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 36

def pop_key
  @output_keys.pop
end

#select(object) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 21

def select object
  case object
  when Hash
    convert_hash object
  when Array
    convert_array object
  else
    update object
  end
end

#update(value) ⇒ Object



44
45
46
47
48
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 44

def update value
  unless current_key.empty?
    @output.update current_key => value
  end
end