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



27
28
29
30
31
32
33
# File 'lib/letters/core_ext.rb', line 27

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



36
37
38
39
40
# File 'lib/letters/core_ext.rb', line 36

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

#c(opts = {}) ⇒ Object

Callstack



43
44
45
46
47
# File 'lib/letters/core_ext.rb', line 43

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

#d(opts = {}) ⇒ Object

Debug



50
51
52
53
54
# File 'lib/letters/core_ext.rb', line 50

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

#d1(opts = {}) ⇒ Object

Diff 1



57
58
59
60
61
# File 'lib/letters/core_ext.rb', line 57

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

#d2(opts = {}) ⇒ Object

Diff 2



64
65
66
67
68
69
70
# File 'lib/letters/core_ext.rb', line 64

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



73
74
75
76
77
78
# File 'lib/letters/core_ext.rb', line 73

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



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/letters/core_ext.rb', line 81

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



95
96
97
98
99
# File 'lib/letters/core_ext.rb', line 95

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

#k(opts = {}) ⇒ Object

Kill



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

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



120
121
122
123
124
125
126
127
128
129
# File 'lib/letters/core_ext.rb', line 120

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



132
133
134
135
136
137
138
139
140
# File 'lib/letters/core_ext.rb', line 132

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



143
144
145
146
147
148
# File 'lib/letters/core_ext.rb', line 143

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



151
152
153
154
155
156
157
# File 'lib/letters/core_ext.rb', line 151

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



160
161
162
163
164
165
# File 'lib/letters/core_ext.rb', line 160

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



168
169
170
171
172
173
# File 'lib/letters/core_ext.rb', line 168

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

#t(opts = {}) ⇒ Object

Timestamp



176
177
178
179
180
# File 'lib/letters/core_ext.rb', line 176

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
# File 'lib/letters/core_ext.rb', line 16

def ubertap(letter, opts={}, orig_caller=[], &block)
  full_opts = Letters.defaults_with(letter, opts)
  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