typerb 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -0
- data/.travis.yml +5 -0
- data/Gemfile.lock +1 -1
- data/lib/typerb/exceptional.rb +17 -0
- data/lib/typerb/variable_name.rb +30 -0
- data/lib/typerb/version.rb +1 -1
- data/lib/typerb.rb +11 -33
- data/typerb.gemspec +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ce9f42a58f215a179f850016e0f647a4e16b4f23b3021d7cd17a43960857439
|
4
|
+
data.tar.gz: 9bb4dc2cb1f2396c7ccfd773d27a2d85e0d11f3a66ef54537e9c809594795d5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a35899b9f7f7cc045725241d6eef0d312470016e2945edea86c67696c67f3ba69bbc52743fd51cb0253b6b7bc4f3be30ac9567784269a6f382d46484b9bc2476
|
7
|
+
data.tar.gz: 58c8a8839809ff2c87801388cca35dca300a565616645a01d9bd59fdea6e6986c74763250093400aaca8d2b91a47cfad8b24b0060b24ae0215a3bdb846617568
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Typerb
|
4
|
+
class Exceptional # NOTE: don't want to collide with 'Exception' class name
|
5
|
+
class << self
|
6
|
+
def klasses_text(klasses)
|
7
|
+
klasses.size > 1 ? klasses.map(&:name).join(' or ') : klasses.first.name
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def raise_with(backtrace, exception_text)
|
12
|
+
exception = TypeError.new(exception_text)
|
13
|
+
exception.set_backtrace(backtrace)
|
14
|
+
raise exception
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Typerb
|
4
|
+
class VariableName
|
5
|
+
attr_reader :file, :line
|
6
|
+
|
7
|
+
def initialize(caller_loc)
|
8
|
+
@file = caller_loc[0].path
|
9
|
+
@line = caller_loc[0].lineno
|
10
|
+
end
|
11
|
+
|
12
|
+
def get
|
13
|
+
return if RUBY_VERSION < '2.6.0'
|
14
|
+
return unless File.exist?(file)
|
15
|
+
|
16
|
+
caller_method = caller_locations(1, 1)[0].label.to_sym
|
17
|
+
from_ast(caller_method)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def from_ast(caller_method) # rubocop: disable Metrics/AbcSize not worth fixing
|
23
|
+
code = File.read(file).lines[line - 1].strip
|
24
|
+
node = RubyVM::AST.parse(code)
|
25
|
+
if node.children.last.children.size == 3 && node.children.last.children[1] == caller_method # rubocop: disable Style/IfUnlessModifier, Style/GuardClause
|
26
|
+
node.children.last.children.first.children.first
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/typerb/version.rb
CHANGED
data/lib/typerb.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'typerb/version'
|
4
|
+
require 'typerb/variable_name'
|
5
|
+
require 'typerb/exceptional'
|
4
6
|
|
5
7
|
module Typerb
|
6
8
|
refine Object do
|
@@ -8,50 +10,26 @@ module Typerb
|
|
8
10
|
raise ArgumentError, 'provide at least one class' if klasses.empty?
|
9
11
|
return self if klasses.any? { |kls| is_a?(kls) }
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
+
klasses_text = Typerb::Exceptional.klasses_text(klasses)
|
14
|
+
exception_text = if (var_name = Typerb::VariableName.new(caller_locations(1, 1)).get)
|
15
|
+
"`#{var_name}` should be #{klasses_text}, not #{self.class} (#{self})"
|
13
16
|
else
|
14
|
-
"expected #{
|
17
|
+
"expected #{klasses_text}, got #{self.class} (#{self})"
|
15
18
|
end
|
16
19
|
|
17
|
-
|
18
|
-
exception.set_backtrace(caller)
|
19
|
-
raise exception
|
20
|
+
Typerb::Exceptional.new.raise_with(caller, exception_text)
|
20
21
|
end
|
21
22
|
|
22
23
|
def not_nil!
|
23
|
-
return self unless self.nil? # rubocop: disable Style/RedundantSelf
|
24
|
+
return self unless self.nil? # rubocop: disable Style/RedundantSelf rubocop breaks without reundant self
|
24
25
|
|
25
|
-
exception_text = if (var_name =
|
26
|
+
exception_text = if (var_name = Typerb::VariableName.new(caller_locations(1, 1)).get)
|
26
27
|
"`#{var_name}` should not be nil"
|
27
28
|
else
|
28
|
-
'expected not nil,
|
29
|
+
'expected not nil, got nil'
|
29
30
|
end
|
30
31
|
|
31
|
-
|
32
|
-
exception.set_backtrace(caller)
|
33
|
-
raise exception
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
module VariableNameUtil
|
38
|
-
module_function
|
39
|
-
|
40
|
-
def get(c_loc) # rubocop: disable Metrics/AbcSize screw this
|
41
|
-
where = c_loc[0]
|
42
|
-
file = where.path
|
43
|
-
line = where.lineno
|
44
|
-
return unless File.exist?(file)
|
45
|
-
|
46
|
-
code = File.read(file).lines[line - 1].strip
|
47
|
-
node = RubyVM::AST.parse(code)
|
48
|
-
if node.children.last.children.size == 3 && %i[type! not_nil!].include?(node.children.last.children[1]) # rubocop: disable Style/IfUnlessModifier, Style/GuardClause
|
49
|
-
node.children.last.children.first.children.first
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def klasses_text(klasses)
|
54
|
-
klasses.size > 1 ? klasses.map(&:name).join(' or ') : klasses.first.name
|
32
|
+
Typerb::Exceptional.new.raise_with(caller, exception_text)
|
55
33
|
end
|
56
34
|
end
|
57
35
|
end
|
data/typerb.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typerb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Antonyan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -115,6 +115,8 @@ files:
|
|
115
115
|
- bin/console
|
116
116
|
- bin/setup
|
117
117
|
- lib/typerb.rb
|
118
|
+
- lib/typerb/exceptional.rb
|
119
|
+
- lib/typerb/variable_name.rb
|
118
120
|
- lib/typerb/version.rb
|
119
121
|
- typerb.gemspec
|
120
122
|
homepage: https://github.com/olegantonyan/typerb
|
@@ -129,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
129
131
|
requirements:
|
130
132
|
- - ">="
|
131
133
|
- !ruby/object:Gem::Version
|
132
|
-
version: 2.
|
134
|
+
version: '2.4'
|
133
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
136
|
requirements:
|
135
137
|
- - ">="
|