module Cassandra::Util
@private
Constants
- COLON
@private
- COMMA
@private
- CRL_CLS
@private
- CRL_OPN
@private
- DBL_QUOT
@private
- ESC_QUOT
@private
- FALSE_STR
@private
- LOWERCASE_REGEXP
@private
- NULL_STR
@private
- PRN_CLS
@private
- PRN_OPN
@private
- QUOT
@private
- RESERVED_WORDS
- SQR_CLS
@private
- SQR_OPN
@private
- TRUE_STR
@private
Public Instance Methods
assert(condition, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 287 def assert(condition, message = nil, &block) 288 unless condition 289 message = yield if block_given? 290 message ||= 'assertion failed' 291 292 raise ::ArgumentError, message 293 end 294 end
assert_equal(expected, actual, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 296 def assert_equal(expected, actual, message = nil, &block) 297 unless expected == actual 298 message = yield if block_given? 299 message ||= "expected #{actual.inspect} to equal #{expected.inspect}" 300 301 raise ::ArgumentError, message 302 end 303 end
assert_file_exists(path, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 259 def assert_file_exists(path, message = nil, &block) 260 unless ::File.exist?(path) 261 message = yield if block_given? 262 message ||= "expected file at #{path.inspect} to exist, but it doesn't" 263 264 raise ::ArgumentError, message 265 end 266 end
assert_instance_of(kind, value, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 211 def assert_instance_of(kind, value, message = nil, &block) 212 unless value.is_a?(kind) 213 message = yield if block_given? 214 message ||= "value must be an instance of #{kind}, #{value.inspect} given" 215 216 raise ::ArgumentError, message 217 end 218 end
assert_instance_of_one_of(kinds, value, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 220 def assert_instance_of_one_of(kinds, value, message = nil, &block) 221 unless kinds.any? {|kind| value.is_a?(kind)} 222 message = yield if block_given? 223 message ||= "value must be an instance of one of #{kinds.inspect}, " \ 224 "#{value.inspect} given" 225 226 raise ::ArgumentError, message 227 end 228 end
assert_not_empty(value, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 250 def assert_not_empty(value, message = nil, &block) 251 if value.empty? 252 message = yield if block_given? 253 message ||= 'value cannot be empty' 254 255 raise ::ArgumentError, message 256 end 257 end
assert_one_of(range, value, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 268 def assert_one_of(range, value, message = nil, &block) 269 unless range.include?(value) 270 message = yield if block_given? 271 message ||= "value must be included in #{value.inspect}, #{value.inspect} given" 272 273 raise ::ArgumentError, message 274 end 275 end
assert_responds_to(method, value, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 230 def assert_responds_to(method, value, message = nil, &block) 231 unless value.respond_to?(method) 232 message = yield if block_given? 233 message ||= "value #{value.inspect} must respond to #{method.inspect}, " \ 234 "but doesn't" 235 236 raise ::ArgumentError, message 237 end 238 end
assert_responds_to_all(methods, value, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 240 def assert_responds_to_all(methods, value, message = nil, &block) 241 unless methods.all? {|method| value.respond_to?(method)} 242 message = yield if block_given? 243 message ||= "value #{value.inspect} must respond to all methods " \ 244 "#{methods.inspect}, but doesn't" 245 246 raise ::ArgumentError, message 247 end 248 end
assert_size(size, value, message = nil) { || ... }
click to toggle source
# File lib/cassandra/util.rb 277 def assert_size(size, value, message = nil, &block) 278 unless value.size == size 279 message = yield if block_given? 280 message ||= "value #{value.inspect} must have size equal to " \ 281 "#{size.inspect}, but doesn't" 282 283 raise ::ArgumentError, message 284 end 285 end
assert_type(type, value, message = nil, &block)
click to toggle source
# File lib/cassandra/util.rb 205 def assert_type(type, value, message = nil, &block) 206 assert_instance_of(Cassandra::Type, type, message, &block) 207 return if value.nil? 208 type.assert(value, message, &block) 209 end
encode_array(array, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 62 def encode_array(array, io = StringIO.new) 63 first = true 64 65 io.putc(SQR_OPN) 66 array.each do |object| 67 if first 68 first = false 69 else 70 io.print(COMMA) 71 end 72 73 encode_object(object, io) 74 end 75 io.putc(SQR_CLS) 76 77 io.string 78 end
encode_hash(hash, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 24 def encode_hash(hash, io = StringIO.new) 25 first = true 26 27 io.putc(CRL_OPN) 28 hash.each do |k, v| 29 if first 30 first = false 31 else 32 io.print(COMMA) 33 end 34 35 encode_object(k, io) 36 io.print(COLON) 37 encode_object(v, io) 38 end 39 io.putc(CRL_CLS) 40 41 io.string 42 end
encode_inet(inet, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 141 def encode_inet(inet, io = StringIO.new) 142 io.putc(QUOT) 143 io.print(inet) 144 io.putc(QUOT) 145 io.string 146 end
encode_number(number, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 131 def encode_number(number, io = StringIO.new) 132 io.print(number) 133 io.string 134 end
encode_object(object, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 94 def encode_object(object, io = StringIO.new) 95 case object 96 when ::Hash then encode_hash(object, io) 97 when ::Array then encode_array(object, io) 98 when ::Set then encode_set(object, io) 99 when ::String then encode_string(object, io) 100 when ::Time then encode_timestamp(object, io) 101 when ::Numeric then encode_number(object, io) 102 when ::IPAddr then encode_inet(object, io) 103 when Uuid then encode_uuid(object, io) 104 when Tuple then encode_tuple(object, io) 105 when Time then encode_time(object, io) 106 when UDT then encode_udt(object, io) 107 when nil then io.print(NULL_STR) 108 when false then io.print(FALSE_STR) 109 when true then io.print(TRUE_STR) 110 else 111 raise ::ArgumentError, "unsupported type: #{object.inspect}" 112 end 113 114 io.string 115 end
Also aliased as: encode
encode_set(set, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 44 def encode_set(set, io = StringIO.new) 45 first = true 46 47 io.putc(CRL_OPN) 48 set.each do |object| 49 if first 50 first = false 51 else 52 io.print(COMMA) 53 end 54 55 encode_object(object, io) 56 end 57 io.putc(CRL_CLS) 58 59 io.string 60 end
encode_string(string, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 80 def encode_string(string, io = StringIO.new) 81 io.putc(QUOT) 82 string.chars do |c| 83 case c 84 when QUOT then io.print(ESC_QUOT) 85 else 86 io.putc(c) 87 end 88 end 89 io.putc(QUOT) 90 91 io.string 92 end
encode_time(time, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 118 def encode_time(time, io = StringIO.new) 119 encode_string(time.to_s, io) 120 end
encode_timestamp(time, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 126 def encode_timestamp(time, io = StringIO.new) 127 io.print(time.to_i) 128 io.string 129 end
encode_tuple(tuple, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 148 def encode_tuple(tuple, io = StringIO.new) 149 first = true 150 151 io.putc(PRN_OPN) 152 tuple.each do |object| 153 if first 154 first = false 155 else 156 io.print(COMMA) 157 end 158 159 encode_object(object, io) 160 end 161 io.putc(PRN_CLS) 162 end
encode_udt(udt, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 122 def encode_udt(udt, io = StringIO.new) 123 encode_hash(udt.to_h, io) 124 end
encode_uuid(uuid, io = StringIO.new)
click to toggle source
# File lib/cassandra/util.rb 136 def encode_uuid(uuid, io = StringIO.new) 137 io.print(uuid) 138 io.string 139 end
escape_name(name)
click to toggle source
# File lib/cassandra/util.rb 164 def escape_name(name) 165 # If name only contains lower-case chars and it's not a reserved word, return it 166 # as-is. Otherwise, quote. 167 return name if name[LOWERCASE_REGEXP] == name && !RESERVED_WORDS.include?(name) 168 169 # Replace double-quotes within name with two double-quotes (if any) and surround the whole 170 # thing with double-quotes 171 DBL_QUOT + name.gsub('"', '""') + DBL_QUOT 172 end
guess_type(object)
click to toggle source
# File lib/cassandra/util.rb 174 def guess_type(object) 175 case object 176 when ::String then Types.varchar 177 when ::Integer then object.size > 8 ? Types.varint : Types.bigint 178 when ::Float then Types.double 179 when ::BigDecimal then Types.decimal 180 when ::TrueClass then Types.boolean 181 when ::FalseClass then Types.boolean 182 when ::NilClass then Types.bigint 183 when Uuid then Types.uuid 184 when TimeUuid then Types.timeuuid 185 when ::IPAddr then Types.inet 186 when ::Time then Types.timestamp 187 when ::Hash 188 pair = object.first 189 Types.map(guess_type(pair[0]), guess_type(pair[1])) 190 when ::Array then Types.list(guess_type(object.first)) 191 when ::Set then Types.set(guess_type(object.first)) 192 when Tuple::Strict then Types.tuple(*object.types) 193 when Tuple then Types.tuple(*object.map {|v| guess_type(v)}) 194 when UDT::Strict 195 Types.udt(object.keyspace, object.name, object.types) 196 when UDT 197 Types.udt('unknown', 'unknown', object.map {|k, v| [k, guess_type(v)]}) 198 when Cassandra::CustomData then object.class.type 199 else 200 raise ::ArgumentError, 201 "Unable to guess the type of the argument: #{object.inspect}" 202 end 203 end