typed_attr 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/typed_attr.rb +5 -5
- data/lib/typed_attr/composite_type.rb +28 -7
- data/lib/typed_attr/version.rb +1 -1
- data/spec/lib/typed_attr/composite_type_spec.rb +17 -0
- metadata +3 -3
data/lib/typed_attr.rb
CHANGED
@@ -37,11 +37,11 @@ module TypedAttr
|
|
37
37
|
names = h.keys
|
38
38
|
name_to_type = h
|
39
39
|
else
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
name_to_type = Hash[*types_and_names.reverse]
|
41
|
+
names =
|
42
|
+
(0 .. types_and_names.size).to_a.
|
43
|
+
keep_if(&:odd?).
|
44
|
+
map { | i | types_and_names[i] }
|
45
45
|
end
|
46
46
|
expr = <<"END"
|
47
47
|
def initialize *__args
|
@@ -6,16 +6,15 @@ class Module
|
|
6
6
|
end
|
7
7
|
def _a; @a; end
|
8
8
|
def _b; @b; end
|
9
|
-
def to_s
|
10
|
-
@to_s ||= "#{@a}.#{op}(#{@b})".freeze
|
11
|
-
end
|
12
9
|
end
|
13
10
|
|
14
11
|
class ContainerType < CompositeType
|
15
12
|
def === x
|
16
13
|
@a === x and x.all?{|e| @b === e }
|
17
14
|
end
|
18
|
-
def
|
15
|
+
def to_s
|
16
|
+
@to_s ||= "#{@a}.of(#{@b})".freeze
|
17
|
+
end
|
19
18
|
end
|
20
19
|
|
21
20
|
# Constructs a type of Enumeration of an element type.
|
@@ -29,7 +28,9 @@ class Module
|
|
29
28
|
def === x
|
30
29
|
Enumerable === x and @a === x[0] and @b === x[1]
|
31
30
|
end
|
32
|
-
def
|
31
|
+
def to_s
|
32
|
+
@to_s ||= "#{@a}.with(#{@b})".freeze
|
33
|
+
end
|
33
34
|
end
|
34
35
|
|
35
36
|
# Constructs a type of Pairs.
|
@@ -73,7 +74,7 @@ class Module
|
|
73
74
|
|
74
75
|
class NegativeType < CompositeType
|
75
76
|
def === x
|
76
|
-
! @a === x
|
77
|
+
! (@a === x)
|
77
78
|
end
|
78
79
|
def to_s
|
79
80
|
@to_s ||= "(~#{@a})".freeze
|
@@ -86,14 +87,16 @@ class Module
|
|
86
87
|
def ~@
|
87
88
|
case self
|
88
89
|
when NegativeType
|
89
|
-
self.
|
90
|
+
self._a
|
90
91
|
else
|
91
92
|
NegativeType.new(self)
|
92
93
|
end
|
93
94
|
end
|
94
95
|
end
|
95
96
|
|
97
|
+
# Numeric origin/continuum types.
|
96
98
|
|
99
|
+
# Objects that are Numeric or respond to :to_numeric.
|
97
100
|
module Numericlike
|
98
101
|
def self.=== x
|
99
102
|
case
|
@@ -105,18 +108,36 @@ module Numericlike
|
|
105
108
|
end
|
106
109
|
end
|
107
110
|
|
111
|
+
# Objects that are Numericlike and > 0.
|
108
112
|
module Positive
|
109
113
|
def self.=== x
|
110
114
|
n = Numericlike === x and n > 0
|
111
115
|
end
|
112
116
|
end
|
113
117
|
|
118
|
+
# Objects that are Numericlike and < 0.
|
114
119
|
module Negative
|
115
120
|
def self.=== x
|
116
121
|
n = Numericlike === x and n < 0
|
117
122
|
end
|
118
123
|
end
|
119
124
|
|
125
|
+
# Objects that are Numericlike and <= 0.
|
126
|
+
module NonPositive
|
127
|
+
def self.=== x
|
128
|
+
n = Numericlike === x and n <= 0
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Objects that are Numericlike and >= 0.
|
133
|
+
module NonNegative
|
134
|
+
def self.=== x
|
135
|
+
n = Numericlike === x and n >= 0
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# Objects that can do IO.
|
140
|
+
#
|
120
141
|
# Note: IO and StringIO do not share a common ancestor Module
|
121
142
|
# that distingushes them as being capable of "IO".
|
122
143
|
# So we create one here -- devdriven.com 2013/11/14
|
data/lib/typed_attr/version.rb
CHANGED
@@ -130,6 +130,23 @@ describe Class::CompositeType do
|
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
|
+
context "NegativeType" do
|
134
|
+
it "~~A == A" do
|
135
|
+
a = Class.new
|
136
|
+
(~ ~ a).should == a
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should be true for match" do
|
140
|
+
v = [ 1, :symbol ]
|
141
|
+
(Array.of(~NilClass) === v).should === true
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should be false for match" do
|
145
|
+
v = [ 1, nil, :symbol ]
|
146
|
+
(Array.of(~NilClass) === v).should === false
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
133
150
|
context "Negative" do
|
134
151
|
it "should be true for negative Numeric" do
|
135
152
|
v = -1234
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typed_attr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -144,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
144
|
version: '0'
|
145
145
|
segments:
|
146
146
|
- 0
|
147
|
-
hash:
|
147
|
+
hash: -3017759551049438692
|
148
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
149
|
none: false
|
150
150
|
requirements:
|
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
153
|
version: '0'
|
154
154
|
segments:
|
155
155
|
- 0
|
156
|
-
hash:
|
156
|
+
hash: -3017759551049438692
|
157
157
|
requirements: []
|
158
158
|
rubyforge_project:
|
159
159
|
rubygems_version: 1.8.25
|