dsl_evaluator 0.2.2 → 0.2.5

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: 26846cd800a79e9572ce8c211cd24d1e4911853d20ddcbdc10ae986963735967
4
- data.tar.gz: 309e468802ecc3b8633ca6e56278c919b7a152dfc797f7ce8f841b25ad672894
3
+ metadata.gz: 75e69e5dcedabbb5526f3904000165c50045a1f1629957c576132daec5c538ee
4
+ data.tar.gz: 1ce18a76ee79dd815bf7284b3cf831097b48a0a863cefe1cd217b88e663d82fc
5
5
  SHA512:
6
- metadata.gz: 7f2de655796b2f5ff2da73cb7c5287ecbb2030d15ac8e05c7a41aadaa4e0ba304cd0c1b05b6535134feb8df6d8bd90c405f37ac631837b8a9dbd77700a9f7ab4
7
- data.tar.gz: fc056998929261db856e2d2755e8dcda41cceeaacd815dd62c22c9dd1ad846f37d4dc361d078a9bdc93fcb7c866b8fa75269db39ae18f7b93a7ab4315c0e6c4c
6
+ metadata.gz: d521c3290df0b2c6347a56233ae4ee4f24bc20e7c1eefe05c99cfe810ebd99fca9041ace34c5e43e2963e3a4fb52a2937310b72e5871d89d2ed1cf3459cb325f
7
+ data.tar.gz: 8456cdad0388860b6011e9c849712281dc00640a562fa44f9b3d2a67cccb3a9b89ee0eb8ff52aa07aa1153dfdae001a7c9e763ba9621afb8e775a32b17405a56
data/CHANGELOG.md CHANGED
@@ -3,6 +3,15 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/).
5
5
 
6
+ ## [0.2.5] - 2022-03-06
7
+ - [#6](https://github.com/tongueroo/dsl_evaluator/pull/6) DslEvaluator.print_code public method
8
+
9
+ ## [0.2.4] - 2022-03-03
10
+ - print error message as part of trace
11
+
12
+ ## [0.2.3] - 2022-02-27
13
+ - [#5](https://github.com/tongueroo/dsl_evaluator/pull/5) add zeitwerk dependency
14
+
6
15
  ## [0.2.2] - 2022-02-25
7
16
  - dont filter lines with FULL_BACKTRACE=1
8
17
 
data/README.md CHANGED
@@ -14,7 +14,6 @@ DslEvaluator.configure do |config|
14
14
  config.root = Lono.root
15
15
  end
16
16
 
17
-
18
17
  class Dsl
19
18
  include DslEvaluator
20
19
  def build
@@ -24,6 +23,14 @@ class Dsl
24
23
  end
25
24
  ```
26
25
 
26
+ For other libraries where printing the code and context lines around the code is useful, you can use:
27
+
28
+ ```ruby
29
+ path = "replace with path to file"
30
+ line_number = "replace with line number. usually can get the exception.message"
31
+ DslEvaluator.print_code(path, line_number)
32
+ ```
33
+
27
34
  ## Installation
28
35
 
29
36
  Add this line to your application's Gemfile:
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency "activesupport"
26
26
  spec.add_dependency "memoist"
27
27
  spec.add_dependency "rainbow"
28
+ spec.add_dependency "zeitwerk"
28
29
  end
@@ -1,3 +1,5 @@
1
+ require "singleton"
2
+
1
3
  module DslEvaluator
2
4
  class App
3
5
  extend Memoist
@@ -1,3 +1,5 @@
1
+ require "zeitwerk"
2
+
1
3
  module DslEvaluator
2
4
  class Autoloader
3
5
  class Inflector < Zeitwerk::Inflector
@@ -9,25 +9,14 @@ module DslEvaluator
9
9
  info = error_info
10
10
  path = info[:path]
11
11
  line_number = info[:line_number].to_i
12
-
12
+ logger.error "ERROR: #{@error.message}".color(:red)
13
13
  logger.error "Error evaluating #{pretty_path(path)}".color(:red)
14
14
  logger.error "Here's the line with the error:\n\n"
15
+ print_code(path, line_number)
16
+ end
15
17
 
16
- contents = IO.read(path)
17
- content_lines = contents.split("\n")
18
- context = 5 # lines of context
19
- top, bottom = [line_number-context-1, 0].max, line_number+context-1
20
- lpad = content_lines.size.to_s.size
21
- content_lines[top..bottom].each_with_index do |line_content, index|
22
- current_line = top+index+1
23
- if current_line == line_number
24
- printf("%#{lpad}d %s\n".color(:red), current_line, line_content)
25
- else
26
- printf("%#{lpad}d %s\n", current_line, line_content)
27
- end
28
- end
29
-
30
- logger.info "Rerun with FULL_BACKTRACE=1 to see full backtrace" unless ENV['FULL_BACKTRACE']
18
+ def print_code(path, line_number)
19
+ DslEvaluator.print_code(path, line_number)
31
20
  end
32
21
 
33
22
  def error_info
@@ -1,3 +1,3 @@
1
1
  module DslEvaluator
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.5"
3
3
  end
data/lib/dsl_evaluator.rb CHANGED
@@ -44,5 +44,36 @@ module DslEvaluator
44
44
  end
45
45
  memoize :config
46
46
 
47
+ # So other libraries can use this method
48
+ def print_code(path, line_number)
49
+ check_line_number!(line_number)
50
+ line_number = line_number.to_i
51
+ contents = IO.read(path)
52
+ content_lines = contents.split("\n")
53
+ context = 5 # lines of context
54
+ top, bottom = [line_number-context-1, 0].max, line_number+context-1
55
+ lpad = content_lines.size.to_s.size
56
+ content_lines[top..bottom].each_with_index do |line_content, index|
57
+ current_line = top+index+1
58
+ if current_line == line_number
59
+ printf("%#{lpad}d %s\n".color(:red), current_line, line_content)
60
+ else
61
+ printf("%#{lpad}d %s\n", current_line, line_content)
62
+ end
63
+ end
64
+
65
+ logger.info "Rerun with FULL_BACKTRACE=1 to see full backtrace" unless ENV['FULL_BACKTRACE']
66
+ end
67
+
68
+ def check_line_number!(line_number)
69
+ return line_number unless line_number.is_a?(String)
70
+ integer = line_number.to_i
71
+ if integer == 0
72
+ logger.error "ERROR: Think you accidentally passed in a String for the line_number: #{line_number}".color(:red)
73
+ puts caller
74
+ exit 1
75
+ end
76
+ end
77
+
47
78
  extend self
48
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsl_evaluator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-25 00:00:00.000000000 Z
11
+ date: 2022-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: zeitwerk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description:
56
70
  email:
57
71
  - tongueroo@gmail.com