class Cassandra::UDT
A user-defined type value representation
Public Class Methods
Creates a UDT
instance @param values [Hash<String, Object>, Array<Array<String, Object>>,
*Object, *Array<String, Object>] - UDT field values
@example Various ways of creating the same UDT
instance
Cassandra::UDT.new({'street' => '123 Main St.', 'city' => 'Whatever', 'state' => 'XZ', 'zip' => '10020'}) Cassandra::UDT.new(street: '123 Main St.', city: 'Whatever', state: 'XZ', zip: '10020') Cassandra::UDT.new('street', '123 Main St.', 'city', 'Whatever', 'state', 'XZ', 'zip', '10020') Cassandra::UDT.new(:street, '123 Main St.', :city, 'Whatever', :state, 'XZ', :zip, '10020') Cassandra::UDT.new(['street', '123 Main St.'], ['city', 'Whatever'], ['state', 'XZ'], ['zip', '10020']) Cassandra::UDT.new([:street, '123 Main St.'], [:city, 'Whatever'], [:state, 'XZ'], [:zip, '10020']) Cassandra::UDT.new([['street', '123 Main St.'], ['city', 'Whatever'], ['state', 'XZ'], ['zip', '10020']]) Cassandra::UDT.new([[:street, '123 Main St.'], [:city, 'Whatever'], [:state, 'XZ'], [:zip, '10020']])
# File lib/cassandra/udt.rb 223 def initialize(*values) 224 values = Array(values.first) if values.one? 225 226 Util.assert_not_empty(values, 227 'user-defined type must contain at least one value') 228 229 if values.first.is_a?(::Array) 230 @values = values.map do |pair| 231 Util.assert(pair.size == 2, 232 'values of a user-defined type must be an Array of name and ' \ 233 "value pairs, #{pair.inspect} given") 234 name, value = pair 235 236 [String(name), value] 237 end 238 else 239 Util.assert(values.size.even?, 240 'values of a user-defined type must be an Array of alternating ' \ 241 "names and values pairs, #{values.inspect} given") 242 @values = values.each_slice(2).map do |(name, value)| 243 [String(name), value] 244 end 245 end 246 end
Public Instance Methods
Returns value of the field.
@param field [String, Integer] name or numeric index of the field to
lookup
@raise [ArgumentError] when neither a numeric index nor a field name given
@return [Object] value of the field, or nil if the field is not present
# File lib/cassandra/udt.rb 309 def [](field) 310 case field 311 when ::Integer 312 return nil if field >= 0 && field < @values.size 313 314 @values[field][1] 315 when ::String 316 index = @values.index {|(n, _)| field == n} 317 318 index && @values[index][1] 319 else 320 raise ::ArgumentError, "Unrecognized field #{field.inspect}" 321 end 322 end
Sets value of the field.
@param field [String] name of the field to set @param value [Object] new value for the field
@raise [IndexError] when numeric index given is out of bounds @raise [KeyError] when field with a given name is not present @raise [ArgumentError] when neither a numeric index nor a field name given
@return [Object] value.
# File lib/cassandra/udt.rb 380 def []=(field, value) 381 case field 382 when ::Integer 383 raise ::IndexError, "Field index #{field.inspect} is not present" if field < 0 || field >= @values.size 384 385 @values[field][1] = value 386 when ::String 387 index = @values.index {|(n, _)| field == n} 388 389 raise ::KeyError, "Unsupported field #{field.inspect}" unless index 390 391 @values[index][1] = value 392 else 393 raise ::ArgumentError, "Unrecognized field #{field.inspect}" 394 end 395 end
Iterates over all fields of the UDT
@yieldparam name [String] field name @yieldparam value [Object] field value @return [Cassandra::UDT] self
# File lib/cassandra/udt.rb 401 def each(&block) 402 @values.each {|(n, v)| yield(n, v)} 403 self 404 end
@private
# File lib/cassandra/udt.rb 430 def eql?(other) 431 other.is_a?(UDT) && @values.all? {|(n, v)| v == other[n]} 432 end
Returns value of the field.
@param field [String, Integer] name or numeric index of the field to
lookup
@raise [IndexError] when numeric index given is out of bounds @raise [KeyError] when field with a given name is not present @raise [ArgumentError] when neither a numeric index nor a field name given
@return [Object] value of the field
# File lib/cassandra/udt.rb 334 def fetch(field) 335 case field 336 when ::Integer 337 raise ::IndexError, "Field index #{field.inspect} is not present" if field >= 0 && field < @values.size 338 339 @values[field][1] 340 when ::String 341 index = @values.index {|(n, _)| field == n} 342 343 raise ::KeyError, "Unsupported field #{field.inspect}" unless index 344 345 @values[index][1] 346 else 347 raise ::ArgumentError, "Unrecognized field #{field.inspect}" 348 end 349 end
@param field [String, Integer] name or numeric index of the field to
lookup
@return [Boolean] whether the field is present in this UDT
# File lib/cassandra/udt.rb 355 def has_field?(field) 356 case field 357 when ::Integer 358 return false if field < 0 || field >= @values.size 359 when ::String 360 return false unless @values.index {|(n, _)| field == n} 361 else 362 return false 363 end 364 365 true 366 end
@private
# File lib/cassandra/udt.rb 436 def hash 437 @values.inject(17) do |h, (n, v)| 438 h = 31 * h + n.hash 439 31 * h + v.hash 440 end 441 end
@private
# File lib/cassandra/udt.rb 425 def inspect 426 "#<Cassandra::UDT:0x#{object_id.to_s(16)} #{self}>" 427 end
@!visibility public Allows access to properties of a User-Defined Type
.
@example Getting and setting field values
session.execute("CREATE TYPE address (street text, zipcode int)") session.execute("CREATE TABLE users (id int PRIMARY KEY, location frozen<address>)") row = session.execute("SELECT * FROM users WHERE id = 123").first address = row['address'] puts address.street address.street = '123 SomePlace Cir'
@overload method_missing
(field)
@param field [Symbol] name of the field to lookup @raise [NoMethodError] if the field is not present @return [Object] value of the field if present
@overload method_missing
(field, value)
@param field [Symbol] name of the field (suffixed with `=`) to set the value for @param value [Symbol] new value for the field @raise [NoMethodError] if the field is not present @return [Cassandra::UDT] self.
# File lib/cassandra/udt.rb 270 def method_missing(field, *args, &block) 271 return super if block_given? || args.size > 1 272 273 key = field.to_s 274 set = !key.chomp!('=').nil? 275 276 return super if set && args.empty? 277 278 index = @values.index {|(name, _)| name == key} 279 return super unless index 280 281 if set 282 @values[index][1] = args.first 283 else 284 @values[index][1] 285 end 286 end
Returns true if a field with a given name is present
@param field [Symbol] method or name of the field
@return [Boolean] whether a field is present
# File lib/cassandra/udt.rb 293 def respond_to?(field) 294 key = field.to_s 295 key.chomp!('=') 296 297 return true if @values.any? {|(name, _)| name == key} 298 super 299 end
Hash representation of the UDT
# File lib/cassandra/udt.rb 413 def to_h 414 @values.each_with_object(::Hash.new) do |(n, v), hash| 415 hash[n] = v 416 end 417 end
String representation of the UDT
# File lib/cassandra/udt.rb 420 def to_s 421 '{ ' + @values.map {|(n, v)| "#{n}: #{v.inspect}"}.join(', ') + ' }' 422 end