Module: Letters::Helpers
- Defined in:
- lib/letters/helpers.rb
Class Method Summary collapse
- .ap(object) ⇒ Object
-
.call_debugger ⇒ Object
This provides a mockable method for testing.
- .change_safety(safety) ⇒ Object
- .diff(obj1, obj2) ⇒ Object
- .json(object) ⇒ Object
- .message(opts = {}) ⇒ Object
- .out(object, opts = {}) ⇒ Object
- .pp(object) ⇒ Object
- .pretty_callstack(callstack) ⇒ Object
- .print_line(caller_line) ⇒ Object
- .string(object) ⇒ Object
- .xml(object) ⇒ Object
- .yaml(object) ⇒ Object
Class Method Details
.ap(object) ⇒ Object
35 36 37 38 |
# File 'lib/letters/helpers.rb', line 35 def self.ap(object) require "awesome_print" object.awesome_inspect end |
.call_debugger ⇒ Object
This provides a mockable method for testing
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/letters/helpers.rb', line 118 def self.call_debugger if (defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx') require 'rubinius/debugger' Rubinius::Debugger.start else require 'debug' end nil end |
.change_safety(safety) ⇒ Object
129 130 131 |
# File 'lib/letters/helpers.rb', line 129 def self.change_safety(safety) $SAFE = safety end |
.diff(obj1, obj2) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/letters/helpers.rb', line 6 def self.diff(obj1, obj2) case obj2 when Hash { removed: obj1.reject {|k, v| obj2.include? k }, added: obj2.reject {|k, v| obj1.include? k }, updated: obj2.select {|k, v| obj1.include?(k) && obj1[k] != v } } 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" end |
.json(object) ⇒ Object
40 41 42 43 |
# File 'lib/letters/helpers.rb', line 40 def self.json(object) require "json" object.to_json end |
.message(opts = {}) ⇒ Object
26 27 28 |
# File 'lib/letters/helpers.rb', line 26 def self.(opts={}) out(opts[:message], opts) if opts[:message] end |
.out(object, opts = {}) ⇒ Object
30 31 32 33 |
# File 'lib/letters/helpers.rb', line 30 def self.out(object, opts={}) opts = { stream: $stdout, format: "string" }.merge opts opts[:stream].puts Helpers.send(opts[:format], object) end |
.pp(object) ⇒ Object
45 46 47 48 |
# File 'lib/letters/helpers.rb', line 45 def self.pp(object) require "pp" object.pretty_inspect end |
.pretty_callstack(callstack) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/letters/helpers.rb', line 81 def self.pretty_callstack(callstack) home = ENV["MY_RUBY_HOME"] parsed = callstack.map do |entry| line, line_no, method_name = entry.split ":" { line: line.gsub(home + "/", "").green, line_no: line_no.yellow, method_name: method_name.scan(/`([^\']+)'/).first.first.light_blue } end headers = { line: "Line".green, line_no: "No.".yellow, method_name: "Method".light_blue } parsed.unshift headers longest_line = parsed.map {|entry| entry[:line] }. sort_by(&:length). last longest_method = parsed.map {|entry| entry[:method_name] }. sort_by(&:length). last formatter = "%#{longest_method.length}{method_name} %-#{longest_line.length}{line} %{line_no}\n" parsed.map {|h| formatter % h }.join end |
.print_line(caller_line) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/letters/helpers.rb', line 64 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)..relative_path_from(Pathname.getwd) puts "Letters call at #{file}, line #{line_no}".underline puts puts " " + line.strip.chomp.sub(/^\W*/, "").green puts end |
.string(object) ⇒ Object
50 51 52 |
# File 'lib/letters/helpers.rb', line 50 def self.string(object) object.to_s end |
.xml(object) ⇒ Object
54 55 56 57 |
# File 'lib/letters/helpers.rb', line 54 def self.xml(object) require "xmlsimple" XmlSimple.xml_out(object, { "KeepRoot" => true }) end |
.yaml(object) ⇒ Object
59 60 61 62 |
# File 'lib/letters/helpers.rb', line 59 def self.yaml(object) require "yaml" object.to_yaml end |