Module: Cequel::Type

Defined in:
lib/cequel/type.rb

Overview

The Type module encapsulates information about the CQL3 type system. Each type has a ‘cql_name`, which is the name of the type as defined in CQL, and an `internal_name`, which is the name of the type in the lower-level interface that is exposed when introspecting table information in the database.

As well as knowing their respective names, types also know how to cast Ruby objects to the correct canonical class corresponding to the type. These implicit types are used by the underlying ‘cassandra-cql` library to determine how to represent values when passing them to Cassandra.

Since:

  • 1.0.0

Defined Under Namespace

Classes: Ascii, Base, Bigint, Blob, Boolean, Counter, Decimal, Double, Float, Inet, Int, String, Text, Timestamp, Timeuuid, Uuid, Varint

Constant Summary collapse

UnknownType =

Raised if an unknown type is looked up

Since:

  • 1.0.0

Class.new(ArgumentError)
BY_CQL_NAME =

Since:

  • 1.0.0

{}
BY_INTERNAL_NAME =

Since:

  • 1.0.0

{}

Class Method Summary collapse

Class Method Details

.[](cql_name) ⇒ Base

Return a type corresponding to the given input

Parameters:

  • cql_name (Symbol, Base)

    CQL name of a type, or a type

Returns:

  • (Base)

    type with the given CQL name

Since:

  • 1.0.0



45
46
47
# File 'lib/cequel/type.rb', line 45

def self.[](cql_name)
  cql_name.is_a?(Base) ? cql_name : lookup_cql(cql_name)
end

.lookup_cql(cql_name) ⇒ Base

Look up a type by CQL name

Parameters:

  • cql_name (Symbol)

    CQL name of a type

Returns:

  • (Base)

    type with the given CQL name

Raises:

  • (UnknownType)

    if no type by that name is registered

Since:

  • 1.0.0



56
57
58
59
60
# File 'lib/cequel/type.rb', line 56

def self.lookup_cql(cql_name)
  BY_CQL_NAME.fetch(cql_name.to_sym)
rescue KeyError
  raise UnknownType, "Unrecognized CQL type #{cql_name.inspect}"
end

.lookup_internal(internal_name) ⇒ Base

Look up a type by internal name

Parameters:

  • internal_name (String)

    internal name of a type

Returns:

  • (Base)

    type with the given internal name

Raises:

  • (UnknownType)

    if no type by that name is registered

Since:

  • 1.0.0



69
70
71
72
73
# File 'lib/cequel/type.rb', line 69

def self.lookup_internal(internal_name)
  BY_INTERNAL_NAME.fetch(internal_name)
rescue KeyError
  raise UnknownType, "Unrecognized internal type #{internal_name.inspect}"
end

.register(type) ⇒ void

This method returns an undefined value.

Register a type for lookup

Parameters:

  • type (Type)

    a new type

Since:

  • 1.0.0



31
32
33
34
35
36
37
# File 'lib/cequel/type.rb', line 31

def self.register(type)
  BY_CQL_NAME[type.cql_name] = type
  type.cql_aliases.each { |aliaz| BY_CQL_NAME[aliaz] = type }
  type.internal_names.each do |internal_name|
    BY_INTERNAL_NAME[internal_name] = type
  end
end