render_me_pretty 0.9.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28f4378bce74504d490fde1c53d18fa5132a9aa5ea072fdde13a322181222325
4
- data.tar.gz: 35d9971f8c07a02bf099d2b0a78c6d204fa5a542027399d99d334776366b0615
3
+ metadata.gz: 80b046fa6c8baf78bd662403f3db8c7d069bcf42f244cc6045763f32b592f075
4
+ data.tar.gz: d30655c34e425304fab8c46fb8d6ba1fec8b6c886d7fb8d95b8579516888a3bf
5
5
  SHA512:
6
- metadata.gz: b370e5090fcc8c5c6344caf169562a491ea319d1cc866d4968f5ea659e10009ed17383413181b8657048040e77cc99d8e18e01d9bf04d50fa8f21a885895fcff
7
- data.tar.gz: 4f5b87455ae81cb9e3b49f7e1771e1155f5c8a3a6e797e8833616c6dcef6f836f2de99546cf880aeaad3c081345406202f01574652490d7c50095d43b614e54a
6
+ metadata.gz: 4908d4dfe9b6147284d7ec66f4fce89b2cbeecf186f5a4c93fae1164909921200a2923c6c62f3abef4d075183fd0159cc7e9385d59baef2fdf01df7b796737a3
7
+ data.tar.gz: b3b036d451a78ede232701541606a0cb1eae094a6f78d64aaaa2924cd4a79e47f4014f5cc9ea715dc6bd7a069f2a52fe484550f5912dcb23e8c4984829d4ddc0
data/CHANGELOG.md CHANGED
@@ -3,6 +3,11 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [1.0.0] - 2024-08-29
7
+ - [#5](https://github.com/tongueroo/render_me_pretty/pull/5) Standardrb
8
+ - [#6](https://github.com/tongueroo/render_me_pretty/pull/6) add mit license
9
+ - [#7](https://github.com/tongueroo/render_me_pretty/pull/7) on_error config option error exit or raise
10
+
6
11
  ## [0.9.0] - 2023-06-06
7
12
  - [#2](https://github.com/tongueroo/render_me_pretty/pull/2) require tilt so EMPTY_HASH is defined
8
13
  - [#3](https://github.com/tongueroo/render_me_pretty/pull/3) CI: Github Actions Rspec
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in render_me_pretty.gemspec
6
6
  gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Tung Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :spec
2
+ task default: :spec
@@ -1,6 +1,6 @@
1
1
  class RenderMePretty::Erb
2
2
  class BaseHandler
3
- def initialize(exception, path, layout_path=nil)
3
+ def initialize(exception, path, layout_path = nil)
4
4
  @exception = exception
5
5
  @path = path
6
6
  @layout_path = layout_path
@@ -8,25 +8,25 @@ class RenderMePretty::Erb
8
8
 
9
9
  def handle
10
10
  line_number = find_line_number
11
- pretty_trace(line_number, full_message=true) # returns StringIO
11
+ pretty_trace(line_number, true) # returns StringIO
12
12
  end
13
13
 
14
- def pretty_trace(error_line_number, full_message=true)
14
+ def pretty_trace(error_line_number, full_message = true)
15
15
  io = StringIO.new
16
16
 
17
17
  message = full_message ? ": #{@exception.message}" : ""
18
18
  io.puts "#{@exception.class}#{message}".color(:red)
19
19
 
20
- pretty_path = template_path_with_error.sub(/^\.\//, '')
20
+ pretty_path = template_path_with_error.sub(/^\.\//, "")
21
21
  io.puts "Error evaluating ERB template around line #{error_line_number.to_s.color(:red)} of: #{pretty_path}:"
22
22
 
23
23
  context = 5 # lines of context
24
- top, bottom = [error_line_number-context-1, 0].max, error_line_number+context-1
24
+ top, bottom = [error_line_number - context - 1, 0].max, error_line_number + context - 1
25
25
 
26
26
  lines = IO.read(template_path_with_error).split("\n")
27
27
  spacing = lines.size.to_s.size
28
28
  lines[top..bottom].each_with_index do |line_content, index|
29
- current_line_number = top+index+1
29
+ current_line_number = top + index + 1
30
30
  if current_line_number == error_line_number
31
31
  io.printf("%#{spacing}d %s\n".color(:red), current_line_number, line_content)
32
32
  else
@@ -56,11 +56,10 @@ class RenderMePretty::Erb
56
56
  # /Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:82:in `block in build_templates'
57
57
  # /Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:81:in `each'
58
58
  def backtrace_lines
59
- full = ENV['FULL_BACKTRACE']
59
+ full = ENV["FULL_BACKTRACE"]
60
+ lines = @exception.backtrace
60
61
  if full
61
- lines = @exception.backtrace
62
62
  else
63
- lines = @exception.backtrace
64
63
  # This filtering business makes is hiding useful info.
65
64
  # Think it was needed for ERB but Tilt provides a better stack trace.
66
65
  # Commenting out for now.
@@ -68,15 +67,15 @@ class RenderMePretty::Erb
68
67
  # filter out internal lines
69
68
  # removal_index = lines.find_index { |l| l =~ %r[lib/render_me_pretty] }
70
69
  # lines = lines[removal_index..-1] # remove leading lines above the lib/
71
- # render_me_pretty lines by keeping lines past the removal index
70
+ # render_me_pretty lines by keeping lines past the removal index
72
71
  # lines.reject! { |l| l =~ %r[lib/render_me_pretty] } # now filter out
73
- # render_me_pretty lines
72
+ # render_me_pretty lines
74
73
  lines = lines[0..7] # keep 8 lines
75
74
  end
76
75
  lines[0] = lines[0].color(:red)
77
76
 
78
77
  # header
79
- lines.unshift "\nOriginal backtrace#{full ? '' : ' (last 8 lines)'}:"
78
+ lines.unshift "\nOriginal backtrace#{full ? "" : " (last 8 lines)"}:"
80
79
  # footer
81
80
  lines << "\nRe-run with FULL_BACKTRACE=1 to see all lines"
82
81
  lines.join("\n")
@@ -7,15 +7,15 @@ class RenderMePretty::Erb
7
7
  # error_info = e.backtrace[0]
8
8
  def find_line_number
9
9
  lines = @exception.backtrace
10
- error_line = lines.select do |line|
10
+ error_line = lines.find do |line|
11
11
  line.include?(template_path_with_error)
12
- end.first
12
+ end
13
13
 
14
14
  if error_line.nil?
15
15
  puts "WARN: Unable to find line number. Fallback and print out full backtrace."
16
16
  puts @exception.backtrace
17
17
  else
18
- error_line.split(':')[1].to_i
18
+ error_line.split(":")[1].to_i
19
19
  end
20
20
  end
21
21
 
@@ -1,54 +1,52 @@
1
- =begin
2
- ## Usage examples:
3
-
4
- Given an example template /path/to/template.erb that contains:
5
-
6
- a: <%= @a %>
7
-
1
+ # ## Usage examples:
2
+ #
3
+ # Given an example template /path/to/template.erb that contains:
4
+ #
5
+ # a: <%= @a %>
6
+ #
8
7
  ### Variables at initialization
9
-
10
- erb = RenderMePretty::Erb.new("/path/to/template.erb", a: 1)
11
- erb.render
12
-
13
- Result: a: 1
14
-
8
+ #
9
+ # erb = RenderMePretty::Erb.new("/path/to/template.erb", a: 1)
10
+ # erb.render
11
+ #
12
+ # Result: a: 1
13
+ #
15
14
  ### Variables at render time
16
-
17
- erb = RenderMePretty::Erb.new("/path/to/template.erb")
18
- erb.render(a: 2)
19
-
20
- Result: a: 2
21
-
15
+ #
16
+ # erb = RenderMePretty::Erb.new("/path/to/template.erb")
17
+ # erb.render(a: 2)
18
+ #
19
+ # Result: a: 2
20
+ #
22
21
  ### Variables at both initialization and render time:
23
-
24
- erb = RenderMePretty::Erb.new("/path/to/template.erb", a: 3)
25
- erb.render(a: "override", a: 4)
26
-
27
- Result: a: 4
28
-
29
- Variables at render time will override variables at initialization time.
30
-
22
+ #
23
+ # erb = RenderMePretty::Erb.new("/path/to/template.erb", a: 3)
24
+ # erb.render(a: "override", a: 4)
25
+ #
26
+ # Result: a: 4
27
+ #
28
+ # Variables at render time will override variables at initialization time.
29
+ #
31
30
  ## Context Scope
32
-
33
- If you want to use your own context object, pass it as a variable. The context variable is specially treated as a context object. Example:
34
-
35
- person = Person.new # must implement get_binding
36
- erb = RenderMePretty::Erb.new("/path/to/template.erb")
37
- erb.render(context: person, a: 2)
38
-
39
- The context will be `person`. So person methods and instance variables will be available in the ERB templates.
40
-
41
- =end
42
- require 'tilt'
43
- require 'tilt/erb'
31
+ #
32
+ # If you want to use your own context object, pass it as a variable. The context variable is specially treated as a context object. Example:
33
+ #
34
+ # person = Person.new # must implement get_binding
35
+ # erb = RenderMePretty::Erb.new("/path/to/template.erb")
36
+ # erb.render(context: person, a: 2)
37
+ #
38
+ # The context will be `person`. So person methods and instance variables will be available in the ERB templates.
39
+ #
40
+ require "tilt"
41
+ require "tilt/erb"
44
42
 
45
43
  module RenderMePretty
46
44
  class Erb
47
- autoload :BaseHandler, 'render_me_pretty/erb/base_handler'
45
+ autoload :BaseHandler, "render_me_pretty/erb/base_handler"
48
46
  autoload :SyntaxErrorHandler, "render_me_pretty/erb/syntax_error_handler"
49
47
  autoload :MainErrorHandler, "render_me_pretty/erb/main_error_handler"
50
48
 
51
- def initialize(path, variables={})
49
+ def initialize(path, variables = {})
52
50
  @path = path
53
51
  @init_vars = variables
54
52
  @context = variables.delete(:context)
@@ -71,11 +69,11 @@ module RenderMePretty
71
69
  context = context.clone # so we dont stomp the original object
72
70
  # override context's instance variables with init and render vars.
73
71
  @init_vars.each do |key, value|
74
- context.instance_variable_set('@' + key.to_s, value)
72
+ context.instance_variable_set("@" + key.to_s, value)
75
73
  end
76
74
 
77
75
  # https://github.com/gotar/dry-view/commit/39e3f96625bf90da2e51fb1fd437f18cedb9ae8c
78
- tilt_options = {trim: '-', default_encoding: "utf-8"}
76
+ tilt_options = {trim: "-", default_encoding: "utf-8"}
79
77
  if @layout_path
80
78
  layout = Tilt::ERBTemplate.new(@layout_path, tilt_options)
81
79
  else
@@ -91,12 +89,8 @@ module RenderMePretty
91
89
  else
92
90
  template.render(context)
93
91
  end
94
- rescue Exception => e
95
- if e.class == SystemExit # allow exit to happen normally
96
- raise
97
- else
98
- handle_exception(e)
99
- end
92
+ rescue StandardError, ScriptError => e
93
+ handle_exception(e)
100
94
  end
101
95
  end
102
96
 
@@ -122,11 +116,15 @@ module RenderMePretty
122
116
  end
123
117
 
124
118
  def print_result(io)
125
- if ENV['TEST']
119
+ if ENV["TEST"]
126
120
  io.string
127
121
  else
128
122
  puts io.string
129
- exit 1
123
+ if RenderMePretty.on_error.to_sym == :exit
124
+ exit 1
125
+ else
126
+ raise
127
+ end
130
128
  end
131
129
  end
132
130
 
@@ -144,11 +142,10 @@ module RenderMePretty
144
142
  # /Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:82:in `block in build_templates'
145
143
  # /Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:81:in `each'
146
144
  def backtrace_lines(e)
147
- full = ENV['FULL_BACKTRACE']
145
+ full = ENV["FULL_BACKTRACE"]
146
+ lines = e.backtrace
148
147
  if full
149
- lines = e.backtrace
150
148
  else
151
- lines = e.backtrace
152
149
  # This filtering business makes is hiding useful info.
153
150
  # Think it was needed for ERB but Tilt provides a better stack trace.
154
151
  # Commenting out for now.
@@ -156,15 +153,15 @@ module RenderMePretty
156
153
  # filter out internal lines
157
154
  # removal_index = lines.find_index { |l| l =~ %r[lib/render_me_pretty] }
158
155
  # lines = lines[removal_index..-1] # remove leading lines above the lib/
159
- # render_me_pretty lines by keeping lines past the removal index
156
+ # render_me_pretty lines by keeping lines past the removal index
160
157
  # lines.reject! { |l| l =~ %r[lib/render_me_pretty] } # now filter out
161
- # render_me_pretty lines
158
+ # render_me_pretty lines
162
159
  lines = lines[0..7] # keep 8 lines
163
160
  end
164
161
  lines[0] = lines[0].color(:red)
165
162
 
166
163
  # header
167
- lines.unshift "\nOriginal backtrace#{full ? '' : ' (last 8 lines)'}:"
164
+ lines.unshift "\nOriginal backtrace#{full ? "" : " (last 8 lines)"}:"
168
165
  # footer
169
166
  lines << "\nRe-run with FULL_BACKTRACE=1 to see all lines"
170
167
  lines.join("\n")
@@ -1,3 +1,3 @@
1
1
  module RenderMePretty
2
- VERSION = "0.9.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -4,12 +4,15 @@ require "active_support/core_ext/string"
4
4
  require "rainbow/ext/string"
5
5
 
6
6
  module RenderMePretty
7
- autoload :Erb, 'render_me_pretty/erb'
7
+ autoload :Erb, "render_me_pretty/erb"
8
8
 
9
- def result(path, variables={})
9
+ def result(path, variables = {})
10
10
  erb = Erb.new(path, variables)
11
11
  erb.render
12
12
  end
13
13
 
14
+ mattr_accessor :on_error
15
+ self.on_error = :exit # :exit or :raise
16
+
14
17
  extend self
15
18
  end
@@ -3,19 +3,20 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require "render_me_pretty/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "render_me_pretty"
7
- spec.version = RenderMePretty::VERSION
8
- spec.authors = ["Tung Nguyen"]
9
- spec.email = ["tongueroo@gmail.com"]
6
+ spec.name = "render_me_pretty"
7
+ spec.version = RenderMePretty::VERSION
8
+ spec.authors = ["Tung Nguyen"]
9
+ spec.email = ["tongueroo@gmail.com"]
10
10
 
11
- spec.summary = %q{Render ERB template and provide more useful message pointing out the line with the error in the view}
12
- spec.homepage = "https://github.com/tongueroo/render_me_pretty"
11
+ spec.summary = "Render ERB template and provide more useful message pointing out the line with the error in the view"
12
+ spec.homepage = "https://github.com/tongueroo/render_me_pretty"
13
+ spec.license = "MIT"
13
14
 
14
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
16
  f.match(%r{^(test|spec|features)/})
16
17
  end
17
- spec.bindir = "exe"
18
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
20
  spec.require_paths = ["lib"]
20
21
 
21
22
  spec.add_dependency "activesupport"
data/tilt_examples.rb CHANGED
@@ -1,18 +1,18 @@
1
- require 'tilt/erb'
2
- template = Tilt::ERBTemplate.new('templates/foo.erb')
3
- joe = Person.find('joe')
4
- output = template.render(joe, :x => 35, :y => 42)
1
+ require "tilt/erb"
2
+ template = Tilt::ERBTemplate.new("templates/foo.erb")
3
+ joe = Person.find("joe")
4
+ template.render(joe, x: 35, y: 42)
5
5
  # If no scope is provided, the template is evaluated within the context of an object created with Object.new.
6
6
 
7
7
  # A single Template instance's render method may be called multiple times with different scope and locals arguments. Continuing the previous example, we render the same compiled template but this time in jane's scope:
8
8
 
9
- jane = Person.find('jane')
10
- output = template.render(jane, :x => 22, :y => nil)
9
+ jane = Person.find("jane")
10
+ template.render(jane, x: 22, y: nil)
11
11
  # Blocks can be passed to render for templates that support running arbitrary ruby code (usually with some form of yield). For instance, assuming the following in foo.erb:
12
12
 
13
13
  # Hey <%= yield %>!
14
14
  # The block passed to render is called on yield:
15
15
 
16
- template = Tilt::ERBTemplate.new('foo.erb')
17
- template.render { 'Joe' }
16
+ template = Tilt::ERBTemplate.new("foo.erb")
17
+ template.render { "Joe" }
18
18
  # => "Hey Joe!"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render_me_pretty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-06 00:00:00.000000000 Z
11
+ date: 2024-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -106,6 +106,7 @@ files:
106
106
  - ".rspec"
107
107
  - CHANGELOG.md
108
108
  - Gemfile
109
+ - LICENSE.txt
109
110
  - README.md
110
111
  - Rakefile
111
112
  - bin/console
@@ -119,7 +120,8 @@ files:
119
120
  - render_me_pretty.gemspec
120
121
  - tilt_examples.rb
121
122
  homepage: https://github.com/tongueroo/render_me_pretty
122
- licenses: []
123
+ licenses:
124
+ - MIT
123
125
  metadata: {}
124
126
  post_install_message:
125
127
  rdoc_options: []
@@ -136,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
138
  - !ruby/object:Gem::Version
137
139
  version: '0'
138
140
  requirements: []
139
- rubygems_version: 3.4.10
141
+ rubygems_version: 3.5.10
140
142
  signing_key:
141
143
  specification_version: 4
142
144
  summary: Render ERB template and provide more useful message pointing out the line