Skip to content

Commit

Permalink
Merge pull request #8 from rjackson/add_binary_data_utility_method
Browse files Browse the repository at this point in the history
Fix is_binary_data? was removed in 1.9. Add binary_data? utility function
  • Loading branch information
Kelley Reynolds committed Aug 19, 2011
2 parents 1d762f0 + a7b11e8 commit 11c7bf5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/cassandra-cql/statement.rb
Expand Up @@ -87,7 +87,7 @@ def self.cast_to_cql(obj)
obj.to_guid
# There are corner cases where this is an invalid assumption but they are extremely rare.
# The alternative is to make the user pack the data on their own .. let's not do that until we have to
elsif obj.kind_of?(String) and obj.is_binary_data?
elsif obj.kind_of?(String) and Utility.binary_data?(obj)
escape(obj.unpack('H*')[0])
else
escape(obj.to_s)
Expand Down
4 changes: 4 additions & 0 deletions lib/cassandra-cql/utility.rb
Expand Up @@ -9,5 +9,9 @@ def self.compress(source, level=2)
def self.decompress(source)
Zlib::Inflate.inflate(source)
end

def self.binary_data?(string)
( string.count( "^ -~", "^\r\n" ).fdiv(string.size) > 0.3 || string.index( "\x00" ) ) unless string.empty?
end
end
end
2 changes: 1 addition & 1 deletion spec/utility_spec.rb
Expand Up @@ -5,7 +5,7 @@
it "should return some valid gzipped stuff" do
stuff = "This is some stuff"
bytes = Utility.compress(stuff)
bytes.is_binary_data?.should be_true
Utility.binary_data?(bytes).should be_true
Utility.decompress(bytes).should eq(stuff)
end
end
Expand Down

0 comments on commit 11c7bf5

Please sign in to comment.