Class: Cassandra::UDT

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cassandra/udt.rb

Overview

A user-defined type value representation

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ UDT

Creates a UDT instance

Examples:

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']])

Parameters:

  • values (Hash<String, Object>, Array<Array<String, Object>>, *Object, *Array<String, Object>)
    • UDT field values


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/cassandra/udt.rb', line 199

def initialize(*values)
  values = Array(values.first) if values.one?

  Util.assert_not_empty(values,
    "user-defined type must contain at least one value"
  )

  if values.first.is_a?(::Array)
    @values = values.map do |pair|
      Util.assert(pair.size == 2,
        "values of a user-defined type must be an Array of name and " \
        "value pairs, #{pair.inspect} given"
      )
      name, value = pair

      [String(name), value]
    end
  else
    Util.assert((values.size % 2) == 0,
      "values of a user-defined type must be an Array of alternating " \
      "names and values pairs, #{values.inspect} given"
    )
    @values = values.each_slice(2).map do |(name, value)|
      [String(name), value]
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(field) ⇒ Object #method_missing(field, value) ⇒ Cassandra::UDT

Allows access to properties of a User-Defined Type.

Examples:

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'

Overloads:

  • #method_missing(field) ⇒ Object

    Returns value of the field if present.

    Parameters:

    • field (Symbol)

      name of the field to lookup

    Returns:

    • (Object)

      value of the field if present

    Raises:

    • (NoMethodError)

      if the field is not present

  • #method_missing(field, value) ⇒ Cassandra::UDT

    Returns self.

    Parameters:

    • field (Symbol)

      name of the field (suffixed with =) to set the value for

    • value (Symbol)

      new value for the field

    Returns:

    Raises:

    • (NoMethodError)

      if the field is not present



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/cassandra/udt.rb', line 249

def method_missing(field, *args, &block)
  return super if block_given? || args.size > 1

  key = field.to_s
  set = !key.chomp!('=').nil?

  return super if set && args.empty?

  index = @values.index {|(name, _)| name == key}
  return super unless index

  if set
    @values[index][1] = args.first
  else
    @values[index][1]
  end
end

Instance Method Details

#[](field) ⇒ Object

Returns value of the field.

Parameters:

  • field (String, Integer)

    name or numeric index of the field to lookup

Returns:

  • (Object)

    value of the field, or nil if the field is not present

Raises:

  • (ArgumentError)

    when neither a numeric index nor a field name given



288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/cassandra/udt.rb', line 288

def [](field)
  case field
  when ::Integer
    return nil if field >= 0 && field < @values.size

    @values[field][1]
  when ::String
    index = @values.index {|(n, _)| field == n}

    index && @values[index][1]
  else
    raise ::ArgumentError, "Unrecognized field #{field.inspect}"
  end
end

#[]=(field, value) ⇒ Object

Sets value of the field.

Parameters:

  • field (String)

    name of the field to set

  • value (Object)

    new value for the field

Returns:

  • (Object)

    value.

Raises:

  • (IndexError)

    when numeric index given is out of bounds

  • (KeyError)

    when field with a given name is not present

  • (ArgumentError)

    when neither a numeric index nor a field name given



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/cassandra/udt.rb', line 363

def []=(field, value)
  case field
  when ::Integer
    if field < 0 || field >= @values.size
      raise ::IndexError, "Field index #{field.inspect} is not present"
    end

    @values[field][1] = value
  when ::String
    index = @values.index {|(n, _)| field == n}

    unless index
      raise ::KeyError, "Unsupported field #{field.inspect}"
    end

    @values[index][1] = value
  else
    raise ::ArgumentError, "Unrecognized field #{field.inspect}"
  end
end

#each {|name, value| ... } ⇒ Cassandra::UDT

Iterates over all fields of the UDT

Yield Parameters:

  • name (String)

    field name

  • value (Object)

    field value

Returns:



388
389
390
391
# File 'lib/cassandra/udt.rb', line 388

def each(&block)
  @values.each {|(n, v)| yield(n, v)}
  self
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


415
416
417
# File 'lib/cassandra/udt.rb', line 415

def eql?(other)
  other.is_a?(UDT) && @values.all? {|(n, v)| v == other[n]}
end

#fetch(field) ⇒ Object

Returns value of the field.

Parameters:

  • field (String, Integer)

    name or numeric index of the field to lookup

Returns:

  • (Object)

    value of the field

Raises:

  • (IndexError)

    when numeric index given is out of bounds

  • (KeyError)

    when field with a given name is not present

  • (ArgumentError)

    when neither a numeric index nor a field name given



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/cassandra/udt.rb', line 313

def fetch(field)
  case field
  when ::Integer
    if field >= 0 && field < @values.size
      raise ::IndexError, "Field index #{field.inspect} is not present"
    end

    @values[field][1]
  when ::String
    index = @values.index {|(n, _)| field == n}

    unless index
      raise ::KeyError, "Unsupported field #{field.inspect}"
    end

    @values[index][1]
  else
    raise ::ArgumentError, "Unrecognized field #{field.inspect}"
  end
end

#has_field?(field) ⇒ Boolean Also known as: include?

Returns whether the field is present in this UDT.

Parameters:

  • field (String, Integer)

    name or numeric index of the field to lookup

Returns:

  • (Boolean)

    whether the field is present in this UDT



338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/cassandra/udt.rb', line 338

def has_field?(field)
  case field
  when ::Integer
    return false if field < 0 || field >= @values.size
  when ::String
    return false unless @values.index {|(n, _)| field == n}
  else
    return false
  end

  true
end

#inspectObject



411
412
413
# File 'lib/cassandra/udt.rb', line 411

def inspect
  "#<Cassandra::UDT:0x#{self.object_id.to_s(16)} #{to_s}>"
end

#respond_to?(field) ⇒ Boolean

Returns true if a field with a given name is present

Parameters:

  • field (Symbol)

    method or name of the field

Returns:

  • (Boolean)

    whether a field is present



272
273
274
275
276
277
278
# File 'lib/cassandra/udt.rb', line 272

def respond_to?(field)
  key = field.to_s
  key.chomp!('=')

  return true if @values.any? {|(name, _)| name == key}
  super
end

#sizeInteger

Returns UDT size

Returns:

  • (Integer)

    UDT size



395
396
397
# File 'lib/cassandra/udt.rb', line 395

def size
  @values.size
end

#to_hObject

Hash representation of the UDT



400
401
402
403
404
# File 'lib/cassandra/udt.rb', line 400

def to_h
  @values.each_with_object(::Hash.new) do |(n, v), hash|
    hash[n] = v
  end
end

#to_sObject

String representation of the UDT



407
408
409
# File 'lib/cassandra/udt.rb', line 407

def to_s
  '{ ' + @values.map {|(n, v)| "#{n}: #{v.inspect}"}.join(', ') + ' }'
end