Sha256: 927653df2b9e805f80eea921b09cd3a33ed324b8f36ab5dad6a6f1f236544c24

Contents?: true

Size: 815 Bytes

Versions: 2

Compression:

Stored size: 815 Bytes

Contents

require 'json'

module Timber
  module Util
    # @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.
    class NonNilHashBuilder
      class << self
        def build(&block)
          builder = new
          yield builder
          builder.target
        end
      end

      attr_reader :target

      def initialize
        @target = {}
      end

      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
timber-3.0.1 lib/timber/util/non_nil_hash_builder.rb
timber-3.0.0 lib/timber/util/non_nil_hash_builder.rb