rtype 0.6.4 → 0.6.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +44 -48
- data/benchmark/benchmark.rb +9 -1
- data/lib/rtype.rb +15 -10
- data/lib/rtype/version.rb +3 -3
- data/spec/rtype_spec.rb +6 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2be0f81f879eb14cec746991740c174607e0246
|
4
|
+
data.tar.gz: 42cdefc859a0d3b3063716bb4c8b1c064adfe9ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f8f677e527e1ae79b30bcb7dce0dcf0b9f13eed3212c33980c58b1e9f3272bd76ae892a606f6de89ce4693ac9a102fbe7a12ae833839e7ce59e6b00ad1a53f5
|
7
|
+
data.tar.gz: afbc41fd9ba4f9377c352170ea24bed21b1fdd65087563a0a04b2f8acff0a4f3d26ebde19a22b8ccc81c7f73e7049b37d5e83070e3dc67077687c2ba67f2e61a
|
data/README.md
CHANGED
@@ -29,6 +29,7 @@ Test::invert(state: 0)
|
|
29
29
|
|
30
30
|
## Requirements
|
31
31
|
- Ruby >= 2.1
|
32
|
+
- If you need to use old ruby, see [rtype-legacy](https://github.com/sputnikgugja/rtype/tree/master/benchmark/benchmark.rb) for ruby 1.9+
|
32
33
|
- MRI
|
33
34
|
- If C native extension is used. otherwise it is not required
|
34
35
|
- JRuby (JRuby 9000+)
|
@@ -40,6 +41,7 @@ Test::invert(state: 0)
|
|
40
41
|
- [Type checking for hash elements](#hash)
|
41
42
|
- [Duck Typing](#duck-typing)
|
42
43
|
- [Typed Array](#typed-array)
|
44
|
+
- [Numeric check](#special-behaviors). e.g. `Int >= 0`
|
43
45
|
- Custom type behavior
|
44
46
|
- ...
|
45
47
|
|
@@ -79,22 +81,21 @@ then, Rtype uses it. (**Do not** `require 'rtype-java'`)
|
|
79
81
|
## Usage
|
80
82
|
|
81
83
|
### Supported Type Behaviors
|
82
|
-
- `Module`
|
83
|
-
- Value must be of this module (`is_a?`)
|
84
|
+
- `Module` : Value must be of this module (`is_a?`)
|
84
85
|
- `Any` : Alias for `BasicObject` (means Any Object)
|
85
86
|
- `Boolean` : `true` or `false`
|
86
|
-
- `Symbol`
|
87
|
-
|
88
|
-
- `
|
89
|
-
|
90
|
-
- `
|
91
|
-
|
92
|
-
- `
|
93
|
-
|
87
|
+
- `Symbol` : Value must respond to a method with this name
|
88
|
+
- `Regexp` : Value must match this regexp pattern
|
89
|
+
- `Range` : Value must be included in this range
|
90
|
+
- `Array` : Value can be any type in this array
|
91
|
+
- `Proc` : Value must return a truthy value for this proc
|
92
|
+
- `true` : Value must be truthy
|
93
|
+
- `false` : Value must be falsy
|
94
|
+
- `nil` : Value must be nil
|
94
95
|
- `Hash`
|
95
96
|
- Value must be a hash
|
96
97
|
- Each of elements must be valid
|
97
|
-
-
|
98
|
+
- Keys of the value must be equal to keys of this hash
|
98
99
|
- **String** key is **different** from **symbol** key
|
99
100
|
- vs. Keyword arguments (e.g.)
|
100
101
|
- `[{}]` is **not** hash argument. it is keyword argument, because its position is last
|
@@ -103,44 +104,9 @@ then, Rtype uses it. (**Do not** `require 'rtype-java'`)
|
|
103
104
|
- `{}` is keyword argument. non-keyword arguments must be in array.
|
104
105
|
- Of course, nested hash works
|
105
106
|
- Example: [Hash](#hash)
|
106
|
-
- `Proc`
|
107
|
-
- Value must return a truthy value for this proc
|
108
|
-
- `true`
|
109
|
-
- Value must be truthy
|
110
|
-
- `false`
|
111
|
-
- Value must be falsy
|
112
|
-
- `nil`
|
113
|
-
- Value must be nil
|
114
107
|
|
115
|
-
- Special Behaviors
|
116
|
-
- `TypedArray`
|
117
|
-
- `Array::of(type)` (recommended)
|
118
|
-
- or `Rtype::Behavior::TypedArray[type]`
|
119
|
-
- Example: [TypedArray](#typed-array)
|
120
|
-
|
121
|
-
- `Num, Int, Flo` : Numeric check
|
122
|
-
- `Num/Int/Flo >/>=/</<=/== x`
|
123
|
-
- e.g. `Num >= 2` means value must be a `Numeric` and >= 2
|
124
|
-
- e.g. `Int >= 2` means value must be a `Integer` and >= 2
|
125
|
-
- e.g. `Flo >= 2` means value must be a `Float` and >= 2
|
126
|
-
|
127
|
-
- `And` : Ensures value is valid for all given types
|
128
|
-
- `Rtype::and(*types)`, `Rtype::Behavior::And[*types]`
|
129
|
-
- or `Array#comb`, `Object#and(*others)`
|
130
|
-
|
131
|
-
- `Xor` : Ensures value is valid for only one of given types
|
132
|
-
- `Rtype::xor(*types)`, `Rtype::Behavior::Xor[*types]`
|
133
|
-
- or `Object#xor(*others)`
|
134
|
-
|
135
|
-
- `Not` : Ensures value is not valid for all given types
|
136
|
-
- `Rtype::not(*types)`, `Rtype::Behavior::Not[*types]`
|
137
|
-
- or `Object#not`
|
138
|
-
|
139
|
-
- `Nilable` : Value can be nil
|
140
|
-
- `Rtype::nilable(type)`, `Rtype::Behavior::Nilable[type]`
|
141
|
-
- or `Object#nilable`, `Object#or_nil`
|
142
|
-
|
143
|
-
- You can create custom behavior by extending `Rtype::Behavior::Base`
|
108
|
+
- [Special Behaviors](#special-behaviors)
|
109
|
+
- `TypedArray`, `Num, Int, Flo`, `And`, `Xor`, `Not`, `Nilable`
|
144
110
|
|
145
111
|
### Examples
|
146
112
|
|
@@ -424,6 +390,36 @@ Example.new.method(:test).return_type
|
|
424
390
|
# => Any
|
425
391
|
```
|
426
392
|
|
393
|
+
#### Special Behaviors
|
394
|
+
- `TypedArray` : Ensures value is an array with the type (type signature)
|
395
|
+
- `Array::of(type)` (recommended)
|
396
|
+
- or `Rtype::Behavior::TypedArray[type]`
|
397
|
+
- Example: [TypedArray](#typed-array)
|
398
|
+
|
399
|
+
- `Num, Int, Flo` : Numeric check
|
400
|
+
- `Num/Int/Flo >/>=/</<=/== x`
|
401
|
+
- e.g. `Num >= 2` means value must be a `Numeric` and >= 2
|
402
|
+
- e.g. `Int >= 2` means value must be a `Integer` and >= 2
|
403
|
+
- e.g. `Flo >= 2` means value must be a `Float` and >= 2
|
404
|
+
|
405
|
+
- `And` : Ensures value is valid for all given types
|
406
|
+
- `Rtype::and(*types)`, `Rtype::Behavior::And[*types]`
|
407
|
+
- or `Array#comb`, `Object#and(*others)`
|
408
|
+
|
409
|
+
- `Xor` : Ensures value is valid for only one of given types
|
410
|
+
- `Rtype::xor(*types)`, `Rtype::Behavior::Xor[*types]`
|
411
|
+
- or `Object#xor(*others)`
|
412
|
+
|
413
|
+
- `Not` : Ensures value is not valid for all given types
|
414
|
+
- `Rtype::not(*types)`, `Rtype::Behavior::Not[*types]`
|
415
|
+
- or `Object#not`
|
416
|
+
|
417
|
+
- `Nilable` : Value can be nil
|
418
|
+
- `Rtype::nilable(type)`, `Rtype::Behavior::Nilable[type]`
|
419
|
+
- or `Object#nilable`, `Object#or_nil`
|
420
|
+
|
421
|
+
- You can create custom behaviors by extending `Rtype::Behavior::Base`
|
422
|
+
|
427
423
|
## Documentation
|
428
424
|
[RubyDoc.info](http://www.rubydoc.info/gems/rtype)
|
429
425
|
|
data/benchmark/benchmark.rb
CHANGED
@@ -19,6 +19,14 @@ puts "Sig version: #{Sig::VERSION}"
|
|
19
19
|
puts "Contracts version: #{Contracts::VERSION}"
|
20
20
|
puts "Typecheck version: #{Typecheck::VERSION}"
|
21
21
|
|
22
|
+
if !Rtype::NATIVE_EXT_VERSION.nil?
|
23
|
+
puts "Rtype with native extension"
|
24
|
+
elsif !Rtype::JAVA_EXT_VERSION.nil?
|
25
|
+
puts "Rtype with java extension"
|
26
|
+
else
|
27
|
+
puts "Rtype without native extension"
|
28
|
+
end
|
29
|
+
|
22
30
|
class PureTest
|
23
31
|
def sum(x, y)
|
24
32
|
x + y
|
@@ -189,4 +197,4 @@ Benchmark.ips do |x|
|
|
189
197
|
end
|
190
198
|
|
191
199
|
x.compare!
|
192
|
-
end
|
200
|
+
end
|
data/lib/rtype.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
if
|
1
|
+
if Object.const_defined?(:RUBY_ENGINE)
|
2
2
|
case RUBY_ENGINE
|
3
3
|
when "jruby"
|
4
4
|
begin
|
@@ -220,7 +220,7 @@ module Rtype
|
|
220
220
|
# Checks the arguments type signature is valid
|
221
221
|
#
|
222
222
|
# e.g.
|
223
|
-
# `[Integer]`, `{key: "value"} are valid.
|
223
|
+
# `[Integer]`, `{key: "value"}` are valid.
|
224
224
|
# `Integer` is invalid
|
225
225
|
#
|
226
226
|
# @param sig A arguments type signature
|
@@ -400,22 +400,27 @@ module Rtype
|
|
400
400
|
private
|
401
401
|
# @param owner
|
402
402
|
# @param [Symbol] method_name
|
403
|
-
# @param expected_args
|
404
|
-
# @param expected_kwargs
|
403
|
+
# @param [Array] expected_args
|
404
|
+
# @param [Hash] expected_kwargs
|
405
405
|
# @param return_sig
|
406
406
|
# @return [void]
|
407
407
|
def define_typed_method_to_proxy(owner, method_name, expected_args, expected_kwargs, return_sig)
|
408
|
-
|
409
|
-
|
410
|
-
|
408
|
+
if expected_kwargs.empty?
|
409
|
+
# `send` is faster than `method(...).call`
|
410
|
+
owner.send(:_rtype_proxy).send :define_method, method_name do |*args, &block|
|
411
411
|
::Rtype::assert_arguments_type(expected_args, args)
|
412
412
|
result = super(*args, &block)
|
413
|
-
|
413
|
+
::Rtype::assert_return_type(return_sig, result)
|
414
|
+
result
|
415
|
+
end
|
416
|
+
else
|
417
|
+
# `send` is faster than `method(...).call`
|
418
|
+
owner.send(:_rtype_proxy).send :define_method, method_name do |*args, **kwargs, &block|
|
414
419
|
::Rtype::assert_arguments_type_with_keywords(expected_args, args, expected_kwargs, kwargs)
|
415
420
|
result = super(*args, **kwargs, &block)
|
421
|
+
::Rtype::assert_return_type(return_sig, result)
|
422
|
+
result
|
416
423
|
end
|
417
|
-
::Rtype::assert_return_type(return_sig, result)
|
418
|
-
result
|
419
424
|
end
|
420
425
|
nil
|
421
426
|
end
|
data/lib/rtype/version.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Rtype
|
2
|
-
VERSION = "0.6.
|
2
|
+
VERSION = "0.6.5".freeze
|
3
3
|
# rtype java extension version. nil If the extension is not used
|
4
|
-
JAVA_EXT_VERSION = nil unless
|
4
|
+
JAVA_EXT_VERSION = nil unless const_defined?(:JAVA_EXT_VERSION, false)
|
5
5
|
# rtype c extension version. nil If the extension is not used
|
6
|
-
NATIVE_EXT_VERSION = nil unless
|
6
|
+
NATIVE_EXT_VERSION = nil unless const_defined?(:NATIVE_EXT_VERSION, false)
|
7
7
|
end
|
data/spec/rtype_spec.rb
CHANGED
@@ -274,12 +274,12 @@ describe Rtype do
|
|
274
274
|
describe 'Hash' do
|
275
275
|
it "is right" do
|
276
276
|
klass.send :rtype, :return_arg, [{k: Integer}, {}] => Any
|
277
|
-
instance.return_arg({k: 123}
|
277
|
+
instance.return_arg({k: 123})
|
278
278
|
end
|
279
279
|
it "is wrong args" do
|
280
280
|
klass.send :rtype, :return_arg, [{k: Integer}, {}] => Any
|
281
281
|
expect {
|
282
|
-
instance.return_arg({k: "str"}
|
282
|
+
instance.return_arg({k: "str"})
|
283
283
|
}.to raise_error Rtype::ArgumentTypeError
|
284
284
|
end
|
285
285
|
it "is wrong result" do
|
@@ -556,15 +556,15 @@ describe Rtype do
|
|
556
556
|
|
557
557
|
it 'two hash' do
|
558
558
|
klass.send :rtype, :two_args, [{k: Integer}, {k: Integer}, {}] => Any
|
559
|
-
instance.two_args({k: 123}, {k: 456}
|
559
|
+
instance.two_args({k: 123}, {k: 456})
|
560
560
|
expect {
|
561
|
-
instance.two_args({k: 123}, {}
|
561
|
+
instance.two_args({k: 123}, {})
|
562
562
|
}.to raise_error Rtype::ArgumentTypeError
|
563
563
|
expect {
|
564
|
-
instance.two_args({k: 123}, 456
|
564
|
+
instance.two_args({k: 123}, 456)
|
565
565
|
}.to raise_error Rtype::ArgumentTypeError
|
566
566
|
expect {
|
567
|
-
instance.two_args({k: 123}, {k: "str"}
|
567
|
+
instance.two_args({k: 123}, {k: "str"})
|
568
568
|
}.to raise_error Rtype::ArgumentTypeError
|
569
569
|
end
|
570
570
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rtype
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sputnik Gugja
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|