typestrict 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/README +28 -4
- data/README.txt +1 -1
- data/lib/typestrict.rb +35 -15
- metadata +4 -4
data/History.txt
CHANGED
data/README
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
strict is a rudimentary static typesystem for ruby objects - it
|
1
|
+
strict is a rudimentary static typesystem for ruby objects - it enables easy validation of individual ruby objects and Hash sets.
|
2
2
|
|
3
3
|
For an example of validation:
|
4
4
|
|
@@ -12,12 +12,12 @@ class Rectangle
|
|
12
12
|
validate_map!({
|
13
13
|
:width => :natural_number,
|
14
14
|
:height => :natural_number,
|
15
|
-
:
|
15
|
+
:color => :hex_color
|
16
16
|
}, p)
|
17
17
|
|
18
18
|
@width = p[:width]
|
19
19
|
@height = p[:height]
|
20
|
-
@
|
20
|
+
@color = p[:color]
|
21
21
|
end
|
22
22
|
|
23
23
|
def set_dimensions(dimensions)
|
@@ -40,11 +40,35 @@ Strict defines a number of 'super-types', currently which are:
|
|
40
40
|
:natural_number
|
41
41
|
:float
|
42
42
|
:boolean
|
43
|
+
:integer
|
44
|
+
:numeric
|
45
|
+
:character
|
46
|
+
:procedure
|
43
47
|
:string_array
|
44
48
|
:numeric_array
|
45
49
|
:float_array
|
46
50
|
:integer_array
|
47
|
-
:
|
51
|
+
:hex_color
|
52
|
+
|
53
|
+
New supertypes may be registered with the register_supertype as follows:
|
54
|
+
|
55
|
+
register_supertype(:char, proc {|data, context| enforce_primitive!(String, data); catch_error("#{data.inspect} must be a single character in length") unless (data.size == 1)})
|
56
|
+
registered a dynamic handler for supertype: character
|
57
|
+
=> nil
|
58
|
+
|
59
|
+
irb(main)> enforce!(:char, "a")
|
60
|
+
=> "a"
|
61
|
+
irb(main)> enforce!(:char, "aa")
|
62
|
+
Strict::TypeError: TypeError caught:
|
63
|
+
"aa" must be a single character in length
|
64
|
+
|
65
|
+
from (irb):6
|
66
|
+
from ./str.rb:148:in `call'
|
67
|
+
from ./str.rb:148:in `enforce!'
|
68
|
+
from (irb):8
|
69
|
+
|
70
|
+
irb(main):009:0> enforce!(:char, "x")
|
71
|
+
=> "x"
|
48
72
|
|
49
73
|
Strict simply implements an extendable static type-checker over dynamic ruby objects. For an example of validation with troublesome datasets:
|
50
74
|
|
data/README.txt
CHANGED
data/lib/typestrict.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Strict
|
2
|
-
VERSION = '0.0.
|
3
|
-
|
2
|
+
VERSION = '0.0.5'
|
3
|
+
|
4
4
|
class TypeError < Exception
|
5
5
|
attr :errors
|
6
6
|
def initialize(error_list)
|
7
|
-
enforce!(:string_array, error_list, 'lib/
|
7
|
+
enforce!(:string_array, error_list, 'lib/typestrict')
|
8
8
|
@errors = error_list
|
9
9
|
end
|
10
10
|
|
@@ -19,6 +19,7 @@ module Strict
|
|
19
19
|
|
20
20
|
@@errors = []
|
21
21
|
@@raise_exception = true
|
22
|
+
@@dynamic_handlers = {}
|
22
23
|
|
23
24
|
def setmode_raise!
|
24
25
|
@@errors = []
|
@@ -41,6 +42,14 @@ module Strict
|
|
41
42
|
raise(t, t.inspect, caller) if @@errors.size > 0
|
42
43
|
end
|
43
44
|
|
45
|
+
def register_supertype(supertype, handler)
|
46
|
+
enforce_primitive!(Symbol, supertype)
|
47
|
+
enforce_primitive!(Proc, handler)
|
48
|
+
|
49
|
+
@@dynamic_handlers[supertype] = handler
|
50
|
+
puts "registered a dynamic handler for supertype: #{supertype}"
|
51
|
+
end
|
52
|
+
|
44
53
|
def header(context, data)
|
45
54
|
"#{context}; #{data.class.inspect}::#{data.inspect}"
|
46
55
|
end
|
@@ -51,8 +60,8 @@ module Strict
|
|
51
60
|
end
|
52
61
|
|
53
62
|
def enforce_strict_primitives!(types, data, context="Value")
|
54
|
-
enforce_primitive!(Array, types, 'lib/
|
55
|
-
types.each {|type| enforce_primitive!(Object, type, 'lib/
|
63
|
+
enforce_primitive!(Array, types, 'lib/typestrict')
|
64
|
+
types.each {|type| enforce_primitive!(Object, type, 'lib/typestrict')}
|
56
65
|
types.each do |type|
|
57
66
|
enforce_primitive!(type, data, context)
|
58
67
|
end
|
@@ -60,8 +69,8 @@ module Strict
|
|
60
69
|
end
|
61
70
|
|
62
71
|
def enforce_weak_primitives!(types, data, context="Value")
|
63
|
-
enforce_primitive!(Array, types, 'lib/
|
64
|
-
types.each {|type| enforce_primitive!(Object, type, 'lib/
|
72
|
+
enforce_primitive!(Array, types, 'lib/typestrict')
|
73
|
+
types.each {|type| enforce_primitive!(Object, type, 'lib/typestrict')}
|
65
74
|
|
66
75
|
match_found = false
|
67
76
|
types.each do |type|
|
@@ -94,11 +103,6 @@ module Strict
|
|
94
103
|
enforce_primitive!(String, data, context)
|
95
104
|
catch_error "#{header(context, data)} can't be empty string" unless (data.size > 0)
|
96
105
|
|
97
|
-
when :hex_color
|
98
|
-
enforce!(:string, data, context)
|
99
|
-
catch_error "#{header(context, data)} must be six characters long" unless (data.size == 6)
|
100
|
-
data.upcase.each_byte {|c| catch_error "#{header(context, data)} must contain only hexadecimal characters" unless ((48 <= c and c <= 57) or (65 <= c and c <= 70))}
|
101
|
-
|
102
106
|
when :natural_number
|
103
107
|
enforce_primitive!(Fixnum, data, context)
|
104
108
|
catch_error "#{header(context, data)} must be > 0" unless (data > 0)
|
@@ -115,6 +119,13 @@ module Strict
|
|
115
119
|
when :float
|
116
120
|
enforce_primitive!(Float, data, context)
|
117
121
|
|
122
|
+
when :character
|
123
|
+
enforce!(:string, data, context)
|
124
|
+
catch_error "#{header(context, data)} must be a single character!" unless data.size == 1
|
125
|
+
|
126
|
+
when :procedure
|
127
|
+
enforce_primitive!(Proc, data, context)
|
128
|
+
|
118
129
|
when :string_array
|
119
130
|
enforce_primitive!(Array, data, context)
|
120
131
|
data.each {|item| enforce_primitive!(String, item, context)}
|
@@ -131,8 +142,17 @@ module Strict
|
|
131
142
|
enforce_primitive!(Array, data, context)
|
132
143
|
data.each {|item| enforce_primitive!(Fixnum, item, context)}
|
133
144
|
|
145
|
+
when :hex_color
|
146
|
+
enforce!(:string, data, context)
|
147
|
+
catch_error "#{header(context, data)} must be six characters long" unless (data.size == 6)
|
148
|
+
data.upcase.each_byte {|c| catch_error "#{header(context, data)} must contain only hexadecimal characters" unless ((48 <= c and c <= 57) or (65 <= c and c <= 70))}
|
149
|
+
|
134
150
|
else
|
135
|
-
|
151
|
+
if @@dynamic_handlers.has_key? supertype
|
152
|
+
@@dynamic_handlers[supertype].call(data, context="Value")
|
153
|
+
else
|
154
|
+
catch_error "undefined symbol-supertype encountered: #{supertype.inspect}"
|
155
|
+
end
|
136
156
|
end
|
137
157
|
end
|
138
158
|
end
|
@@ -140,8 +160,8 @@ module Strict
|
|
140
160
|
end
|
141
161
|
|
142
162
|
def enforce_map!(matrix, map)
|
143
|
-
enforce_primitive!(Hash, matrix, "lib/
|
144
|
-
enforce_primitive!(Hash, map, "lib/
|
163
|
+
enforce_primitive!(Hash, matrix, "lib/typestrict")
|
164
|
+
enforce_primitive!(Hash, map, "lib/typestrict")
|
145
165
|
|
146
166
|
setmode_catch!
|
147
167
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typestrict
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Raeez Lorgat
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-07 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|