Sha256: 971e06ff8d66185400d9709cf102cf2b832e70ec2322e4ba0b0c9397e10e964b

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

# Used in debugging to display an expression and its value. Examples:
#   x = 1
#   lll{'x'} #=> x = 1
#   lll("a message"){'x'} #=> a message: x = 1
#   lll "a message" #=> a message
# Enumerable members are output on separate lines

def lll msg = nil, &block
  Lll.lll msg, block
end

module Lll
  def self.lll msg, block = nil
    output_string = " "
    expression_value = 0
    if block
      output_string << msg + ': ' if msg
      expression_string = block.call
      expression_value = eval(expression_string, block.binding)
      output_string << expression_string + ' = '
      if defined?(AwesomePrint)
        output_string << expression_value.awesome_inspect << " \n"
      elsif enumerable? expression_value
        output_string << " \n"
        expression_value.each { |e| output_string << ' ' << e.inspect << " \n" }
      else
        output_string << expression_value.inspect << " \n"
      end
    else
      output_string << msg if msg
      output_string << " \n"
    end

    $stderr.puts format(output_string, ENV['TERM'] != 'dumb')
    Rails.logger.debug(format(output_string)) if defined?(Rails) && Rails.logger

    expression_value
  end

  def self.format output_string, colorize = true
    string = output_string
    string =  "\e[7m" + string + "\e[0m" if colorize
    string + "lll: #{caller[2].to_s} #{Time.now.strftime('%X')}"
  end

  def self.enumerable? value
    value.respond_to?(:each) &&
    !value.is_a?(String) &&
    (!defined? Nokogiri ||
     (!value.is_a?(Nokogiri::HTML::Document) &&
      !value.is_a?(Nokogiri::XML::Element)))
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lll-1.12.3 lib/lll.rb
lll-1.12.2 lib/lll.rb
lll-1.12.1 lib/lll.rb
lll-1.12.0 lib/lll.rb