Module: Tattletail

Included in:
Object, Merge
Defined in:
lib/tattletail.rb,
lib/tattletail/demo.rb,
lib/tattletail/version.rb

Defined Under Namespace

Modules: Merge Classes: Fib

Constant Summary collapse

COLORS =
{ :count => '00a0b0',
:file => '444',
:context => '999',
:exception => 'f00',
:self => '793A57',
:method => 'fff',
:args => 'EB6841',
:result => '2DE04D',
:result_yaml => '105023',
:time_good => 'EDC951',
:time_bad => 'CC333F' }
VERSION =
"0.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cont_indent_strObject



90
91
92
# File 'lib/tattletail.rb', line 90

def self.cont_indent_str
  "#{indent_str}"
end

.context_str(method_caller) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/tattletail.rb', line 66

def self.context_str method_caller
  file_name, line_number, context = method_caller[0].split(':', 3)
  begin
    actual_line = File.readlines(file_name)[line_number.to_i - 1].strip
  rescue Exception => e
    actual_line = "Unable to open #{file_name}:#{line_number}"
  end
  "#{context} ... #{actual_line}".color(COLORS[:context])
end

.countObject



41
42
43
# File 'lib/tattletail.rb', line 41

def self.count
  $_tma_count ||= 0
end

.count_strObject



53
54
55
# File 'lib/tattletail.rb', line 53

def self.count_str
  " ##{count.to_s} ".color(COLORS[:count]).bold
end

.end_indent_strObject



94
95
96
# File 'lib/tattletail.rb', line 94

def self.end_indent_str
  "#{indent_str} └─"
end

.exception_str(exception, indent_str) ⇒ Object



113
114
115
# File 'lib/tattletail.rb', line 113

def self.exception_str exception, indent_str
  "#{exception.class.name}: #{exception.message.to_s}".color(COLORS[:exception]).bold.underline
end

.file_str(method_caller) ⇒ Object



61
62
63
64
# File 'lib/tattletail.rb', line 61

def self.file_str method_caller
  file_name, line_number = method_caller[0].split(':', 3)
  "#{file_name}:#{line_number}".color(COLORS[:file])
end

.incObject



25
26
27
# File 'lib/tattletail.rb', line 25

def self.inc
  $_tma_count += 1
end

.indentObject



29
30
31
# File 'lib/tattletail.rb', line 29

def self.indent
  $_tma_indent += 1
end

.indent_changed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/tattletail.rb', line 49

def self.indent_changed?
  $_tma_indent < $_tma_last_indent
end

.indent_levelObject



45
46
47
# File 'lib/tattletail.rb', line 45

def self.indent_level
  $_tma_indent ||= 0
end

.indent_strObject



82
83
84
# File 'lib/tattletail.rb', line 82

def self.indent_str
  indent_level.times.inject('') {|m| m += ""}
end

.method_str(method_name, *args) ⇒ Object



76
77
78
79
80
# File 'lib/tattletail.rb', line 76

def self.method_str method_name, *args
  name_str = "#{method_name}".bold
  args_str = "(#{args.map {|a| a.inspect.size > 150 ? a.to_s : a.inspect}.map {|a| a.color(COLORS[:args])}.join(', ')})"
  name_str + args_str
end

.outdentObject



33
34
35
# File 'lib/tattletail.rb', line 33

def self.outdent
  $_tma_indent -= 1
end

.remember_indentObject



37
38
39
# File 'lib/tattletail.rb', line 37

def self.remember_indent
  $_tma_last_indent = $_tma_indent
end

.resetObject



19
20
21
22
23
# File 'lib/tattletail.rb', line 19

def self.reset
  $_tma_count = 0
  $_tma_indent = 0
  $_tma_last_indent = 0
end

.result_str(result, indent_str) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/tattletail.rb', line 103

def self.result_str result, indent_str
  result.to_s.color(COLORS[:result]) + if result.instance_variables.any? && ENV['SHOW_YAML']
                        "\n" + indent_str + "         " + result.to_yaml.each_line.map do |l|
                          l.gsub(/\n/,'').color(COLORS[:result_yaml])
                        end.join("\n").gsub(/\n/, "\n#{indent_str}           ")
                      else
                        ''
                      end
end

.self_str(passed_self) ⇒ Object



57
58
59
# File 'lib/tattletail.rb', line 57

def self.self_str passed_self
  passed_self.to_s.color(COLORS[:self])
end

.start_indent_strObject



86
87
88
# File 'lib/tattletail.rb', line 86

def self.start_indent_str
  indent_str.reverse.sub("", "─├").reverse + "─┬─"
end

.time_str(seconds) ⇒ Object



98
99
100
101
# File 'lib/tattletail.rb', line 98

def self.time_str seconds
  time_str = (' ' * count.to_s.size) + ("%.4f sec" % seconds)
  seconds > 0.05 ? time_str.color(COLORS[:time_bad]) : time_str.color(COLORS[:time_good])
end

Instance Method Details

#tattle_on(*method_names) ⇒ Object Also known as: tell_on, tell_me_about



117
118
119
120
121
122
123
124
125
# File 'lib/tattletail.rb', line 117

def tattle_on(*method_names)
  method_names.each do |method_name|
    if class_method? method_name
      tattle_on_class_method method_name
    else
      tattle_on_instance_method method_name
    end
  end
end

#tattle_on_class_method(*method_names) ⇒ Object Also known as: tattle_on_class_methods, tell_on_class_methods, tell_on_class_method, tell_me_about_class_methods, tell_me_about_class_method



129
130
131
132
133
134
135
136
137
# File 'lib/tattletail.rb', line 129

def tattle_on_class_method(*method_names)
  method_names.each do |method_name|
    class_eval <<-EOS, __FILE__, __LINE__ + 1
      class << self
        #{make_talk method_name}
      end
    EOS
  end
end

#tattle_on_instance_method(*method_names) ⇒ Object Also known as: tattle_on_instance_methods, tell_on_instance_methods, tell_on_instance_method, tell_me_about_instance_methods, tell_me_about_instance_method



144
145
146
147
148
149
150
# File 'lib/tattletail.rb', line 144

def tattle_on_instance_method(*method_names)
  method_names.each do |method_name|
    class_eval <<-EOS, __FILE__, __LINE__ + 1
      #{make_talk method_name}
    EOS
  end
end