Module: Ricecream

Extended by:
Config
Defined in:
lib/ricecream/ic.rb,
lib/ricecream/config.rb,
lib/ricecream/refine.rb,
lib/ricecream/version.rb,
lib/ricecream/analyzer.rb,
lib/ricecream/colorize.rb,
lib/ricecream/ic_method.rb

Defined Under Namespace

Modules: Config, P Classes: Analyzer, ICMethod

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Methods included from Config

arg_to_s, colorize=, disable, enable, include_context=, output, output=, prefix, prefix=

Class Method Details

.colorize_code(code) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/ricecream/colorize.rb', line 4

def self.colorize_code(code)
  return code unless @colorize
  require "irb"
  unless defined?(IRB::Color) &&
         IRB::Color.respond_to?(:colorize_code) &&
         @output.respond_to?(:tty?) &&
         @output.tty?
    return code
  end
  IRB::Color.colorize_code(code.to_s)
end

.context(location) ⇒ Object



43
44
45
# File 'lib/ricecream/ic.rb', line 43

def self.context(location)
  "#{location.path}:#{colorize_code(location.lineno)} in #{location.label}"
end

.format(location, name, args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ricecream/ic.rb', line 15

def self.format(location, name, args)
  str = prefix(location)
  arity = args.size
  if arity == 0
    return "#{str}#{context(location)} at #{Time.now.strftime('%H:%M:%S.%L')}"
  end

  path = location.absolute_path
  unless @ic_methods[path]
    require_relative "analyzer"
    @ic_methods[path] = Analyzer.new(path).ic_methods
  end

  lineno = location.lineno
  method = @ic_methods[path].find do |m|
    m.lineno == lineno && m.arity == arity && m.name == name
  end

  str += "#{context(location)}- " if @include_context || !method
  argstrs = method ? method.args.map { |arg| colorize_code(arg) } :
                     1.upto(arity).map { |i| "<arg#{i}>" }
  args = args.map { |arg| colorize_code(arg_to_s(arg)) }
  argpairs = argstrs.zip(args).map do |argstr, arg|
    argstr == arg ? argstr : "#{argstr}: #{arg}"
  end
  str + argpairs.join(", ")
end

.ic(location, name, args) ⇒ Object



11
12
13
# File 'lib/ricecream/ic.rb', line 11

def self.ic(location, name, args)
  output format(location, name, args) if @enable
end