Class: Object

Inherits:
BasicObject
Defined in:
lib/cutter/stamper.rb,
lib/cutter/inspection.rb,
lib/cutter/inspection.rb,
lib/cutter/colored_outputs.rb

Instance Method Summary collapse

Instance Method Details

#__colorize__(obj) ⇒ Object



4
5
6
7
8
# File 'lib/cutter/colored_outputs.rb', line 4

def __colorize__ obj
  colors = Cutter::ColoredOutputs.colors_config
  color = colors[obj] || :default
  color != :default ? to_s.send(color) : to_s
end

#inspect!(*options, &block) ⇒ Object Also known as: iii

#inspect! may be called inside any method as ‘inspect! {}’ or more rigorously as ‘inspect!(binding)’ Binding is a Ruby class: www.ruby-doc.org/core/classes/Binding.html

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cutter/inspection.rb', line 24

def inspect! *options, &block
  return true if Cutter::Inspection.quiet?

  # Getting binding
  _binding = options.first if options.first.class == Binding
  raise ArgumentError, "Try inspect(binding) or inspect! {}", caller if (!block_given? && !_binding)
  _binding ||= block.binding

  max = true if options.include? :max
  options << :instance << :max << :self << :caller if max
  options.uniq!

  iv = true if options.include? :instance

  # Want caller methods chain to be traced? - pass option :caller to #inspect!
  _caller = true if options.include? :caller

  self_inspection = eval('self.inspect', _binding) if options.include? :self

  # Basic info
  method_name = eval('__method__', _binding)
  class_name = eval('self.class', _binding)

  if method_name && (meth = method(method_name.to_sym)).respond_to?(:source_location)
    source_path, source_number = meth.source_location
  end

  puts "\n%s `%s' %s" % ['method:'.__colorize__(:method), method_name.__colorize__(:method_name), ('(maximal tracing)' if max)]

  puts "  %s %s:%s" % ['source_location:'.__colorize__(:source), source_path.dup.__colorize__(:source_path), source_number.to_s.__colorize__(:source_number)] if source_path && source_number

  puts "  %s %s" % ['called from class:'.__colorize__(:called_from), class_name.__colorize__(:class_name)]

  # Local Variables
  lvb = eval('local_variables',_binding)
  puts "  %s %s" % ['local_variables:'.__colorize__(:lv), ("[]" if lvb.empty?)]

  lvb.map do |lv|
    local_variable = eval(lv.to_s, _binding)
    local_variable = (max ? local_variable.inspect : local_variable.__real_to_s__)

    puts "    %s: %s" % [lv.__colorize__(:lv_names), local_variable.__colorize__(:lv_values)]
  end if lvb

  # Instance Variables
  begin
    ivb = eval('instance_variables',_binding)

    puts "  %s %s" % ["instance_variables:".__colorize__(:iv), ("[]" if ivb.empty?)]

    ivb.map do |iv|
      instance_variable = eval(iv.to_s, _binding)
      instance_variable = (max ? instance_variable.inspect : instance_variable.__real_to_s__)

      puts "    %s: %s" % [iv.__colorize__(:iv_names), instance_variable.__colorize__(:iv_values)]
    end if ivb
  end if iv

  # Self inspection
  begin
    puts "  self inspection:".__colorize__(:self_inspection)
    puts "  %s" % self_inspection.__colorize__(:self_inspection_trace)
  end if self_inspection

  # Caller methods chain
  begin
    puts "  caller methods: ".__colorize__(:caller_methods)
    caller.each do |meth|
      puts "  %s" % meth.__colorize__(:caller_method)
    end
  end if _caller

  puts "\n"

  # Yield mysterious things if they exist in block.
  yield if block_given?
end

#line(sp = "") ⇒ Object



10
11
12
# File 'lib/cutter/colored_outputs.rb', line 10

def line sp = ""
  log_coloured sp, "------------------------------", color(:line)
end

#lll(object = nil) ⇒ Object



126
127
128
# File 'lib/cutter/inspection.rb', line 126

def lll object = nil
  Rails.logger.info object.inspect
end

#log_coloured(sp, msg, color = :default) ⇒ Object



14
15
16
17
18
# File 'lib/cutter/colored_outputs.rb', line 14

def log_coloured sp, msg, color = :default
  message = sp + msg
  message = color != :default ? message.send(color) : message
  puts message
end

#ppp(object = nil) ⇒ Object



122
123
124
# File 'lib/cutter/inspection.rb', line 122

def ppp object = nil
  puts object.inspect
end

#rrr(object = nil) ⇒ Object



118
119
120
# File 'lib/cutter/inspection.rb', line 118

def rrr object = nil
  raise object.inspect
end

#stamper(*args, &block) ⇒ Object



8
9
10
11
12
13
14
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cutter/stamper.rb', line 8

def stamper *args, &block
  return if stamper_class.off?

  options = args.extract_options!

  name = args.first

  capture = options.delete :capture


  scope = stamper_class[name] || stamper_class[:default]
  scope.indent = stamper_class.last ? stamper_class.last.indent + 1 : 0
  stamper_class.push scope

  msg = 'no msg'
  if scope
    message = scope.label.values.first
  end

  print "\n"

  spaces = "    " * scope.indent

  log_coloured spaces, "#{message}", __color__(:message_name)
  log_coloured spaces, "#{'-'*message.length}", __color__(:message_line)

  scope.time_initial = Time.now

  (class << self; self end).send :define_method, :stamp do |*args|
    scope.stamp args.first
  end

  (class << self; self end).send :define_method, :stamp! do |*args|
    scope.stamp args.first
  end

  capture ? capture_stdout { yield scope } : yield(scope)

  (class << self; self end).send :remove_method, :stamp if respond_to? :stamp
  (class << self; self end).send :remove_method, :stamp! if respond_to? :stamp!

  scope.indent -= 1 if scope.indent > 0
  stamper_class.pop

  time_passed = Time.now.ms_since scope.time_initial

  tps = "#{time_passed}ms"
  offset = message.length - tps.length
  offset = 0 if offset < 0
  log_coloured spaces, "#{'-'*message.length}", __color__(:total_line)
  log_coloured spaces + "#{' ' * (offset)}", tps, __color__(:total_count)
  print "\n"

  tps
end