Class: Typed::Hash
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :schema => true, }
Instance Method Summary collapse
-
#[](key) ⇒ Object
Accessor.
- #[]=(key, val) ⇒ Object
- #check(key, type = nil) ⇒ Object
-
#default(key = nil, &block) ⇒ Object
Default values.
-
#each(&block) ⇒ Object
Hash compat.
-
#exist?(key) ⇒ Boolean
Testing.
-
#initialize(options = {}) ⇒ Hash
constructor
A new instance of Hash.
-
#inspect ⇒ Object
Utils.
-
#schema(key = nil) ⇒ Object
Schema values.
- #set?(key) ⇒ Boolean
- #update(key, val) ⇒ Object
- #values ⇒ Object
Constructor Details
Instance Method Details
#[](key) ⇒ Object
Accessor
43 44 45 46 47 48 49 50 |
# File 'lib/typed/hash.rb', line 43 def [](key) if exist?(key) return load(key) else from = caller.is_a?(Array) ? caller.first : self.class raise NotDefined, "'#{key}' is not initialized\n#{from}" end end |
#[]=(key, val) ⇒ Object
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 |
# File 'lib/typed/hash.rb', line 56 def []=(key, val) if check_schema?(key) if @schema.exist?(key) case val when LazyValue # not schema when [], {} # not schema cause already declared else struct = Must::StructInfo.new(val).compact # when schema format, try update schema @schema[key] = val if struct == val end @schema.check!(key, val) else case val when LazyValue # not schema when [], {} # ambiguous @schema[key] = val when nil, true, false # no information else struct = Must::StructInfo.new(val).compact @schema[key] = struct # when schema format, just declare schema and return return if struct == val end end end update(key, val) end |
#check(key, type = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/typed/hash.rb', line 102 def check(key, type = nil) return @schema.check!(key, self[key]) unless type self[key].must.struct(type) { got = Must::StructInfo.new(self[key]).compact.inspect value = self[key].inspect.truncate(200) raise TypeError, "%s(%s) got %s: %s" % [key, type.inspect, got, value] } end |
#default(key = nil, &block) ⇒ Object
Default values
21 22 23 24 25 26 27 |
# File 'lib/typed/hash.rb', line 21 def default(key = nil, &block) if key @default.regsiter_lazy(key, block) else @default end end |
#each(&block) ⇒ Object
Hash compat
115 116 117 118 119 120 |
# File 'lib/typed/hash.rb', line 115 def each(&block) keys.each do |key| val = self[key] block.call([key,val]) end end |
#exist?(key) ⇒ Boolean
Testing
94 95 96 |
# File 'lib/typed/hash.rb', line 94 def exist?(key) @hash.has_key?(key) end |
#inspect ⇒ Object
Utils
129 130 131 132 |
# File 'lib/typed/hash.rb', line 129 def inspect keys = @hash.keys.map(&:to_s).sort.join(',') "{#{keys}}" end |
#schema(key = nil) ⇒ Object
Schema values
32 33 34 35 36 37 38 |
# File 'lib/typed/hash.rb', line 32 def schema(key = nil) if key @schema[key] else @schema end end |
#set?(key) ⇒ Boolean
98 99 100 |
# File 'lib/typed/hash.rb', line 98 def set?(key) !! (exist?(key) && self[key]) end |
#update(key, val) ⇒ Object
52 53 54 |
# File 'lib/typed/hash.rb', line 52 def update(key, val) @hash[key] = val end |
#values ⇒ Object
122 123 124 |
# File 'lib/typed/hash.rb', line 122 def values keys.map{|key| self[key]} end |