Module: Strict::SuperTypes
- Included in:
- Strict
- Defined in:
- lib/supertypes.rb
Constant Summary collapse
- @@handlers =
{}
- @@called =
false
Instance Method Summary collapse
-
#register_default_supertypes! ⇒ Object
must be called from an env including Base.
- #registered?(supertype) ⇒ Boolean
Instance Method Details
#register_default_supertypes! ⇒ Object
must be called from an env including Base
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/supertypes.rb', line 12 def register_default_supertypes! #must be called from an env including Base if @@called return else @@called = true end @@handlers[:string] = proc do |data, context| enforce_primitive!(String, data, context) if data.is_a? String catch_error "#{header(context, data)} can't be empty string" unless (data.size > 0) end end @@handlers[:natural_number] = proc do |data, context| enforce_primitive!(Fixnum, data, context) catch_error "#{header(context, data)} must be > 0" unless (data > 0) if data.is_a? Fixnum end @@handlers[:integer] = proc do |data, context| enforce_primitive!(Fixnum, data, context) end @@handlers[:numeric] = proc do |data, context| enforce_primitive!(Numeric, data, context) end @@handlers[:boolean] = proc do |data, context| enforce_weak_primitives!([TrueClass, FalseClass], data, context) end @@handlers[:float] = proc do |data, context| enforce_primitive!(Float, data, context) end @@handlers[:character] = proc do |data, context| enforce!(:string, data, context) if data.is_a? String catch_error "#{header(context, data)} must be a single character!" unless data.size == 1 end end @@handlers[:procedure] = proc do |data, context| enforce_primitive!(Proc, data, context) end @@handlers[:hash_map] = proc do |data, context| enforce_primitive!(Hash, data, context) end @@handlers[:hex_color] = proc do |data, context| enforce!(:string, data, context) if data.is_a? String catch_error "#{header(context, data)} must be six characters long" unless (data.size == 6) 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))} end end @@handlers[:uri] = proc do |data, context| enforce!(:string, data, context) end @@handlers[:string_array] = proc do |data, context| enforce_primitive!(Array, data, context) if data.is_a? Array data.each {|item| enforce_primitive!(String, item, context)} end end @@handlers[:numeric_array] = proc do |data, context| enforce_primitive!(Array, data, context) if data.is_a? Array data.each {|item| enforce_primitive!(Numeric, item, context)} end end @@handlers[:float_array] = proc do |data, context| enforce_primitive!(Array, data, context) if data.is_a? Array data.each {|item| enforce_primitive!(Float, item, context)} end end @@handlers[:integer_array] = proc do |data, context| enforce_primitive!(Array, data, context) if data.is_a? Array data.each {|item| enforce_primitive!(Fixnum, item, context)} end end @@handlers[:hex_color_array] = proc do |data, context| enforce_primitive!(Array, data, context) if data.is_a? Array data.each {|item| enforce!(:hex_color, item, context)} end end @@handlers[:uri_array] = proc do |data, context| enforce_primitive!(Array, data, context) if data.is_a? Array data.each {|item| enforce!(:uri, item, context)} end end @@handlers.each do |supertype, handler| Base::register_supertype(supertype, handler, false) #verbose set to false end end |
#registered?(supertype) ⇒ Boolean
7 8 9 10 |
# File 'lib/supertypes.rb', line 7 def registered?(supertype) enforce_primitive!(Symbol, supertype) Strict::Base.has_key? supertype end |