class Logtail::Util::NonNilHashBuilder

@private

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.

Attributes

target[R]

Public Class Methods

build() { |builder| ... } click to toggle source
# File lib/logtail/util/non_nil_hash_builder.rb, line 12
def build(&block)
  builder = new
  yield builder
  builder.target
end
new() click to toggle source
# File lib/logtail/util/non_nil_hash_builder.rb, line 21
def initialize
  @target = {}
end

Public Instance Methods

add(k, v, options = {}) click to toggle source
# 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