# File lib/amq/protocol/table_value_encoder.rb, line 107 107: def self.array_size(value) 108: acc = 0 109: value.each { |v| acc += self.field_value_size(v) } 110: 111: acc 112: end
API
# File lib/amq/protocol/table_value_encoder.rb, line 22 22: def self.encode(value) 23: accumulator = String.new 24: 25: case value 26: when String then 27: accumulator << TYPE_STRING 28: accumulator << [value.bytesize].pack(PACK_UINT32) 29: accumulator << value 30: when Symbol then 31: v = value.to_s 32: accumulator << TYPE_STRING 33: accumulator << [v.bytesize].pack(PACK_UINT32) 34: accumulator << v 35: when Integer then 36: accumulator << TYPE_INTEGER 37: accumulator << [value].pack(PACK_UINT32) 38: when Float then 39: accumulator << TYPE_64BIT_FLOAT 40: accumulator << [value].pack(PACK_64BIT_FLOAT) 41: when true, false then 42: accumulator << TYPE_BOOLEAN 43: accumulator << (value ? BOOLEAN_TRUE : BOOLEAN_FALSE) 44: when Time then 45: accumulator << TYPE_TIME 46: accumulator << [value.to_i].pack(PACK_INT64).reverse # FIXME: there has to be a more efficient way 47: when nil then 48: accumulator << TYPE_VOID 49: when Array then 50: accumulator << TYPE_ARRAY 51: accumulator << [self.array_size(value)].pack(PACK_UINT32) 52: 53: value.each { |v| accumulator << self.encode(v) } 54: when Hash then 55: accumulator << TYPE_HASH 56: accumulator << AMQ::Protocol::Table.encode(value) 57: else 58: # We don't want to require these libraries. 59: if defined?(BigDecimal) && value.is_a?(BigDecimal) 60: accumulator << TYPE_DECIMAL 61: if value.exponent < 0 62: decimals = -value.exponent 63: raw = (value * (decimals ** 10)).to_i 64: accumulator << [decimals + 1, raw].pack(PACK_UCHAR_UINT32) # somewhat like floating point 65: else 66: # per spec, the "decimals" octet is unsigned (!) 67: accumulator << [0, value.to_i].pack(PACK_UCHAR_UINT32) 68: end 69: else 70: raise ArgumentError.new("Unsupported value #{value.inspect} of type #{value.class.name}") 71: end # if 72: end # case 73: 74: accumulator 75: end
# File lib/amq/protocol/table_value_encoder.rb, line 80 80: def self.field_value_size(value) 81: # the type tag takes 1 byte 82: acc = 1 83: 84: case value 85: when String then 86: acc += (value.bytesize + 4) 87: when Integer then 88: acc += 4 89: when Float then 90: acc += 8 91: when Time, DateTime then 92: acc += 8 93: when true, false then 94: acc += 1 95: when nil then 96: # nothing, type tag alone is enough 97: when Hash then 98: acc += (4 + Table.hash_size(value)) 99: when Array then 100: acc += (4 + self.array_size(value)) 101: end 102: 103: acc 104: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.