Class: Fluent::NestedHashFilter::NestedObject
- Inherits:
-
Object
- Object
- Fluent::NestedHashFilter::NestedObject
- Defined in:
- lib/fluent/plugin/nested_hash_filter/nested_object.rb
Constant Summary collapse
- DEFAULT_CONNECTOR =
"."
Instance Attribute Summary collapse
-
#connector ⇒ Object
readonly
Returns the value of attribute connector.
-
#jsonify_keys ⇒ Object
Returns the value of attribute jsonify_keys.
-
#output ⇒ Object
Returns the value of attribute output.
-
#output_keys ⇒ Object
Returns the value of attribute output_keys.
Class Method Summary collapse
Instance Method Summary collapse
- #acts_as_json(value) ⇒ Object
- #add_key(key) ⇒ Object
- #convert_array(array) ⇒ Object
- #convert_hash(hash) ⇒ Object
- #current_key ⇒ Object
- #init_jsonify_keys(keys) ⇒ Object
-
#initialize(options = {}) ⇒ NestedObject
constructor
A new instance of NestedObject.
- #jsonified! ⇒ Object
- #jsonify_key? ⇒ Boolean
- #pop_key ⇒ Object
- #select(object) ⇒ Object
- #update(value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ NestedObject
Returns a new instance of NestedObject.
17 18 19 20 21 22 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 17 def initialize = {} @output = {} @output_keys = [] @connector = [:connector] || DEFAULT_CONNECTOR @jsonify_keys = init_jsonify_keys [:jsonify_keys] end |
Instance Attribute Details
#connector ⇒ Object (readonly)
Returns the value of attribute connector.
15 16 17 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 15 def connector @connector end |
#jsonify_keys ⇒ Object
Returns the value of attribute jsonify_keys.
14 15 16 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 14 def jsonify_keys @jsonify_keys end |
#output ⇒ Object
Returns the value of attribute output.
14 15 16 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 14 def output @output end |
#output_keys ⇒ Object
Returns the value of attribute output_keys.
14 15 16 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 14 def output_keys @output_keys end |
Class Method Details
.convert(object, options = {}) ⇒ Object
8 9 10 11 12 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 8 def self.convert object, = {} converter = new converter.select object converter.output end |
Instance Method Details
#acts_as_json(value) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 47 def acts_as_json value json = JSON.parse value select json rescue TypeError => error err = JSON::ParserError.new error. err.set_backtrace err.backtrace raise err rescue JSON::ParserError => error raise error ensure jsonified! end |
#add_key(key) ⇒ Object
35 36 37 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 35 def add_key key @output_keys.push key end |
#convert_array(array) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 100 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
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 88 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_key ⇒ Object
43 44 45 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 43 def current_key @output_keys.join connector end |
#init_jsonify_keys(keys) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 68 def init_jsonify_keys keys keys = keys || [] values = [true] * keys.size zipped = keys.zip values Hash[ zipped ] end |
#jsonified! ⇒ Object
64 65 66 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 64 def jsonified! @jsonify_keys[current_key] = false end |
#jsonify_key? ⇒ Boolean
60 61 62 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 60 def jsonify_key? !!@jsonify_keys[current_key] end |
#pop_key ⇒ Object
39 40 41 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 39 def pop_key @output_keys.pop end |
#select(object) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 24 def select object case object when Hash convert_hash object when Array convert_array object else update object end end |
#update(value) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fluent/plugin/nested_hash_filter/nested_object.rb', line 75 def update value case when current_key.empty? return when jsonify_key? acts_as_json value else @output.update current_key => value end rescue JSON::ParserError @output.update current_key => value end |