Class: Canoser::HashT
- Inherits:
-
Object
- Object
- Canoser::HashT
- Defined in:
- lib/canoser/field.rb
Instance Attribute Summary collapse
-
#ktype ⇒ Object
Returns the value of attribute ktype.
-
#vtype ⇒ Object
Returns the value of attribute vtype.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #decode(cursor) ⇒ Object
- #encode(hash) ⇒ Object
-
#initialize(ktype = DEFAULT_KV, vtype = DEFAULT_KV) ⇒ HashT
constructor
A new instance of HashT.
Constructor Details
#initialize(ktype = DEFAULT_KV, vtype = DEFAULT_KV) ⇒ HashT
Returns a new instance of HashT.
141 142 143 144 |
# File 'lib/canoser/field.rb', line 141 def initialize(ktype=DEFAULT_KV, vtype=DEFAULT_KV) @ktype = ktype || DEFAULT_KV @vtype = vtype || DEFAULT_KV end |
Instance Attribute Details
#ktype ⇒ Object
Returns the value of attribute ktype.
139 140 141 |
# File 'lib/canoser/field.rb', line 139 def ktype @ktype end |
#vtype ⇒ Object
Returns the value of attribute vtype.
139 140 141 |
# File 'lib/canoser/field.rb', line 139 def vtype @vtype end |
Instance Method Details
#==(other) ⇒ Object
171 172 173 |
# File 'lib/canoser/field.rb', line 171 def ==(other) return self.ktype == other.ktype && self.vtype == other.vtype end |
#decode(cursor) ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/canoser/field.rb', line 160 def decode(cursor) hash = {} len = Uint32.decode(cursor) len.times do k = ktype.decode(cursor) v = vtype.decode(cursor) hash[k] = v end hash end |
#encode(hash) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/canoser/field.rb', line 146 def encode(hash) output = "" output << Uint32.encode(hash.size) sorted_map = {} hash.each do |k, v| sorted_map[ktype.encode(k)] = vtype.encode(v) end sorted_map.keys.sort.each do |k| output << k output << sorted_map[k] end output end |