Class: Logtail::Util::NonNilHashBuilder
- Inherits:
-
Object
- Object
- Logtail::Util::NonNilHashBuilder
- 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
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
Instance Method Summary collapse
- #add(k, v, options = {}) ⇒ Object
-
#initialize ⇒ NonNilHashBuilder
constructor
A new instance of NonNilHashBuilder.
Constructor Details
#initialize ⇒ NonNilHashBuilder
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
#target ⇒ Object (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
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, = {}) if !v.nil? if [:json_encode] v = v.to_json end if [:limit] v = v.byteslice(0, [:limit]) end @target[k] = v end end |