rtype 0.5.0 → 0.5.1
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/Gemfile +2 -2
- data/LICENSE +8 -8
- data/README.md +567 -567
- data/Rakefile +11 -11
- data/benchmark/benchmark.rb +191 -191
- data/lib/rtype/argument_type_error.rb +3 -3
- data/lib/rtype/behavior/and.rb +23 -23
- data/lib/rtype/behavior/base.rb +16 -16
- data/lib/rtype/behavior/core_ext.rb +21 -21
- data/lib/rtype/behavior/nilable.rb +20 -20
- data/lib/rtype/behavior/not.rb +23 -23
- data/lib/rtype/behavior/or.rb +23 -23
- data/lib/rtype/behavior/xor.rb +24 -24
- data/lib/rtype/behavior.rb +11 -11
- data/lib/rtype/core_ext.rb +98 -98
- data/lib/rtype/method_annotator.rb +30 -30
- data/lib/rtype/return_type_error.rb +3 -3
- data/lib/rtype/rtype_proxy.rb +9 -9
- data/lib/rtype/type_signature.rb +8 -8
- data/lib/rtype/type_signature_error.rb +3 -3
- data/lib/rtype/version.rb +3 -3
- data/lib/rtype.rb +324 -321
- data/spec/rtype_spec.rb +822 -822
- data/spec/spec_helper.rb +4 -4
- metadata +4 -3
data/lib/rtype/behavior/xor.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
1
|
-
module Rtype
|
2
|
-
module Behavior
|
3
|
-
class Xor < Base
|
4
|
-
def initialize(*types)
|
5
|
-
@types = types
|
6
|
-
end
|
7
|
-
|
8
|
-
def valid?(value)
|
9
|
-
result = @types.map do |e|
|
10
|
-
Rtype::valid? e, value
|
11
|
-
end
|
12
|
-
result.count(true) == 1
|
13
|
-
end
|
14
|
-
|
15
|
-
def error_message(value)
|
16
|
-
arr = @types.map { |e| Rtype::type_error_message(e, value) }
|
17
|
-
arr.join "\nXOR "
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def xor(*args)
|
23
|
-
Behavior::Xor[*args]
|
24
|
-
end
|
1
|
+
module Rtype
|
2
|
+
module Behavior
|
3
|
+
class Xor < Base
|
4
|
+
def initialize(*types)
|
5
|
+
@types = types
|
6
|
+
end
|
7
|
+
|
8
|
+
def valid?(value)
|
9
|
+
result = @types.map do |e|
|
10
|
+
Rtype::valid? e, value
|
11
|
+
end
|
12
|
+
result.count(true) == 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def error_message(value)
|
16
|
+
arr = @types.map { |e| Rtype::type_error_message(e, value) }
|
17
|
+
arr.join "\nXOR "
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def xor(*args)
|
23
|
+
Behavior::Xor[*args]
|
24
|
+
end
|
25
25
|
end
|
data/lib/rtype/behavior.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
module Rtype
|
2
|
-
module Behavior
|
3
|
-
end
|
4
|
-
end
|
5
|
-
|
6
|
-
require_relative 'behavior/base'
|
7
|
-
require_relative 'behavior/or'
|
8
|
-
require_relative 'behavior/and'
|
9
|
-
require_relative 'behavior/xor'
|
10
|
-
require_relative 'behavior/not'
|
11
|
-
require_relative 'behavior/nilable'
|
1
|
+
module Rtype
|
2
|
+
module Behavior
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
require_relative 'behavior/base'
|
7
|
+
require_relative 'behavior/or'
|
8
|
+
require_relative 'behavior/and'
|
9
|
+
require_relative 'behavior/xor'
|
10
|
+
require_relative 'behavior/not'
|
11
|
+
require_relative 'behavior/nilable'
|
12
12
|
require_relative 'behavior/core_ext'
|
data/lib/rtype/core_ext.rb
CHANGED
@@ -1,99 +1,99 @@
|
|
1
|
-
module Boolean; end
|
2
|
-
class TrueClass; include Boolean; end
|
3
|
-
class FalseClass; include Boolean; end
|
4
|
-
Any = BasicObject
|
5
|
-
|
6
|
-
class Object
|
7
|
-
include ::Rtype::MethodAnnotator
|
8
|
-
end
|
9
|
-
|
10
|
-
module Kernel
|
11
|
-
private
|
12
|
-
def _rtype_proxy
|
13
|
-
unless @_rtype_proxy
|
14
|
-
@_rtype_proxy = ::Rtype::RtypeProxy.new
|
15
|
-
prepend @_rtype_proxy
|
16
|
-
end
|
17
|
-
@_rtype_proxy
|
18
|
-
end
|
19
|
-
|
20
|
-
def rtype(method_name=nil, type_sig_info)
|
21
|
-
if is_a?(Module)
|
22
|
-
if method_name.nil?
|
23
|
-
::Rtype::assert_valid_type_sig(type_sig_info)
|
24
|
-
_rtype_proxy.annotation_mode = true
|
25
|
-
_rtype_proxy.annotation_type_sig = type_sig_info
|
26
|
-
else
|
27
|
-
::Rtype::define_typed_method(self, method_name, type_sig_info)
|
28
|
-
end
|
29
|
-
else
|
30
|
-
if method_name.nil?
|
31
|
-
raise ArgumentError, "Annotation mode not working out of module"
|
32
|
-
else
|
33
|
-
rtype_self(method_name, type_sig_info)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def rtype_self(method_name, type_sig_info)
|
39
|
-
::Rtype.define_typed_method(singleton_class, method_name, type_sig_info)
|
40
|
-
end
|
41
|
-
|
42
|
-
def rtype_accessor(accessor_name, type_behavior)
|
43
|
-
accessor_name = accessor_name.to_sym
|
44
|
-
if !respond_to?(accessor_name) || !respond_to?(:"#{accessor_name}=")
|
45
|
-
attr_accessor accessor_name
|
46
|
-
end
|
47
|
-
|
48
|
-
if is_a?(Module)
|
49
|
-
::Rtype::define_typed_accessor(self, accessor_name, type_behavior)
|
50
|
-
else
|
51
|
-
rtype_accessor_self(accessor_name, type_behavior)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def rtype_accessor_self(accessor_name, type_behavior)
|
56
|
-
accessor_name = accessor_name.to_sym
|
57
|
-
if !respond_to?(accessor_name) || !respond_to?(:"#{accessor_name}=")
|
58
|
-
singleton_class.send(:attr_accessor, accessor_name)
|
59
|
-
end
|
60
|
-
::Rtype::define_typed_accessor(singleton_class, accessor_name, type_behavior)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
class Method
|
65
|
-
def typed?
|
66
|
-
!!::Rtype.type_signatures[owner][name]
|
67
|
-
end
|
68
|
-
|
69
|
-
def type_signature
|
70
|
-
::Rtype.type_signatures[owner][name]
|
71
|
-
end
|
72
|
-
|
73
|
-
def type_info
|
74
|
-
::Rtype.type_signatures[owner][name].info
|
75
|
-
end
|
76
|
-
|
77
|
-
def argument_type
|
78
|
-
::Rtype.type_signatures[owner][name].argument_type
|
79
|
-
end
|
80
|
-
|
81
|
-
def return_type
|
82
|
-
::Rtype.type_signatures[owner][name].return_type
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
class Fixnum
|
87
|
-
def ordinalize
|
88
|
-
if (11..13).include?(self % 100)
|
89
|
-
"#{self}th"
|
90
|
-
else
|
91
|
-
case self % 10
|
92
|
-
when 1; "#{self}st"
|
93
|
-
when 2; "#{self}nd"
|
94
|
-
when 3; "#{self}rd"
|
95
|
-
else "#{self}th"
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
1
|
+
module Boolean; end
|
2
|
+
class TrueClass; include Boolean; end
|
3
|
+
class FalseClass; include Boolean; end
|
4
|
+
Any = BasicObject
|
5
|
+
|
6
|
+
class Object
|
7
|
+
include ::Rtype::MethodAnnotator
|
8
|
+
end
|
9
|
+
|
10
|
+
module Kernel
|
11
|
+
private
|
12
|
+
def _rtype_proxy
|
13
|
+
unless @_rtype_proxy
|
14
|
+
@_rtype_proxy = ::Rtype::RtypeProxy.new
|
15
|
+
prepend @_rtype_proxy
|
16
|
+
end
|
17
|
+
@_rtype_proxy
|
18
|
+
end
|
19
|
+
|
20
|
+
def rtype(method_name=nil, type_sig_info)
|
21
|
+
if is_a?(Module)
|
22
|
+
if method_name.nil?
|
23
|
+
::Rtype::assert_valid_type_sig(type_sig_info)
|
24
|
+
_rtype_proxy.annotation_mode = true
|
25
|
+
_rtype_proxy.annotation_type_sig = type_sig_info
|
26
|
+
else
|
27
|
+
::Rtype::define_typed_method(self, method_name, type_sig_info)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
if method_name.nil?
|
31
|
+
raise ArgumentError, "Annotation mode not working out of module"
|
32
|
+
else
|
33
|
+
rtype_self(method_name, type_sig_info)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def rtype_self(method_name, type_sig_info)
|
39
|
+
::Rtype.define_typed_method(singleton_class, method_name, type_sig_info)
|
40
|
+
end
|
41
|
+
|
42
|
+
def rtype_accessor(accessor_name, type_behavior)
|
43
|
+
accessor_name = accessor_name.to_sym
|
44
|
+
if !respond_to?(accessor_name) || !respond_to?(:"#{accessor_name}=")
|
45
|
+
attr_accessor accessor_name
|
46
|
+
end
|
47
|
+
|
48
|
+
if is_a?(Module)
|
49
|
+
::Rtype::define_typed_accessor(self, accessor_name, type_behavior)
|
50
|
+
else
|
51
|
+
rtype_accessor_self(accessor_name, type_behavior)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def rtype_accessor_self(accessor_name, type_behavior)
|
56
|
+
accessor_name = accessor_name.to_sym
|
57
|
+
if !respond_to?(accessor_name) || !respond_to?(:"#{accessor_name}=")
|
58
|
+
singleton_class.send(:attr_accessor, accessor_name)
|
59
|
+
end
|
60
|
+
::Rtype::define_typed_accessor(singleton_class, accessor_name, type_behavior)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class Method
|
65
|
+
def typed?
|
66
|
+
!!::Rtype.type_signatures[owner][name]
|
67
|
+
end
|
68
|
+
|
69
|
+
def type_signature
|
70
|
+
::Rtype.type_signatures[owner][name]
|
71
|
+
end
|
72
|
+
|
73
|
+
def type_info
|
74
|
+
::Rtype.type_signatures[owner][name].info
|
75
|
+
end
|
76
|
+
|
77
|
+
def argument_type
|
78
|
+
::Rtype.type_signatures[owner][name].argument_type
|
79
|
+
end
|
80
|
+
|
81
|
+
def return_type
|
82
|
+
::Rtype.type_signatures[owner][name].return_type
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class Fixnum
|
87
|
+
def ordinalize
|
88
|
+
if (11..13).include?(self % 100)
|
89
|
+
"#{self}th"
|
90
|
+
else
|
91
|
+
case self % 10
|
92
|
+
when 1; "#{self}st"
|
93
|
+
when 2; "#{self}nd"
|
94
|
+
when 3; "#{self}rd"
|
95
|
+
else "#{self}th"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
99
|
end
|
@@ -1,31 +1,31 @@
|
|
1
|
-
module Rtype
|
2
|
-
module MethodAnnotator
|
3
|
-
def self.included(base)
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def method_added(name)
|
9
|
-
if @_rtype_proxy
|
10
|
-
proxy = _rtype_proxy
|
11
|
-
if proxy.annotation_mode
|
12
|
-
::Rtype::define_typed_method(self, name, proxy.annotation_type_sig)
|
13
|
-
proxy.annotation_mode = false
|
14
|
-
proxy.annotation_type_sig = nil
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def singleton_method_added(name)
|
20
|
-
if @_rtype_proxy
|
21
|
-
proxy = _rtype_proxy
|
22
|
-
if proxy.annotation_mode
|
23
|
-
::Rtype::define_typed_method(singleton_class, name, proxy.annotation_type_sig)
|
24
|
-
proxy.annotation_mode = false
|
25
|
-
proxy.annotation_type_sig = nil
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
1
|
+
module Rtype
|
2
|
+
module MethodAnnotator
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def method_added(name)
|
9
|
+
if @_rtype_proxy
|
10
|
+
proxy = _rtype_proxy
|
11
|
+
if proxy.annotation_mode
|
12
|
+
::Rtype::define_typed_method(self, name, proxy.annotation_type_sig)
|
13
|
+
proxy.annotation_mode = false
|
14
|
+
proxy.annotation_type_sig = nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def singleton_method_added(name)
|
20
|
+
if @_rtype_proxy
|
21
|
+
proxy = _rtype_proxy
|
22
|
+
if proxy.annotation_mode
|
23
|
+
::Rtype::define_typed_method(singleton_class, name, proxy.annotation_type_sig)
|
24
|
+
proxy.annotation_mode = false
|
25
|
+
proxy.annotation_type_sig = nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
31
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Rtype
|
2
|
-
class ReturnTypeError < StandardError
|
3
|
-
end
|
1
|
+
module Rtype
|
2
|
+
class ReturnTypeError < StandardError
|
3
|
+
end
|
4
4
|
end
|
data/lib/rtype/rtype_proxy.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
module Rtype
|
2
|
-
class RtypeProxy < Module
|
3
|
-
attr_accessor :annotation_mode
|
4
|
-
attr_accessor :annotation_type_sig
|
5
|
-
def initialize
|
6
|
-
@annotation_mode = false
|
7
|
-
@annotation_type_sig = nil
|
8
|
-
end
|
9
|
-
end
|
1
|
+
module Rtype
|
2
|
+
class RtypeProxy < Module
|
3
|
+
attr_accessor :annotation_mode
|
4
|
+
attr_accessor :annotation_type_sig
|
5
|
+
def initialize
|
6
|
+
@annotation_mode = false
|
7
|
+
@annotation_type_sig = nil
|
8
|
+
end
|
9
|
+
end
|
10
10
|
end
|
data/lib/rtype/type_signature.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
module Rtype
|
2
|
-
class TypeSignature
|
3
|
-
attr_accessor :argument_type, :return_type
|
4
|
-
|
5
|
-
def info
|
6
|
-
{argument_type => return_type}
|
7
|
-
end
|
8
|
-
end
|
1
|
+
module Rtype
|
2
|
+
class TypeSignature
|
3
|
+
attr_accessor :argument_type, :return_type
|
4
|
+
|
5
|
+
def info
|
6
|
+
{argument_type => return_type}
|
7
|
+
end
|
8
|
+
end
|
9
9
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Rtype
|
2
|
-
class TypeSignatureError < ArgumentError
|
3
|
-
end
|
1
|
+
module Rtype
|
2
|
+
class TypeSignatureError < ArgumentError
|
3
|
+
end
|
4
4
|
end
|
data/lib/rtype/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Rtype
|
2
|
-
VERSION = "0.5.
|
3
|
-
end
|
1
|
+
module Rtype
|
2
|
+
VERSION = "0.5.1".freeze
|
3
|
+
end
|