class Enumpath::Logger

A logger for providing debugging information while evaluating path expressions @private

Constants

PAD
SEPARATOR

Attributes

level[RW]

@return [Integer] the indentation level to apply to log messages

logger[RW]

@return [::Logger, <<] a {::Logger}-compatible logger instance

Public Class Methods

new(logdev = STDOUT) click to toggle source

@param logdev [String, IO] The log device. See Ruby's {::Logger.new} documentation.

# File lib/enumpath/logger.rb, line 20
def initialize(logdev = STDOUT)
  @logger = ::Logger.new(logdev)
  @level = 0
  @padding = {}
end

Public Instance Methods

log(title) { || ... } click to toggle source

Generates a log message for debugging. Returns fast if {Enumpath.verbose} is false. Accepts an optional block which must contain a single hash, the contents of which will be added to the log message, and which are lazily evaluated only if {Enumpath.verbose} is true.

@param title [String] the title of this log message @yield A lazily evaluated hash of key/value pairs to include in the log message

# File lib/enumpath/logger.rb, line 32
def log(title)
  return unless Enumpath.verbose

  append_log "#{padding}#{SEPARATOR}\n"
  append_log "#{padding}Enumpath: #{title}\n"
  append_log "#{padding}#{SEPARATOR}\n" if block_given?
  log_vars(yield) if block_given?
end

Private Instance Methods

append_log(message) click to toggle source
# File lib/enumpath/logger.rb, line 43
def append_log(message)
  logger << message
end
enum_for_log(enum, length = 50) click to toggle source
# File lib/enumpath/logger.rb, line 70
def enum_for_log(enum, length = 50)
  json = enum.inspect
  "#{json[0...length]}#{json.length > length ? '...' : ''}"
end
log_vars(vars) click to toggle source
# File lib/enumpath/logger.rb, line 47
def log_vars(vars)
  return unless vars.is_a?(Hash)

  label_size = vars.keys.map(&:size).max
  vars.each do |label, value|
    append_log "#{padding}#{label.to_s.ljust(label_size)}: #{massaged_value(value)}\n"
  end
end
massaged_value(value) click to toggle source
# File lib/enumpath/logger.rb, line 56
def massaged_value(value) # rubocop:disable Metrics/MethodLength
  if value.is_a?(Enumerable)
    enum_for_log(value)
  elsif value.is_a?(TrueClass)
    'True'
  elsif value.is_a?(FalseClass)
    'False'
  elsif value.nil?
    'Nil'
  else
    value.to_s
  end
end
padding() click to toggle source
# File lib/enumpath/logger.rb, line 75
def padding
  PAD * level.to_i
end