Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
Merge branch 'pr_115'
Browse files Browse the repository at this point in the history
  • Loading branch information
iconara committed Jul 4, 2014
2 parents 4b90986 + 228cddb commit 8d6790b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/cql/protocol/cql_byte_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,12 @@ def append_varint(n)
end

def append_decimal(n)
sign, number_string, _, size = n.split
str = n.to_s(FLOAT_STRING_FORMAT)
size = str.index(DECIMAL_POINT)
number_string = str.gsub(DECIMAL_POINT, NO_CHAR)

num = number_string.to_i
raw = self.class.new.append_varint(sign * num)
raw = self.class.new.append_varint(num)
append_int(number_string.length - size)
append(raw)
end
Expand All @@ -284,6 +287,8 @@ def append_float(n)
MINUS = '-'.freeze
ZERO = '0'.freeze
DECIMAL_POINT = '.'.freeze
FLOAT_STRING_FORMAT = 'F'.freeze
NO_CHAR = ''.freeze
end
end
end
10 changes: 10 additions & 0 deletions spec/cql/protocol/cql_byte_buffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,16 @@ module Protocol
buffer.should eql_bytes("\x00\x00\x00\x01\x00")
end

it 'encodes a BigDecimal ending in .0' do
buffer.append_decimal(BigDecimal.new('1042342234234.0'))
buffer.should eql_bytes("\x00\x00\x00\x01\tz\xE4b\xD4\xC4")
end

it 'encodes a BigDecimal ending with 00.0' do
buffer.append_decimal(BigDecimal.new('12000.0'))
buffer.should eql_bytes("\x00\x00\x00\x01\x01\xD4\xC0")
end

it 'appends to the buffer' do
buffer << "\x99"
buffer.append_decimal(BigDecimal.new('1042342234234.123423435647768234'))
Expand Down

0 comments on commit 8d6790b

Please sign in to comment.