typestrict 0.0.11 → 0.0.12

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.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 0.0.12 / 2010-08-16
2
+ * 3 major enhancements
3
+
4
+ * Introduced new supertypes: :uri, :uri_array, :procedure, :hash_map
5
+ * errors caught within a supertype's definition are now returned under setmode_catch! policy
6
+ * Introduced new policies: enforce_defaults!, enforce_map_optional
7
+
1
8
  === 0.0.9 / 2010-08-08
2
9
  * 1 major enhancement
3
10
 
@@ -15,7 +22,6 @@
15
22
  * Revamped implementation and architecture
16
23
  * Supertypes are now implemented using dynamic handlers + dynamic handlers
17
24
 
18
-
19
25
  === 0.0.5 / 2010-08-07
20
26
  * 2 major enhancements
21
27
 
data/lib/base.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Strict
2
2
  module Base
3
3
 
4
- class StrictTypeError < Exception
4
+ class TypeError < Exception
5
5
  attr :errors
6
6
  def initialize(error_list)
7
7
  enforce_primitive!(Array, error_list, "lib/typestrict")
@@ -10,7 +10,7 @@ module Strict
10
10
  end
11
11
 
12
12
  def inspect
13
- msg = "StrictTypeError#{@errors.size > 1 ? "s" : ""} caught:\n"
13
+ msg = "TypeError#{@errors.size > 1 ? "s" : ""} caught:\n"
14
14
  @errors.each do |e|
15
15
  msg += "#{e}\n"
16
16
  end
@@ -33,7 +33,7 @@ module Strict
33
33
 
34
34
  def catch_error error
35
35
  if @@raise_exception
36
- t = StrictTypeError.new([error])
36
+ t = TypeError.new([error])
37
37
  raise(t, t.inspect, caller)
38
38
  else
39
39
  @@errors << error
@@ -42,7 +42,7 @@ module Strict
42
42
 
43
43
  def raise_hell!
44
44
  if @@errors.size > 0
45
- t = StrictTypeError.new(@@errors)
45
+ t = TypeError.new(@@errors)
46
46
  setmode_raise!
47
47
  raise(t, t.inspect, caller)
48
48
  end
@@ -57,7 +57,7 @@ module Strict
57
57
  end
58
58
 
59
59
  def header(context, data)
60
- "#{context}; #{data.class.inspect}::#{data.inspect}"
60
+ "#{context}> #{data.class.inspect}::#{data.inspect}"
61
61
  end
62
62
 
63
63
  def enforce_primitive!(type, data, context="Value")
@@ -124,7 +124,19 @@ module Strict
124
124
  else
125
125
  if supertype.is_a? Symbol #distinct supertype
126
126
  if @@dynamic_handlers.has_key? supertype
127
+
128
+ raise_set_before = @@raise_exception
129
+ previous_errors = @@errors
130
+ setmode_catch!
131
+
127
132
  @@dynamic_handlers[supertype].call(data, context)
133
+
134
+ if raise_set_before
135
+ raise_hell!
136
+ setmode_raise!
137
+ else
138
+ @@errors.concat(previous_errors)
139
+ end
128
140
  else
129
141
  catch_error "undefined symbol-supertype encountered: #{supertype.inspect}"
130
142
  end
@@ -166,5 +178,16 @@ module Strict
166
178
  setmode_raise!
167
179
  return map
168
180
  end
181
+
182
+ def enforce_defaults!(matrix, map)
183
+ enforce_primitive!(Hash, matrix, "lib/typestrict")
184
+ enforce_primitive!(Hash, map, "lib/typestrict")
185
+
186
+ matrix.each do |param, default|
187
+ if !map.has_key? param
188
+ map[param] = default
189
+ end
190
+ end
191
+ end
169
192
  end
170
193
  end
data/lib/supertypes.rb CHANGED
@@ -51,12 +51,21 @@ module Strict
51
51
  enforce_primitive!(Proc, data, context)
52
52
  end
53
53
 
54
+ @@handlers[:hash_map] = proc do |data, context|
55
+ enforce_primitive!(Hash, data, context)
56
+ end
57
+
54
58
  @@handlers[:hex_color] = proc do |data, context|
55
59
  enforce!(:string, data, context)
56
60
  catch_error "#{header(context, data)} must be six characters long" unless (data.size == 6)
57
61
  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))}
58
62
  end
59
63
 
64
+ @@handlers[:uri] = proc do |data, context|
65
+ enforce!(:string, data, context)
66
+ end
67
+
68
+
60
69
  @@handlers[:string_array] = proc do |data, context|
61
70
  enforce_primitive!(Array, data, context)
62
71
  data.each {|item| enforce_primitive!(String, item, context)}
@@ -78,8 +87,13 @@ module Strict
78
87
  end
79
88
 
80
89
  @@handlers[:hex_color_array] = proc do |data, context|
81
- enforce_primitive!(Array, data)
82
- data.each {|item| enforce!(:hex_color, item)}
90
+ enforce_primitive!(Array, data, context)
91
+ data.each {|item| enforce!(:hex_color, item, context)}
92
+ end
93
+
94
+ @@handlers[:uri_array] = proc do |data, context|
95
+ enforce_primitive!(Array, data, context)
96
+ data.each {|item| enforce!(:uri, item, context)}
83
97
  end
84
98
 
85
99
  @@handlers.each do |supertype, handler|
@@ -88,3 +102,6 @@ module Strict
88
102
  end
89
103
  end
90
104
  end
105
+
106
+ # TODO implement uri correctly
107
+ # make catch_error collect in base.rb for supertypes
data/lib/typestrict.rb CHANGED
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), 'base')
2
2
  require File.join(File.dirname(__FILE__), 'supertypes')
3
3
 
4
4
  module Strict
5
- VERSION = '0.0.11'
5
+ VERSION = '0.0.12'
6
6
  include Strict::Base
7
7
  include Strict::SuperTypes
8
8
  end
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: 9
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 11
10
- version: 0.0.11
9
+ - 12
10
+ version: 0.0.12
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-12 00:00:00 -04:00
18
+ date: 2010-08-16 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency