Permalink
Browse files

replace 'tap' with 'ubertap' to unify methods -- then add in line_no …

…reporting
  • Loading branch information...
1 parent e4c90d4 commit 570cfd43e0c846fdf575438b169a3073ef42a599 @davejacobs committed Jul 11, 2013
Showing with 84 additions and 63 deletions.
  1. +59 −61 lib/letters/core_ext.rb
  2. +20 −2 lib/letters/helpers.rb
  3. +5 −0 spec/letters/core_ext_spec.rb
View
120 lib/letters/core_ext.rb
@@ -7,107 +7,109 @@
require "letters/kill_error"
require "letters/nil_error"
+require "colorize"
+
module Letters
module CoreExt
DELIM = "-" * 20
+ 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
+
# Assert
def a(opts={}, &block)
- opts = Letters.defaults_with(:a, opts)
- tap do |o|
- Helpers.message opts
+ ubertap(:a, opts, caller) do |o, full_opts|
if block_given? && !o.instance_eval(&block)
- raise opts[:error_class]
+ raise full_opts[:error_class]
end
end
end
# Beep
- def b
- tap do
+ def b(opts={})
+ ubertap(:b, opts, caller) do |_, _|
$stdout.print "\a"
end
end
# Callstack
def c(opts={})
- tap do
- Helpers.message opts
- Helpers.out caller(4), opts
+ ubertap(:b, opts, caller) do |_, full_opts|
+ Helpers.out caller(4), full_opts
end
end
# Debug
- def d
- tap do
- Helpers.call_debugger
+ def d(opts={})
+ ubertap(:d, opts, caller) do |_, _|
+ Helpers.call_debugger
end
end
# Diff 1
- def d1
- tap do |o|
+ def d1(opts={})
+ ubertap(:d1, opts, caller) do |o, _|
Letters.object_for_diff = o
end
end
# Diff 2
def d2(opts={})
- opts = Letters.defaults_with(:d2, opts)
- tap do |o|
+ ubertap(:d2, opts, caller) do |o, full_opts|
diff = Helpers.diff(Letters.object_for_diff, o)
- Helpers.message opts
- Helpers.out diff, opts
+ Helpers.out diff, full_opts
Letters.object_for_diff = nil
end
end
-
+
# Empty check
def e(opts={})
opts.merge! :error_class => EmptyError
- tap do |o|
- Helpers.message opts
- o.a(opts) { !empty? }
+ ubertap(:e, opts, caller) do |o, full_opts|
+ o.a(full_opts) { !empty? }
end
end
# File
def f(opts={})
- opts = Letters.defaults_with(:f, opts)
- tap do |o|
+ ubertap(:f, opts, caller) do |o, full_opts|
suffixes = [""] + (1..50).to_a
- deduper = suffixes.detect {|x| !File.directory? "#{opts[:name]}#{x}" }
+ deduper = suffixes.detect {|x| !File.directory? "#{full_opts[:name]}#{x}" }
- File.open("#{opts[:name]}#{deduper}", "w+") do |file|
+ File.open("#{full_opts[:name]}#{deduper}", "w+") do |file|
# Override :stream
- opts.merge! :stream => file
- Helpers.message opts
- Helpers.out o, opts
+ full_opts.merge! :stream => file
+ Helpers.out o, full_opts
end
end
end
# Jump
- def j(&block)
- tap do |o|
+ def j(opts={}, &block)
+ ubertap(:j, opts, caller) do |o, full_opts|
o.instance_eval &block
end
end
# Kill
def k(opts={})
- opts = Letters.defaults_with(:k, opts)
-
- # Support :max option until I can deprecate it
- opts[:on] ||= opts[:max]
-
opts.merge! :error_class => KillError
- tap do |o|
+ 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 >= opts[:on]
+ if Letters.kill_count >= full_opts[:on]
Letters.kill_count = 0
- o.a(opts) { false }
+ o.a(full_opts) { false }
end
Letters.kill_count += 1
@@ -116,20 +118,19 @@ def k(opts={})
# Log
def l(opts={})
- opts = Letters.defaults_with(:l, opts)
- tap do |o|
+ ubertap(:l, opts, caller) do |o, full_opts|
begin
- logger.send(opts[:level], opts[:message]) if opts[:message]
- logger.send(opts[:level], Helpers.send(opts[:format], o))
+ 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
# Taint and untaint object
- def m(taint=true)
- tap do |o|
+ def m(taint=true, opts={})
+ ubertap(:m, opts, caller) do |o, _|
if taint
o.taint
else
@@ -141,43 +142,40 @@ def m(taint=true)
# Nil check
def n(opts={})
opts.merge! :error_class => NilError
- tap do |o|
- o.a(opts) { !nil? }
+ ubertap(:n, opts, caller) do |o, full_opts|
+ o.a(full_opts) { !nil? }
end
end
# Print to STDOUT
def o(opts={}, &block)
- opts = Letters.defaults_with(:o, opts)
- tap do |o|
- Helpers.message opts
- obj = block_given? ? o.instance_eval(&block) : o
- Helpers.out obj, opts
+ 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
# RI
- def r(method=nil)
- tap do |o|
+ def r(method=nil, opts={})
+ ubertap(:r, opts, caller) do |o, _|
method_or_empty = method ? "##{method}" : method
- system "ri #{o.class}#{method_or_empty}"
+ system "ri #{o.class}#{method_or_empty}"
end
end
# Change safety level
- def s(level=nil)
- tap do
+ def s(level=nil, opts={})
+ ubertap(:s, opts) do |_, _|
level ||= $SAFE + 1
Helpers.change_safety level
end
end
# Timestamp
def t(opts={})
- opts = Letters.defaults_with(:t, opts)
- tap do
- Helpers.message opts
- Helpers.out Time.now.to_s(opts[:time_format].to_sym), opts
+ ubertap(:t, opts) do |_, full_opts|
+ Helpers.out Time.now.to_s(full_opts[:time_format].to_sym), full_opts
end
end
end
View
22 lib/letters/helpers.rb
@@ -1,4 +1,5 @@
require "colorize"
+require "pathname"
module Letters
module Helpers
@@ -13,13 +14,13 @@ def self.diff(obj1, obj2)
when String
diff(obj1.split("\n"), obj2.split("\n"))
else
- {
+ {
removed: Array(obj1 - obj2),
added: Array(obj2 - obj1)
}
end
rescue
- raise "cannot diff the two marked objects"
+ raise "cannot diff the two marked objects"
end
def self.message(opts={})
@@ -60,6 +61,23 @@ def self.yaml(object)
object.to_yaml
end
+ def self.print_line(caller_line)
+ file, line_no = caller_line.split.first.sub(/:in$/, "").split(":")
+
+ line = if File.exist?(file) && File.file?(file)
+ File.readlines(file)[Integer(line_no) - 1]
+ else
+ ""
+ end
+
+ rel_file = Pathname.new(file).expand_path.relative_path_from(Pathname.getwd)
+
+ puts "Letters call at #{file}, line #{line_no}".underline
+ puts
+ puts " " + line.strip.chomp.sub(/^\W*/, "").green
+ puts
+ end
+
def self.pretty_callstack(callstack)
home = ENV["MY_RUBY_HOME"]
View
5 spec/letters/core_ext_spec.rb
@@ -35,6 +35,11 @@ module Letters
hash.o { nil }.should == hash
end
+ it "all letter methods have the option of outputting the line where they are called" do
+ pending "I need to figure out a good way to test this"
+ # hash.j(:line_no => true) { nil }.should == hash
+ end
+
describe "#a (assert)" do
it "jumps into the receiver's calling context" do
lambda do

0 comments on commit 570cfd4

Please sign in to comment.