Class: Logtail::Util::NonNilHashBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/logtail/util/non_nil_hash_builder.rb

Overview

The purpose of this class is to efficiently build a hash that does not include nil values. It’s proactive instead of reactive, avoiding the need to traverse and reduce a new hash dropping blanks.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNonNilHashBuilder

Returns a new instance of NonNilHashBuilder.



21
22
23
# File 'lib/logtail/util/non_nil_hash_builder.rb', line 21

def initialize
  @target = {}
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



19
20
21
# File 'lib/logtail/util/non_nil_hash_builder.rb', line 19

def target
  @target
end

Class Method Details

.build {|builder| ... } ⇒ Object

Yields:

  • (builder)


12
13
14
15
16
# File 'lib/logtail/util/non_nil_hash_builder.rb', line 12

def build(&block)
  builder = new
  yield builder
  builder.target
end

Instance Method Details

#add(k, v, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/logtail/util/non_nil_hash_builder.rb', line 25

def add(k, v, options = {})
  if !v.nil?
    if options[:json_encode]
      v = v.to_json
    end

    if options[:limit]
      v = v.byteslice(0, options[:limit])
    end

    @target[k] = v
  end
end