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
- .string(object) ⇒ Object
- .xml(object) ⇒ Object
- .yaml(object) ⇒ Object
Class Method Details
.ap(object) ⇒ Object
34 35 36 37 |
# File 'lib/letters/helpers.rb', line 34 def self.ap(object) require "awesome_print" object.awesome_inspect end |
.call_debugger ⇒ Object
This provides a mockable method for testing
100 101 102 103 104 |
# File 'lib/letters/helpers.rb', line 100 def self.call_debugger require "ruby-debug" debugger nil end |
.change_safety(safety) ⇒ Object
106 107 108 |
# File 'lib/letters/helpers.rb', line 106 def self.change_safety(safety) $SAFE = safety end |
.diff(obj1, obj2) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/letters/helpers.rb', line 5 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
39 40 41 42 |
# File 'lib/letters/helpers.rb', line 39 def self.json(object) require "json" object.to_json end |
.message(opts = {}) ⇒ Object
25 26 27 |
# File 'lib/letters/helpers.rb', line 25 def self.(opts={}) out(opts[:message], opts) if opts[:message] end |
.out(object, opts = {}) ⇒ Object
29 30 31 32 |
# File 'lib/letters/helpers.rb', line 29 def self.out(object, opts={}) opts = { stream: $stdout, format: "string" }.merge opts opts[:stream].puts Helpers.send(opts[:format], object) end |
.pp(object) ⇒ Object
44 45 46 47 |
# File 'lib/letters/helpers.rb', line 44 def self.pp(object) require "pp" object.pretty_inspect end |
.pretty_callstack(callstack) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/letters/helpers.rb', line 63 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 |
.string(object) ⇒ Object
49 50 51 |
# File 'lib/letters/helpers.rb', line 49 def self.string(object) object.to_s end |
.xml(object) ⇒ Object
53 54 55 56 |
# File 'lib/letters/helpers.rb', line 53 def self.xml(object) require "xmlsimple" XmlSimple.xml_out(object, { "KeepRoot" => true }) end |
.yaml(object) ⇒ Object
58 59 60 61 |
# File 'lib/letters/helpers.rb', line 58 def self.yaml(object) require "yaml" object.to_yaml end |