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_defaults!(matrix, map) ⇒ Object
- #enforce_exists!(key, map) ⇒ Object
- #enforce_map!(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
34 35 36 37 38 39 40 41 |
# File 'lib/base.rb', line 34 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
118 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 |
# File 'lib/base.rb', line 118 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_defaults!(matrix, map) ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/base.rb', line 182 def enforce_defaults!(matrix, map) enforce_primitive!(Hash, matrix, "lib/typestrict") enforce_primitive!(Hash, map, "lib/typestrict") matrix.each do |param, default| if !map.has_key? param map[param] = default end end end |
#enforce_exists!(key, map) ⇒ Object
113 114 115 116 |
# File 'lib/base.rb', line 113 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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/base.rb', line 148 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_optional!(matrix, map) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/base.rb', line 166 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
108 109 110 111 |
# File 'lib/base.rb', line 108 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
63 64 65 66 |
# File 'lib/base.rb', line 63 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
68 69 70 71 72 73 74 75 |
# File 'lib/base.rb', line 68 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
77 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 |
# File 'lib/base.rb', line 77 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
59 60 61 |
# File 'lib/base.rb', line 59 def header(context, data) "#{context}> #{data.class.inspect}::#{data.inspect}" end |
#raise_hell! ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/base.rb', line 43 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
51 52 53 54 55 56 57 |
# File 'lib/base.rb', line 51 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
29 30 31 32 |
# File 'lib/base.rb', line 29 def setmode_catch! @@errors = [] @@raise_exception = false end |
#setmode_raise! ⇒ Object
24 25 26 27 |
# File 'lib/base.rb', line 24 def setmode_raise! @@errors = [] @@raise_exception = true end |