typestrict 0.0.13 → 0.0.15

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.
Files changed (4) hide show
  1. data/lib/base.rb +14 -1
  2. data/lib/supertypes.rb +29 -11
  3. data/lib/typestrict.rb +1 -1
  4. metadata +4 -4
data/lib/base.rb CHANGED
@@ -6,7 +6,8 @@ module Strict
6
6
  def initialize(error_list)
7
7
  enforce_primitive!(Array, error_list, "lib/typestrict")
8
8
  error_list.each {|item| enforce_primitive!(String, item, "lib/typestrict")}
9
- @errors = error_list
9
+
10
+ @errors = error_list & error_list
10
11
  end
11
12
 
12
13
  def inspect
@@ -124,7 +125,19 @@ module Strict
124
125
  else
125
126
  if supertype.is_a? Symbol #distinct supertype
126
127
  if @@dynamic_handlers.has_key? supertype
128
+
129
+ raise_set_before = @@raise_exception
130
+ previous_errors = @@errors
131
+ setmode_catch!
132
+
127
133
  @@dynamic_handlers[supertype].call(data, context)
134
+
135
+ if raise_set_before
136
+ raise_hell!
137
+ setmode_raise!
138
+ else
139
+ @@errors.concat(previous_errors)
140
+ end
128
141
  else
129
142
  catch_error "undefined symbol-supertype encountered: #{supertype.inspect}"
130
143
  end
data/lib/supertypes.rb CHANGED
@@ -18,12 +18,14 @@ module Strict
18
18
 
19
19
  @@handlers[:string] = proc do |data, context|
20
20
  enforce_primitive!(String, data, context)
21
- catch_error "#{header(context, data)} can't be empty string" unless (data.size > 0)
21
+ if data.is_a? String
22
+ catch_error "#{header(context, data)} can't be empty string" unless (data.size > 0)
23
+ end
22
24
  end
23
25
 
24
26
  @@handlers[:natural_number] = proc do |data, context|
25
27
  enforce_primitive!(Fixnum, data, context)
26
- catch_error "#{header(context, data)} must be > 0" unless (data > 0)
28
+ catch_error "#{header(context, data)} must be > 0" unless (data > 0) if data.is_a? Fixnum
27
29
  end
28
30
 
29
31
  @@handlers[:integer] = proc do |data, context|
@@ -44,7 +46,9 @@ module Strict
44
46
 
45
47
  @@handlers[:character] = proc do |data, context|
46
48
  enforce!(:string, data, context)
47
- catch_error "#{header(context, data)} must be a single character!" unless data.size == 1
49
+ if data.is_a? String
50
+ catch_error "#{header(context, data)} must be a single character!" unless data.size == 1
51
+ end
48
52
  end
49
53
 
50
54
  @@handlers[:procedure] = proc do |data, context|
@@ -57,8 +61,10 @@ module Strict
57
61
 
58
62
  @@handlers[:hex_color] = proc do |data, context|
59
63
  enforce!(:string, data, context)
60
- catch_error "#{header(context, data)} must be six characters long" unless (data.size == 6)
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))}
64
+ if data.is_a? String
65
+ catch_error "#{header(context, data)} must be six characters long" unless (data.size == 6)
66
+ 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))}
67
+ end
62
68
  end
63
69
 
64
70
  @@handlers[:uri] = proc do |data, context|
@@ -68,32 +74,44 @@ module Strict
68
74
 
69
75
  @@handlers[:string_array] = proc do |data, context|
70
76
  enforce_primitive!(Array, data, context)
71
- data.each {|item| enforce_primitive!(String, item, context)}
77
+ if data.is_a? Array
78
+ data.each {|item| enforce_primitive!(String, item, context)}
79
+ end
72
80
  end
73
81
 
74
82
  @@handlers[:numeric_array] = proc do |data, context|
75
83
  enforce_primitive!(Array, data, context)
76
- data.each {|item| enforce_primitive!(Numeric, item, context)}
84
+ if data.is_a? Array
85
+ data.each {|item| enforce_primitive!(Numeric, item, context)}
86
+ end
77
87
  end
78
88
 
79
89
  @@handlers[:float_array] = proc do |data, context|
80
90
  enforce_primitive!(Array, data, context)
81
- data.each {|item| enforce_primitive!(Float, item, context)}
91
+ if data.is_a? Array
92
+ data.each {|item| enforce_primitive!(Float, item, context)}
93
+ end
82
94
  end
83
95
 
84
96
  @@handlers[:integer_array] = proc do |data, context|
85
97
  enforce_primitive!(Array, data, context)
86
- data.each {|item| enforce_primitive!(Fixnum, item, context)}
98
+ if data.is_a? Array
99
+ data.each {|item| enforce_primitive!(Fixnum, item, context)}
100
+ end
87
101
  end
88
102
 
89
103
  @@handlers[:hex_color_array] = proc do |data, context|
90
104
  enforce_primitive!(Array, data, context)
91
- data.each {|item| enforce!(:hex_color, item, context)}
105
+ if data.is_a? Array
106
+ data.each {|item| enforce!(:hex_color, item, context)}
107
+ end
92
108
  end
93
109
 
94
110
  @@handlers[:uri_array] = proc do |data, context|
95
111
  enforce_primitive!(Array, data, context)
96
- data.each {|item| enforce!(:uri, item, context)}
112
+ if data.is_a? Array
113
+ data.each {|item| enforce!(:uri, item, context)}
114
+ end
97
115
  end
98
116
 
99
117
  @@handlers.each do |supertype, handler|
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.13'
5
+ VERSION = '0.0.15'
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: 5
4
+ hash: 1
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 13
10
- version: 0.0.13
9
+ - 15
10
+ version: 0.0.15
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-17 00:00:00 -07:00
18
+ date: 2010-08-18 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency