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


223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/cassandra/udt.rb', line 223

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.even?,
                '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



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/cassandra/udt.rb', line 270

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



309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/cassandra/udt.rb', line 309

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



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/cassandra/udt.rb', line 380

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

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

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

    @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:



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

def each(&block)
  @values.each {|(n, v)| yield(n, v)}
  self
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



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

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

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

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

    @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



355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/cassandra/udt.rb', line 355

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

#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



293
294
295
296
297
298
299
# File 'lib/cassandra/udt.rb', line 293

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



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

def size
  @values.size
end

#to_hObject

Hash representation of the UDT



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

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



420
421
422
# File 'lib/cassandra/udt.rb', line 420

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