Module: Strict::Base
- Included in:
- Strict
- Defined in:
- lib/base.rb
Defined Under Namespace
Classes: TypeError
Constant Summary collapse
- @@errors =
[]
- @@raise_exception =
true
- @@dynamic_handlers =
{}
Instance Method Summary collapse
- #catch_error(error) ⇒ Object
- #enforce!(supertype, data, context = "Value") ⇒ Object
- #enforce_exists!(key, map) ⇒ Object
- #enforce_map!(matrix, map) ⇒ Object
- #enforce_map_defaults!(matrix, map) ⇒ Object
- #enforce_map_optional!(matrix, map) ⇒ Object
- #enforce_non_nil!(obj, context = "Value") ⇒ Object
- #enforce_primitive!(type, data, context = "Value") ⇒ Object
- #enforce_strict_primitives!(types, data, context = "Value") ⇒ Object
- #enforce_weak_primitives!(types, data, context = "Value") ⇒ Object
- #header(context, data) ⇒ Object
- #raise_hell! ⇒ Object
- #register_supertype(supertype, handler, verbose = true) ⇒ Object
- #setmode_catch! ⇒ Object
- #setmode_raise! ⇒ Object
Instance Method Details
#catch_error(error) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/base.rb', line 35 def catch_error error if @@raise_exception t = TypeError.new([error]) raise(t, t.inspect, caller) else @@errors << error end end |
#enforce!(supertype, data, context = "Value") ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/base.rb', line 119 def enforce!(supertype, data, context="Value") enforce_non_nil!(data, context) if supertype.is_a? Array #enumeration of values catch_error "#{header(context, data)} should take on a value within #{supertype.inspect}" unless supertype.include? data else if supertype.is_a? Symbol #distinct supertype if @@dynamic_handlers.has_key? supertype raise_set_before = @@raise_exception previous_errors = @@errors setmode_catch! @@dynamic_handlers[supertype].call(data, context) if raise_set_before raise_hell! setmode_raise! else @@errors.concat(previous_errors) end else catch_error "undefined symbol-supertype encountered: #{supertype.inspect}" end end end return data end |
#enforce_exists!(key, map) ⇒ Object
114 115 116 117 |
# File 'lib/base.rb', line 114 def enforce_exists!(key, map) catch_error "map: missing required param: #{key.inspect}" unless map.has_key? key map end |
#enforce_map!(matrix, map) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/base.rb', line 149 def enforce_map!(matrix, map) enforce_primitive!(Hash, matrix, "lib/typestrict") enforce_primitive!(Hash, map, "lib/typestrict") setmode_catch! matrix.each do |param, type| if map.has_key? param enforce!(type, map[param], "map[#{param.inspect}]") else catch_error "map: missing required param: #{param.inspect}" end end raise_hell! setmode_raise! return map end |
#enforce_map_defaults!(matrix, map) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/base.rb', line 167 def enforce_map_defaults!(matrix, map) enforce_primitive!(Hash, matrix, "lib/typestrict") enforce_primitive!(Hash, map, "lib/typestrict") matrix.each do |param, default_value| if !map.has_key? param map[param] = default_value end end return map end |
#enforce_map_optional!(matrix, map) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/base.rb', line 179 def enforce_map_optional!(matrix, map) enforce_primitive!(Hash, matrix, "lib/typestrict") enforce_primitive!(Hash, map, "lib/typestrict") setmode_catch! matrix.each do |param, type| if map.has_key? param enforce!(type, map[param], "map[#{param.inspect}]") end end raise_hell! setmode_raise! return map end |
#enforce_non_nil!(obj, context = "Value") ⇒ Object
109 110 111 112 |
# File 'lib/base.rb', line 109 def enforce_non_nil!(obj, context="Value") catch_error "#{context}: Object is nil!" if obj.nil? return obj end |
#enforce_primitive!(type, data, context = "Value") ⇒ Object
64 65 66 67 |
# File 'lib/base.rb', line 64 def enforce_primitive!(type, data, context="Value") catch_error "#{header(context, data)} must be of type #{type}" unless (data.is_a? type and type.is_a? Class) return data end |
#enforce_strict_primitives!(types, data, context = "Value") ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/base.rb', line 69 def enforce_strict_primitives!(types, data, context="Value") enforce_primitive!(Array, types, 'lib/typestrict') types.each {|type| enforce_primitive!(Object, type, 'lib/typestrict')} types.each do |type| enforce_primitive!(type, data, context) end return data end |
#enforce_weak_primitives!(types, data, context = "Value") ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/base.rb', line 78 def enforce_weak_primitives!(types, data, context="Value") enforce_primitive!(Array, types, 'lib/typestrict') types.each {|type| enforce_primitive!(Object, type, 'lib/typestrict')} raise_set_before = @@raise_exception previous_errors = @@errors setmode_catch! match_found = false types.each do |type| begin s = @@errors.size enforce_primitive!(type, data, context) match_found = true unless @@errors.size > s rescue next end end @@errors = previous_errors catch_error "#{header(context, data)} must be of one of the types of #{types.inspect}" unless match_found if raise_set_before raise_hell! unless match_found setmode_raise! end return data end |
#header(context, data) ⇒ Object
60 61 62 |
# File 'lib/base.rb', line 60 def header(context, data) "#{context}> #{data.class.inspect}::#{data.inspect}" end |
#raise_hell! ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/base.rb', line 44 def raise_hell! if @@errors.size > 0 t = TypeError.new(@@errors) setmode_raise! raise(t, t.inspect, caller) end end |
#register_supertype(supertype, handler, verbose = true) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/base.rb', line 52 def register_supertype(supertype, handler, verbose=true) enforce_primitive!(Symbol, supertype, "typestrict/register_supertype") enforce_primitive!(Proc, handler, "typestrict/register_supertype") @@dynamic_handlers[supertype] = handler puts "registered a handler for supertype: #{supertype}" if verbose end |
#setmode_catch! ⇒ Object
30 31 32 33 |
# File 'lib/base.rb', line 30 def setmode_catch! @@errors = [] @@raise_exception = false end |
#setmode_raise! ⇒ Object
25 26 27 28 |
# File 'lib/base.rb', line 25 def setmode_raise! @@errors = [] @@raise_exception = true end |