Module: Letters::CoreExt

Defined in:
lib/letters/core_ext.rb

Constant Summary collapse

DELIM =
"-" * 20

Instance Method Summary collapse

Instance Method Details

#a(opts = {}, &block) ⇒ Object

Assert



30
31
32
33
34
35
36
# File 'lib/letters/core_ext.rb', line 30

def a(opts={}, &block)
  ubertap(:a, opts, caller) do |o, full_opts|
    if block_given? && !o.instance_eval(&block)
      raise full_opts[:error_class]
    end
  end
end

#b(opts = {}) ⇒ Object

Beep



39
40
41
42
43
# File 'lib/letters/core_ext.rb', line 39

def b(opts={})
  ubertap(:b, opts, caller) do |_, _|
    $stdout.print "\a"
  end
end

#c(opts = {}) ⇒ Object

Callstack



46
47
48
49
50
# File 'lib/letters/core_ext.rb', line 46

def c(opts={})
  ubertap(:b, opts, caller) do |_, full_opts|
    Helpers.out caller(4), full_opts
  end
end

#d(opts = {}) ⇒ Object

Debug



53
54
55
56
57
# File 'lib/letters/core_ext.rb', line 53

def d(opts={})
  ubertap(:d, opts, caller) do |_, _|
    Helpers.call_debugger
  end
end

#d1(opts = {}) ⇒ Object

Diff 1



60
61
62
63
64
# File 'lib/letters/core_ext.rb', line 60

def d1(opts={})
  ubertap(:d1, opts, caller) do |o, _|
    Letters.object_for_diff = o
  end
end

#d2(opts = {}) ⇒ Object

Diff 2



67
68
69
70
71
72
73
# File 'lib/letters/core_ext.rb', line 67

def d2(opts={})
  ubertap(:d2, opts, caller) do |o, full_opts|
    diff = Helpers.diff(Letters.object_for_diff, o)
    Helpers.out diff, full_opts
    Letters.object_for_diff = nil
  end
end

#e(opts = {}) ⇒ Object

Empty check



76
77
78
79
80
81
# File 'lib/letters/core_ext.rb', line 76

def e(opts={})
  opts.merge! :error_class => EmptyError
  ubertap(:e, opts, caller) do |o, full_opts|
    o.a(full_opts) { !empty? }
  end
end

#f(opts = {}) ⇒ Object

File



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/letters/core_ext.rb', line 84

def f(opts={})
  ubertap(:f, opts, caller) do |o, full_opts|
    suffixes = [""] + (1..50).to_a
    deduper = suffixes.detect {|x| !File.directory? "#{full_opts[:name]}#{x}" }

    File.open("#{full_opts[:name]}#{deduper}", "w+") do |file|
      # Override :stream
      full_opts.merge! :stream => file
      Helpers.out o, full_opts
    end
  end
end

#j(opts = {}, &block) ⇒ Object

Jump



98
99
100
101
102
# File 'lib/letters/core_ext.rb', line 98

def j(opts={}, &block)
  ubertap(:j, opts, caller) do |o, full_opts|
    o.instance_eval &block
  end
end

#k(opts = {}) ⇒ Object

Kill



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/letters/core_ext.rb', line 105

def k(opts={})
  opts.merge! :error_class => KillError
  ubertap(:k, opts, caller) do |o, full_opts|
    # Support :max option until I can deprecate it
    full_opts[:on] ||= full_opts[:max]

    Letters.kill_count ||= 0

    if Letters.kill_count >= full_opts[:on]
      Letters.kill_count = 0
      o.a(full_opts) { false }
    end

    Letters.kill_count += 1
  end
end

#l(opts = {}) ⇒ Object

Log



123
124
125
126
127
128
129
130
131
132
# File 'lib/letters/core_ext.rb', line 123

def l(opts={})
  ubertap(:l, opts, caller) do |o, full_opts|
    begin
      logger.send(full_opts[:level], full_opts[:message]) if full_opts[:message]
      logger.send(full_opts[:level], Helpers.send(full_opts[:format], o))
    rescue
      $stdout.puts "[warning] No logger available"
    end
  end
end

#m(taint = true, opts = {}) ⇒ Object

Taint and untaint object



135
136
137
138
139
140
141
142
143
# File 'lib/letters/core_ext.rb', line 135

def m(taint=true, opts={})
  ubertap(:m, opts, caller) do |o, _|
    if taint
      o.taint
    else
      o.untaint
    end
  end
end

#n(opts = {}) ⇒ Object

Nil check



146
147
148
149
150
151
# File 'lib/letters/core_ext.rb', line 146

def n(opts={})
  opts.merge! :error_class => NilError
  ubertap(:n, opts, caller) do |o, full_opts|
    o.a(full_opts) { !nil? }
  end
end

#o(opts = {}, &block) ⇒ Object

Print to STDOUT



154
155
156
157
158
159
160
# File 'lib/letters/core_ext.rb', line 154

def o(opts={}, &block)
  ubertap(:o, opts, caller) do |o, full_opts|
    Helpers.message full_opts
    obj = block_given? ? o.instance_eval(&block) : o
    Helpers.out obj, full_opts
  end
end

#r(method = nil, opts = {}) ⇒ Object

RI



163
164
165
166
167
168
# File 'lib/letters/core_ext.rb', line 163

def r(method=nil, opts={})
  ubertap(:r, opts, caller) do |o, _|
    method_or_empty = method ? "##{method}" : method
    system "ri #{o.class}#{method_or_empty}"
  end
end

#s(level = nil, opts = {}) ⇒ Object

Change safety level



171
172
173
174
175
176
# File 'lib/letters/core_ext.rb', line 171

def s(level=nil, opts={})
  ubertap(:s, opts) do |_, _|
    level ||= $SAFE + 1
    Helpers.change_safety level
  end
end

#t(opts = {}) ⇒ Object

Timestamp



179
180
181
182
183
# File 'lib/letters/core_ext.rb', line 179

def t(opts={})
  ubertap(:t, opts) do |_, full_opts|
    Helpers.out Time.now.to_s(full_opts[:time_format].to_sym), full_opts
  end
end

#ubertap(letter, opts = {}, orig_caller = [], &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/letters/core_ext.rb', line 16

def ubertap(letter, opts={}, orig_caller=[], &block)
  full_opts = Letters.defaults_with(letter, opts)

  unless full_opts[:disable]
    Helpers.message full_opts
    Helpers.print_line(orig_caller[0]) if full_opts[:line_no]

    tap do |o|
      block.call(o, full_opts)
    end
  end
end