Class: Cassandra::Types::UserDefined

Inherits:
Cassandra::Type show all
Defined in:
lib/cassandra/types.rb

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Attributes inherited from Cassandra::Type

#kind

Instance Method Summary collapse

Instance Attribute Details

#keyspaceString (readonly)

Returns keyspace where this type is defined.

Returns:

  • (String)

    keyspace where this type is defined



1207
1208
1209
# File 'lib/cassandra/types.rb', line 1207

def keyspace
  @keyspace
end

#nameString (readonly)

Returns name of this type.

Returns:

  • (String)

    name of this type



1210
1211
1212
# File 'lib/cassandra/types.rb', line 1210

def name
  @name
end

Instance Method Details

#assert(value, message = nil, &block) ⇒ void

This method returns an undefined value.

Asserts that a given value is an Cassandra::UDT

Parameters:

  • value (Object)

    value to be validated

  • message (String) (defaults to: nil)

    error message to use when assertion fails

Yield Returns:

  • (String)

    error message to use when assertion fails

Raises:

  • (ArgumentError)

    if the value is not an Cassandra::UDT

See Also:



1293
1294
1295
1296
1297
1298
1299
1300
# File 'lib/cassandra/types.rb', line 1293

def assert(value, message = nil, &block)
  Util.assert_instance_of(Cassandra::UDT, value, message, &block)
  Util.assert(value.size <= @fields.size, message, &block)
  @fields.each do |field|
    Util.assert_type(field.type, value[field.name], message, &block)
  end
  nil
end

#each_field {|field| ... } ⇒ Cassandra::Types::UserDefined #each_fieldArray<Array<String, Cassandra::Type>> Also known as: fields

Yield or enumerate each field defined in this type

Overloads:



1235
1236
1237
1238
1239
1240
1241
1242
# File 'lib/cassandra/types.rb', line 1235

def each_field(&block)
  if block_given?
    @fields.each(&block)
    self
  else
    @fields.dup
  end
end

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

Returns:



1320
1321
1322
1323
1324
1325
# File 'lib/cassandra/types.rb', line 1320

def eql?(other)
  other.is_a?(UserDefined) &&
    @keyspace == other.keyspace &&
    @name == other.name &&
    @fields == other.fields
end

#field(name) ⇒ Cassandra::UserDefined::Field?

Returns a field with this name or nil.

Parameters:

  • name (String)

    field name

Returns:

  • (Cassandra::UserDefined::Field, nil)

    a field with this name or nil



1249
1250
1251
# File 'lib/cassandra/types.rb', line 1249

def field(name)
  @fields.find { |f| f.name == name }
end

#has_field?(name) ⇒ Boolean

Returns whether this type has a given field.

Parameters:

  • name (String)

    field name

Returns:

  • (Boolean)

    whether this type has a given field



1225
1226
1227
# File 'lib/cassandra/types.rb', line 1225

def has_field?(name)
  @fields.any? { |f| f.name == name }
end

#hashObject



1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
# File 'lib/cassandra/types.rb', line 1309

def hash
  @hash ||= begin
    h = 17
    h = 31 * h + @kind.hash
    h = 31 * h + @keyspace.hash
    h = 31 * h + @name.hash
    h = 31 * h + @fields.hash
    h
  end
end

#new(*value) ⇒ Cassandra::UDT

Coerces the value to Cassandra::UDT

Parameters:

  • value (Object)

    original value

Returns:

See Also:



1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
# File 'lib/cassandra/types.rb', line 1257

def new(*value)
  value = value.first if value.one?
  value = Array(value) unless value.is_a?(::Hash)

  Util.assert(value.size <= @fields.size) do
    "too many values: #{value.size} out of #{@fields.size}"
  end

  case value
  when ::Array
    result = ::Hash.new
    value.each_with_index do |v, i|
      f = @fields[i]
      Util.assert_type(f.type, v)
      result[f.name] = v
    end
  when ::Hash
    result = ::Hash.new
    @fields.each do |f|
      n = f.name
      v = value[n]
      Util.assert_type(f.type, v)
      result[n] = v
    end
  end

  Cassandra::UDT::Strict.new(@keyspace, @name, @fields, result)
end

#to_cqlObject

Output this type in CQL



1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
# File 'lib/cassandra/types.rb', line 1330

def to_cql
  cql = "CREATE TYPE #{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} " \
                  "(\n"
  first = true

  @fields.each do |field|
    if first
      first = false
    else
      cql << ",\n" unless first
    end
    cql << "  #{field.name} #{type_to_cql(field.type)}"
  end

  cql << "\n);"

  cql
end

#to_sString

Returns "keyspace.name".

Returns:

  • (String)

    "keyspace.name"

See Also:



1304
1305
1306
1307
# File 'lib/cassandra/types.rb', line 1304

def to_s
  "#{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} " \
                  "{#{@fields.join(', ')}}"
end