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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19f90a93082e4acdb25f5750c9de2880bf0c613cb414800a7ce8beb275d2ee54
4
- data.tar.gz: 10e47702b997c0f4b54b416c7f77c800ee50343b391a0f41b33ff6bd4b379dc2
3
+ metadata.gz: 1ce9f42a58f215a179f850016e0f647a4e16b4f23b3021d7cd17a43960857439
4
+ data.tar.gz: 9bb4dc2cb1f2396c7ccfd773d27a2d85e0d11f3a66ef54537e9c809594795d5a
5
5
  SHA512:
6
- metadata.gz: 4c3a3d1dbc81a742d08c3e68413a70a7aa8858e473bf46a78dcdec452e862421bf85b02354ad70d203600ea1031ac85dbfc08758ea1bc79145ce03542bc92b6c
7
- data.tar.gz: 55ffd3b8d1ed4fc33141a9345baaf3f7380336bfa7a4135630e24009607d7d3554b8d5892a887676a24a38a70ca7acfa351293b31f8d3f77e13ba31558cdcf5a
6
+ metadata.gz: a35899b9f7f7cc045725241d6eef0d312470016e2945edea86c67696c67f3ba69bbc52743fd51cb0253b6b7bc4f3be30ac9567784269a6f382d46484b9bc2476
7
+ data.tar.gz: 58c8a8839809ff2c87801388cca35dca300a565616645a01d9bd59fdea6e6986c74763250093400aaca8d2b91a47cfad8b24b0060b24ae0215a3bdb846617568
data/.rubocop.yml CHANGED
@@ -1,7 +1,12 @@
1
1
  AllCops:
2
+ TargetRubyVersion: 2.4
2
3
  Exclude:
3
4
  - 'bin/**/*'
4
5
 
6
+ inherit_mode:
7
+ merge:
8
+ - Exclude
9
+
5
10
  Style/CommentedKeyword:
6
11
  Enabled: false
7
12
 
data/.travis.yml CHANGED
@@ -4,6 +4,11 @@ language: ruby
4
4
  cache: bundler
5
5
  before_install: gem install bundler
6
6
 
7
+ rvm:
8
+ - 2.4.0
9
+ - 2.5.3
10
+ # - 2.6.0-preview3 travis does not support it yet
11
+
7
12
  script:
8
13
  - bundle exec rubocop -DP
9
14
  - bundle exec rspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- typerb (0.1.3)
4
+ typerb (0.1.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Typerb
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.6'
5
5
  end
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
- exception_text = if (var_name = VariableNameUtil.get(caller_locations(1, 1)))
12
- "`#{var_name}` should be #{VariableNameUtil.klasses_text(klasses)}, not #{self.class} (#{self})"
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 #{VariableNameUtil.klasses_text(klasses)}, got #{self.class} (#{self})"
17
+ "expected #{klasses_text}, got #{self.class} (#{self})"
15
18
  end
16
19
 
17
- exception = TypeError.new(exception_text)
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 = VariableNameUtil.get(caller_locations(1, 1)))
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, but got nil'
29
+ 'expected not nil, got nil'
29
30
  end
30
31
 
31
- exception = TypeError.new(exception_text)
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
@@ -31,5 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency 'rubocop'
32
32
  spec.add_development_dependency 'super_awesome_print'
33
33
 
34
- spec.required_ruby_version = '>= 2.6.0-preview3'
34
+ spec.required_ruby_version = '>= 2.4'
35
35
  end
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
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-12 00:00:00.000000000 Z
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.6.0.pre.preview3
134
+ version: '2.4'
133
135
  required_rubygems_version: !ruby/object:Gem::Requirement
134
136
  requirements:
135
137
  - - ">="