Module: Cassandra::Types

Extended by:
Types
Included in:
Types
Defined in:
lib/cassandra/types.rb

Defined Under Namespace

Classes: Ascii, Bigint, Blob, Boolean, Counter, Custom, Decimal, Double, Float, Inet, Int, List, Map, Set, Text, Timestamp, Timeuuid, Tuple, UserDefined, Uuid, Varchar, Varint

Instance Method Summary collapse

Instance Method Details

#asciiCassandra::Types::Ascii

Returns ascii type.

Returns:



1222
1223
1224
# File 'lib/cassandra/types.rb', line 1222

def ascii
  Ascii
end

#bigintCassandra::Types::Bigint

Returns bigint type.

Returns:



1227
1228
1229
# File 'lib/cassandra/types.rb', line 1227

def bigint
  Bigint
end

#blobCassandra::Types::Blob

Returns blob type.

Returns:



1217
1218
1219
# File 'lib/cassandra/types.rb', line 1217

def blob
  Blob
end

#booleanCassandra::Types::Boolean

Returns boolean type.

Returns:



1247
1248
1249
# File 'lib/cassandra/types.rb', line 1247

def boolean
  Boolean
end

#counterCassandra::Types::Counter

Returns counter type.

Returns:



1232
1233
1234
# File 'lib/cassandra/types.rb', line 1232

def counter
  Counter
end

#custom(name) ⇒ Cassandra::Types::Custom

Returns custom type.

Parameters:

  • name (String)

    name of the custom type

Returns:



1402
1403
1404
# File 'lib/cassandra/types.rb', line 1402

def custom(name)
  Custom.new(name)
end

#decimalCassandra::Types::Decimal

Returns decimal type.

Returns:



1252
1253
1254
# File 'lib/cassandra/types.rb', line 1252

def decimal
  Decimal
end

#doubleCassandra::Types::Double

Returns double type.

Returns:



1257
1258
1259
# File 'lib/cassandra/types.rb', line 1257

def double
  Double
end

#floatCassandra::Types::Float

Returns float type.

Returns:



1262
1263
1264
# File 'lib/cassandra/types.rb', line 1262

def float
  Float
end

#inetCassandra::Types::Inet

Returns inet type.

Returns:



1267
1268
1269
# File 'lib/cassandra/types.rb', line 1267

def inet
  Inet
end

#intCassandra::Types::Int

Returns int type.

Returns:



1237
1238
1239
# File 'lib/cassandra/types.rb', line 1237

def int
  Int
end

#list(value_type) ⇒ Cassandra::Types::Map

Returns map type.

Parameters:

Returns:



1288
1289
1290
1291
1292
1293
1294
# File 'lib/cassandra/types.rb', line 1288

def list(value_type)
  Util.assert_instance_of(Cassandra::Type, value_type,
    "list type must be a Cassandra::Type, #{value_type.inspect} given"
  )

  List.new(value_type)
end

#map(key_type, value_type) ⇒ Cassandra::Types::Varchar

Returns varchar type.

Parameters:

Returns:



1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
# File 'lib/cassandra/types.rb', line 1299

def map(key_type, value_type)
  Util.assert_instance_of(Cassandra::Type, key_type,
    "map key type must be a Cassandra::Type, #{key_type.inspect} given"
  )
  Util.assert_instance_of(Cassandra::Type, value_type,
    "map value type must be a Cassandra::Type, #{value_type.inspect} given"
  )

  Map.new(key_type, value_type)
end

#set(value_type) ⇒ Cassandra::Types::Set

Returns set type.

Parameters:

Returns:



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

def set(value_type)
  Util.assert_instance_of(Cassandra::Type, value_type,
    "set type must be a Cassandra::Type, #{value_type.inspect} given"
  )

  Set.new(value_type)
end

#textCassandra::Types::Text

Returns text type.

Returns:



1212
1213
1214
# File 'lib/cassandra/types.rb', line 1212

def text
  Text
end

#timestampCassandra::Types::Timestamp

Returns timestamp type.

Returns:



1272
1273
1274
# File 'lib/cassandra/types.rb', line 1272

def timestamp
  Timestamp
end

#timeuuidCassandra::Types::Timeuuid

Returns timeuuid type.

Returns:



1282
1283
1284
# File 'lib/cassandra/types.rb', line 1282

def timeuuid
  Timeuuid
end

#tuple(*members) ⇒ Cassandra::Types::Tuple

Returns tuple type.

Parameters:

Returns:



1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
# File 'lib/cassandra/types.rb', line 1322

def tuple(*members)
  Util.assert_not_empty(members, "tuple must contain at least one member")
  members.each do |member|
    Util.assert_instance_of(Cassandra::Type, member,
      "each tuple member must be a Cassandra::Type, " \
      "#{member.inspect} given"
    )
  end

  Tuple.new(*members)
end

#udt(keyspace, name, *fields) ⇒ Cassandra::Types::UserDefined

Creates a User Defined Type instance

Examples:

Various ways of defining the same UDT

include Cassandra::Types

udt('simplex', 'address', {'street' => varchar, 'city' => varchar, 'state' => varchar, 'zip' => varchar}) #=> simplex.address

udt('simplex', 'address', [['street', varchar], ['city', varchar], ['state', varchar], ['zip', varchar]]) #=> simplex.address

udt('simplex', 'address', ['street', varchar], ['city', varchar], ['state', varchar], ['zip', varchar]) #=> simplex.address

udt('simplex', 'address', 'street', varchar, 'city', varchar, 'state', varchar, 'zip', varchar) #=> simplex.address

Parameters:

Returns:



1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
# File 'lib/cassandra/types.rb', line 1352

def udt(keyspace, name, *fields)
  keyspace = String(keyspace)
  name     = String(name)
  fields   = Array(fields.first) if fields.one?

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

  if fields.first.is_a?(::Array)
    fields = fields.map do |pair|
      Util.assert(pair.size == 2,
        "fields of a user-defined type must be an Array of name and " \
        "value pairs, #{pair.inspect} given"
      )
      Util.assert_instance_of(::String, pair[0],
        "each field name for a user-defined type must be a String, " \
        "#{pair[0].inspect} given"
      )
      Util.assert_instance_of(Cassandra::Type, pair[1],
        "each field type for a user-defined type must be a " \
        "Cassandra::Type, #{pair[1].inspect} given"
      )

      UserDefined::Field.new(*pair)
    end
  else
    Util.assert((fields.size % 2) == 0,
      "fields of a user-defined type must be an Array of alternating " \
      "names and values pairs, #{fields.inspect} given"
    )
    fields = fields.each_slice(2).map do |field_name, field_type|
      Util.assert_instance_of(::String, field_name,
        "each field name for a user-defined type must be a String, " \
        "#{field_name.inspect} given"
      )
      Util.assert_instance_of(Cassandra::Type, field_type,
        "each field type for a user-defined type must be a " \
        "Cassandra::Type, #{field_type.inspect} given"
      )

      UserDefined::Field.new(field_name, field_type)
    end
  end

  UserDefined.new(keyspace, name, fields)
end

#uuidCassandra::Types::Uuid

Returns uuid type.

Returns:



1277
1278
1279
# File 'lib/cassandra/types.rb', line 1277

def uuid
  Uuid
end

#varcharCassandra::Types::Varchar

Returns varchar type.

Returns:



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

def varchar
  Varchar
end

#varintCassandra::Types::Varint

Returns varint type.

Returns:



1242
1243
1244
# File 'lib/cassandra/types.rb', line 1242

def varint
  Varint
end