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 +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +2 -1
- data/Gemfile.lock +1 -1
- data/README.md +9 -0
- data/lib/typerb/exceptional.rb +4 -0
- data/lib/typerb/variable_name.rb +1 -1
- data/lib/typerb/version.rb +1 -1
- data/lib/typerb.rb +14 -0
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c844da90032caf4ef3ad3b5e14ab417c8bda85c0d876028b075d1f8b154cfae
|
4
|
+
data.tar.gz: 2ba22c2934fcdda11f528e9032ee759712374b4ee06be867335e9539534ddd36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a8b01b03026e99f4387f98c0f326c2ad2a97149421704c60f904e21932a90925f085fac0455a4261ff4db6e8ea4d0978fd38667ad09c42b529c87882b68499b
|
7
|
+
data.tar.gz: 6cbdabf0eae0724331d8d253b32e3a9c5f71748eb9217213b65d45f38db674676795c94c34405287f38b52f24d129a2238ce1ae7971a1dc7d73278e36ad69b0d
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.6.
|
1
|
+
2.6.1
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
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:
|
data/lib/typerb/exceptional.rb
CHANGED
data/lib/typerb/variable_name.rb
CHANGED
data/lib/typerb/version.rb
CHANGED
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.
|
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:
|
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
|
-
|
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.
|