typerb 0.1.8 → 0.2.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: 9ee985e57292c037f842de48682659f4fb90e6f18d26811e065c4b32983a5bbb
4
- data.tar.gz: f4960c88faa5d885690405c70a514cbcea191e12964f7e83b50d925bb0848911
3
+ metadata.gz: 0c844da90032caf4ef3ad3b5e14ab417c8bda85c0d876028b075d1f8b154cfae
4
+ data.tar.gz: 2ba22c2934fcdda11f528e9032ee759712374b4ee06be867335e9539534ddd36
5
5
  SHA512:
6
- metadata.gz: c816163ee5396ecfcd09234b9e2590b8a75bc06ef6ea839897da6bc2722c44bbe9bdbc3c1aef75858be28ae6df118ad834e6548b68449a140381d3188b3d9684
7
- data.tar.gz: 84a1286086584e24305f1816f93cf573bef0a257f7fb541e223986e260697c780ea14d6c22fe9cc59857df95e99f1be4f4a438a9b48265072b055d3823dc5819
6
+ metadata.gz: 8a8b01b03026e99f4387f98c0f326c2ad2a97149421704c60f904e21932a90925f085fac0455a4261ff4db6e8ea4d0978fd38667ad09c42b529c87882b68499b
7
+ data.tar.gz: 6cbdabf0eae0724331d8d253b32e3a9c5f71748eb9217213b65d45f38db674676795c94c34405287f38b52f24d129a2238ce1ae7971a1dc7d73278e36ad69b0d
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.6.0-rc1
1
+ 2.6.1
data/.travis.yml CHANGED
@@ -7,7 +7,8 @@ before_install: gem install bundler
7
7
  rvm:
8
8
  - 2.4.0
9
9
  - 2.5.3
10
- - 2.6.0-rc1
10
+ - 2.6.0
11
+ - 2.6.1
11
12
 
12
13
  script:
13
14
  - bundle exec rubocop -DP
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- typerb (0.1.7)
4
+ typerb (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -17,10 +17,14 @@ class A
17
17
  some_arg.respond_to!(:strip)
18
18
  end
19
19
 
20
+ def call_with_enum(arg)
21
+ arg.enum!(:one, :two)
22
+ end
20
23
  end
21
24
 
22
25
  A.new.call(1) #=> TypeError: `some_arg` should be String or Symbol, not Integer
23
26
  A.new.call_with_respond_checks(1) #=> TypeError: 'Integer should respond to all methods: strip'
27
+ A.new.call_with_enum(:three) #=> TypeError: 'Symbol (`arg`) should be one of: [one, two], not three'
24
28
  ```
25
29
 
26
30
  This is equivalent to:
@@ -52,6 +56,11 @@ end
52
56
  A.new.call(nil) #=> TypeError: expected not nil, but got nil
53
57
  ```
54
58
 
59
+ ## Why?
60
+
61
+ 1. Catch error as early as possible (especially nils);
62
+ 2. Additional documentation: you're telling other people more about interfaces.
63
+
55
64
  ## Installation
56
65
 
57
66
  Add this line to your application's Gemfile:
@@ -10,6 +10,10 @@ module Typerb
10
10
  def methods_text(methods)
11
11
  methods.join(', ')
12
12
  end
13
+
14
+ def elements_text(elements)
15
+ '[' + elements.join(', ') + ']'
16
+ end
13
17
  end
14
18
 
15
19
  def raise_with(backtrace, exception_text)
@@ -10,7 +10,7 @@ module Typerb
10
10
  end
11
11
 
12
12
  def get
13
- return if RUBY_VERSION < '2.6.0'
13
+ return unless defined?(RubyVM::AbstractSyntaxTree)
14
14
  return unless File.exist?(file)
15
15
 
16
16
  caller_method = caller_locations(1, 1)[0].label.to_sym
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Typerb
4
- VERSION = '0.1.8'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/typerb.rb CHANGED
@@ -45,5 +45,19 @@ module Typerb
45
45
 
46
46
  Typerb::Exceptional.new.raise_with(caller, exception_text)
47
47
  end
48
+
49
+ def enum!(*elements)
50
+ raise ArgumentError, 'provide at least one enum element' if elements.empty?
51
+ return self if elements.include?(self)
52
+
53
+ elements_text = Typerb::Exceptional.elements_text(elements)
54
+ exception_text = if (var_name = Typerb::VariableName.new(caller_locations(1, 1)).get)
55
+ "#{self.class} (`#{var_name}`) should be one of: #{elements_text}, not #{self}"
56
+ else
57
+ "#{self.class} expected one of: #{elements_text}, got #{self}"
58
+ end
59
+
60
+ Typerb::Exceptional.new.raise_with(caller, exception_text)
61
+ end
48
62
  end
49
63
  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.8
4
+ version: 0.2.0
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-12-12 00:00:00.000000000 Z
11
+ date: 2019-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -167,8 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
167
  - !ruby/object:Gem::Version
168
168
  version: '0'
169
169
  requirements: []
170
- rubyforge_project:
171
- rubygems_version: 3.0.0.beta3
170
+ rubygems_version: 3.0.1
172
171
  signing_key:
173
172
  specification_version: 4
174
173
  summary: Typecheck sugar for Ruby.