Module: Letters::CoreExt
- Defined in:
- lib/letters/core_ext.rb
Constant Summary collapse
- DELIM =
"-" * 20
Instance Method Summary collapse
-
#a(opts = {}, &block) ⇒ Object
Assert.
-
#b ⇒ Object
Beep.
-
#c(opts = {}) ⇒ Object
Callstack.
-
#d ⇒ Object
Debug.
-
#d1 ⇒ Object
Diff 1.
-
#d2(opts = {}) ⇒ Object
Diff 2.
-
#e(opts = {}) ⇒ Object
Empty check.
-
#f(opts = {}) ⇒ Object
File.
-
#j(&block) ⇒ Object
Jump.
-
#k(opts = {}) ⇒ Object
Kill.
-
#l(opts = {}) ⇒ Object
Log.
-
#m(taint = true) ⇒ Object
Taint and untaint object.
-
#n(opts = {}) ⇒ Object
Nil check.
-
#o(opts = {}, &block) ⇒ Object
Print to STDOUT.
-
#r(method = nil) ⇒ Object
RI.
-
#s(level = nil) ⇒ Object
Change safety level.
-
#t(opts = {}) ⇒ Object
Timestamp.
Instance Method Details
#a(opts = {}, &block) ⇒ Object
Assert
14 15 16 17 18 19 20 21 22 |
# File 'lib/letters/core_ext.rb', line 14 def a(opts={}, &block) opts = Letters.defaults_with(:a, opts) tap do |o| Helpers. opts if block_given? && !o.instance_eval(&block) raise opts[:error_class] end end end |
#b ⇒ Object
Beep
25 26 27 28 29 |
# File 'lib/letters/core_ext.rb', line 25 def b tap do $stdout.print "\a" end end |
#c(opts = {}) ⇒ Object
Callstack
32 33 34 35 36 37 |
# File 'lib/letters/core_ext.rb', line 32 def c(opts={}) tap do Helpers. opts Helpers.out caller(4), opts end end |
#d ⇒ Object
Debug
40 41 42 43 44 |
# File 'lib/letters/core_ext.rb', line 40 def d tap do Helpers.call_debugger end end |
#d1 ⇒ Object
Diff 1
47 48 49 50 51 |
# File 'lib/letters/core_ext.rb', line 47 def d1 tap do |o| Letters.object_for_diff = o end end |
#d2(opts = {}) ⇒ Object
Diff 2
54 55 56 57 58 59 60 61 62 |
# File 'lib/letters/core_ext.rb', line 54 def d2(opts={}) opts = Letters.defaults_with(:d2, opts) tap do |o| diff = Helpers.diff(Letters.object_for_diff, o) Helpers. opts Helpers.out diff, opts Letters.object_for_diff = nil end end |
#e(opts = {}) ⇒ Object
Empty check
65 66 67 68 69 70 71 |
# File 'lib/letters/core_ext.rb', line 65 def e(opts={}) opts.merge! :error_class => EmptyError tap do |o| Helpers. opts o.a(opts) { !empty? } end end |
#f(opts = {}) ⇒ Object
File
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/letters/core_ext.rb', line 74 def f(opts={}) opts = Letters.defaults_with(:f, opts) tap do |o| suffixes = [""] + (1..50).to_a deduper = suffixes.detect {|x| !File.directory? "#{opts[:name]}#{x}" } File.open("#{opts[:name]}#{deduper}", "w+") do |file| # Override :stream opts.merge! :stream => file Helpers. opts Helpers.out o, opts end end end |
#j(&block) ⇒ Object
Jump
90 91 92 93 94 |
# File 'lib/letters/core_ext.rb', line 90 def j(&block) tap do |o| o.instance_eval &block end end |
#k(opts = {}) ⇒ Object
Kill
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/letters/core_ext.rb', line 97 def k(opts={}) opts = Letters.defaults_with(:k, opts) opts.merge! :error_class => KillError tap do |o| @letters_kill_count ||= 0 if @letters_kill_count >= opts[:max] raise opts[:error_class] end @letters_kill_count += 1 end end |
#l(opts = {}) ⇒ Object
Log
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/letters/core_ext.rb', line 110 def l(opts={}) opts = Letters.defaults_with(:l, opts) tap do |o| begin logger.send(opts[:level], opts[:message]) if opts[:message] logger.send(opts[:level], Helpers.send(opts[:format], o)) rescue $stdout.puts "[warning] No logger available" end end end |
#m(taint = true) ⇒ Object
Taint and untaint object
123 124 125 126 127 128 129 130 131 |
# File 'lib/letters/core_ext.rb', line 123 def m(taint=true) tap do |o| if taint o.taint else o.untaint end end end |
#n(opts = {}) ⇒ Object
Nil check
134 135 136 137 138 139 |
# File 'lib/letters/core_ext.rb', line 134 def n(opts={}) opts.merge! :error_class => NilError tap do |o| o.a(opts) { !nil? } end end |
#o(opts = {}, &block) ⇒ Object
Print to STDOUT
142 143 144 145 146 147 148 149 |
# File 'lib/letters/core_ext.rb', line 142 def o(opts={}, &block) opts = Letters.defaults_with(:o, opts) tap do |o| Helpers. opts obj = block_given? ? o.instance_eval(&block) : o Helpers.out obj, opts end end |
#r(method = nil) ⇒ Object
RI
152 153 154 155 156 157 |
# File 'lib/letters/core_ext.rb', line 152 def r(method=nil) tap do |o| method_or_empty = method ? "##{method}" : method system "ri #{o.class}#{method_or_empty}" end end |
#s(level = nil) ⇒ Object
Change safety level
160 161 162 163 164 165 |
# File 'lib/letters/core_ext.rb', line 160 def s(level=nil) tap do level ||= $SAFE + 1 Helpers.change_safety level end end |
#t(opts = {}) ⇒ Object
Timestamp
168 169 170 171 172 173 174 |
# File 'lib/letters/core_ext.rb', line 168 def t(opts={}) opts = Letters.defaults_with(:t, opts) tap do Helpers. opts Helpers.out Time.now.to_s(opts[:time_format].to_sym), opts end end |