Class: RenderMePretty::Erb
- Inherits:
-
Object
- Object
- RenderMePretty::Erb
- Defined in:
- lib/render_me_pretty/erb.rb,
lib/render_me_pretty/erb/base_handler.rb,
lib/render_me_pretty/erb/main_error_handler.rb,
lib/render_me_pretty/erb/syntax_error_handler.rb
Defined Under Namespace
Classes: BaseHandler, MainErrorHandler, SyntaxErrorHandler
Instance Method Summary collapse
-
#backtrace_lines(e) ⇒ Object
Method produces a filtered original stack trace that can be appended to the pretty backtrace output.
-
#handle_exception(e) ⇒ Object
Handles Tilt error in prettier manner.
-
#initialize(path, variables = {}) ⇒ Erb
constructor
A new instance of Erb.
- #print_result(io) ⇒ Object
-
#render(*args) ⇒ Object
Usage:.
Constructor Details
#initialize(path, variables = {}) ⇒ Erb
Returns a new instance of Erb.
49 50 51 52 53 54 |
# File 'lib/render_me_pretty/erb.rb', line 49 def initialize(path, variables = {}) @path = path @init_vars = variables @context = variables.delete(:context) @layout_path = variables.delete(:layout) end |
Instance Method Details
#backtrace_lines(e) ⇒ Object
Method produces a filtered original stack trace that can be appended to the pretty backtrace output.
It parses the original backtrace that looks something like this:
(erb):380:in `get_binding'
/Users/tung/.rbenv/versions/2.5.0/lib/ruby/2.5.0/erb.rb:885:in `eval'
/Users/tung/.rbenv/versions/2.5.0/lib/ruby/2.5.0/erb.rb:885:in `result'
/Users/tung/src/tongueroo/lono/vendor/render_me_pretty/lib/render_me_pretty/erb.rb:67:in `render'
/Users/tung/src/tongueroo/lono/vendor/render_me_pretty/lib/render_me_pretty.rb:11:in `result'
/Users/tung/src/tongueroo/lono/lib/lono/template/template.rb:32:in `build'
/Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:82:in `block in build_templates'
/Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:81:in `each'
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/render_me_pretty/erb.rb', line 144 def backtrace_lines(e) full = ENV["FULL_BACKTRACE"] lines = e.backtrace if full else # This filtering business makes is hiding useful info. # Think it was needed for ERB but Tilt provides a better stack trace. # Commenting out for now. # filter out internal lines # removal_index = lines.find_index { |l| l =~ %r[lib/render_me_pretty] } # lines = lines[removal_index..-1] # remove leading lines above the lib/ # render_me_pretty lines by keeping lines past the removal index # lines.reject! { |l| l =~ %r[lib/render_me_pretty] } # now filter out # render_me_pretty lines lines = lines[0..7] # keep 8 lines end lines[0] = lines[0].color(:red) # header lines.unshift "\nOriginal backtrace#{full ? "" : " (last 8 lines)"}:" # footer lines << "\nRe-run with FULL_BACKTRACE=1 to see all lines" lines.join("\n") end |
#handle_exception(e) ⇒ Object
Handles Tilt error in prettier manner.
When there's a syntax error Tilt does not include the line nubmer of the error in the backtrace it is instead included in the e.message itself.
When for other errors the line_number is included in the backtrace.
Refer to specs and uncomment puts out to see the different types of errors.
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/render_me_pretty/erb.rb', line 105 def handle_exception(e) # puts "*" * 30 # puts e.class.to_s.color(:cyan) # puts e.message.color(:cyan) # puts e.backtrace # puts "*" * 30 handler = e.is_a?(SyntaxError) ? SyntaxErrorHandler.new(e, @path, @layout_path) : MainErrorHandler.new(e, @path, @layout_path) io = handler.handle print_result(io) end |
#print_result(io) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/render_me_pretty/erb.rb', line 118 def print_result(io) if ENV["TEST"] io.string else puts io.string if RenderMePretty.on_error.to_sym == :exit exit 1 else raise end end end |
#render(*args) ⇒ Object
Usage:
render(context, a: 1, b: 2)
render(a: 1, b: 2)
render
61 62 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 |
# File 'lib/render_me_pretty/erb.rb', line 61 def render(*args) if args.last.is_a?(Hash) render_vars = args.pop @init_vars = @init_vars.merge(render_vars) end context = args[0] context ||= @context || Object.new context = context.clone # so we dont stomp the original object # override context's instance variables with init and render vars. @init_vars.each do |key, value| context.instance_variable_set("@" + key.to_s, value) end # https://github.com/gotar/dry-view/commit/39e3f96625bf90da2e51fb1fd437f18cedb9ae8c = {trim: "-", default_encoding: "utf-8"} if @layout_path layout = Tilt::ERBTemplate.new(@layout_path, ) else # trim mode: https://searchcode.com/codesearch/view/77362792/ template = Tilt::ERBTemplate.new(@path, ) end begin if @layout_path layout.render(context) do Tilt::ERBTemplate.new(@path, ).render(context) end else template.render(context) end rescue StandardError, ScriptError => e handle_exception(e) end end |