Class: Canoser::HashT

Inherits:
Object
  • Object
show all
Defined in:
lib/canoser/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#ktypeObject

Returns the value of attribute ktype.



139
140
141
# File 'lib/canoser/field.rb', line 139

def ktype
  @ktype
end

#vtypeObject

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