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

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

#inspect! may be called inside any method as ‘inspect! {}’ or more rigorous: ‘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 (meth = method(method_name.to_sym)).respond_to? :source_location
    source_path, source_number = meth.source_location
  end

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

  puts "  %s %s:%s" % ['source:'.to_colorized_string(:source), source_path.dup.to_colorized_string(:source_path), source_number.to_s.to_colorized_string(:source_number)] if source_path && source_number
 
  puts "  %s %s" % ['called from class:'.to_colorized_string(:called_from), class_name.to_colorized_string(:class_name)]

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

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

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

  # Instance Variables
  begin
    ivb = eval('instance_variables',_binding)
    
    puts "  %s %s" % ["instance_variables:".to_colorized_string(:iv), ("[]" if ivb.empty?)]
    
    ivb.map do |iv|
      instance_variable = eval(iv.to_s, _binding)
      instance_variable = (max ? instance_variable.inspect : instance_variable.to_real_string)
     
      puts "    %s: %s" % [iv.to_colorized_string(:iv_names), instance_variable.to_colorized_string(:iv_values)] 
    end if ivb
  end if iv

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

  # Caller methods chain
  begin
    puts "  caller methods: ".to_colorized_string(:caller_methods)
    caller.each do |meth|
      puts "  %s" % meth.to_colorized_string(:caller_method)
    end
  end if _caller
 
  puts "\n"
  
  # Yield mysterious things if they exist in block.
  yield if block_given?
end

#line(sp = "") ⇒ Object



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

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

#lll(object = nil) ⇒ Object



128
129
130
# File 'lib/cutter/inspection.rb', line 128

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

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



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

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

#ppp(object = nil) ⇒ Object



124
125
126
# File 'lib/cutter/inspection.rb', line 124

def ppp object = nil 
  puts object.inspect
end

#rrr(object = nil) ⇒ Object



120
121
122
# File 'lib/cutter/inspection.rb', line 120

def rrr object = nil
  raise object.inspect
end

#stamper(name = nil) {|scope| ... } ⇒ Object

Yields:

  • (scope)


6
7
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
# File 'lib/cutter/stamper.rb', line 6

def stamper name = nil, &block

  return if stamper_class.off?
  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
  spaces = "    " * scope.indent
  puts "\n"
  log_coloured spaces, "#{message}", color(:message_name)
  log_coloured spaces, "#{'-'*message.length}", color(:message_line)

  scope.time_initial = time_now
  yield scope
  scope.indent -= 1 if scope.indent > 0
  stamper_class.pop
  time_passed = time_now - 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)
  puts "\n"
end

#time_nowObject



2
3
4
# File 'lib/cutter/stamper.rb', line 2

def time_now
  Time.now.strftime("%s%L").to_i
end