Sha256: 093648ab9a6fb31e055221ad66a3af9e2cce49241d7dd11f71a3265c9efc4798

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Lumberjack
  class Tags
    class << self
      # Transform hash keys to strings. This method exists for optimization and backward compatibility.
      # If a hash already has string keys, it will be returned as is.
      def stringify_keys(hash)
        return nil if hash.nil?
        if hash.keys.all? { |key| key.is_a?(String) }
          hash
        elsif hash.respond_to?(:transform_keys)
          hash.transform_keys(&:to_s)
        else
          copy = {}
          hash.each do |key, value|
            copy[key.to_s] = value
          end
          copy
        end
      end

      # Ensure keys are strings and expand any values in a hash that are Proc's by calling them and replacing
      # the value with the result. This allows setting global tags with runtime values.
      def expand_runtime_values(hash)
        return nil if hash.nil?
        if hash.all? { |key, value| key.is_a?(String) && !value.is_a?(Proc) }
          return hash
        end
        
        copy = {}
        hash.each do |key, value|
          if value.is_a?(Proc) && (value.arity == 0 || value.arity == -1)
            value = value.call
          end
          copy[key.to_s] = value
        end
        copy
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lumberjack-1.2.7 lib/lumberjack/tags.rb
lumberjack-1.2.6 lib/lumberjack/tags.rb
lumberjack-1.2.5 lib/lumberjack/tags.rb
lumberjack-1.2.4 lib/lumberjack/tags.rb
lumberjack-1.2.3 lib/lumberjack/tags.rb
lumberjack-1.2.2 lib/lumberjack/tags.rb
lumberjack-1.2.1 lib/lumberjack/tags.rb
lumberjack-1.2.0 lib/lumberjack/tags.rb
lumberjack-1.1.1 lib/lumberjack/tags.rb