Class: Cassandra::Types::UserDefined

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

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyspaceString (readonly)

Returns keyspace where this type is defined.

Returns:

  • (String)

    keyspace where this type is defined



1021
1022
1023
# File 'lib/cassandra/types.rb', line 1021

def keyspace
  @keyspace
end

#nameString (readonly)

Returns name of this type.

Returns:

  • (String)

    name of this type



1024
1025
1026
# File 'lib/cassandra/types.rb', line 1024

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:



1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/cassandra/types.rb', line 1109

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:



1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/cassandra/types.rb', line 1048

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

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

Returns:



1124
1125
1126
1127
1128
1129
# File 'lib/cassandra/types.rb', line 1124

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



1061
1062
1063
# File 'lib/cassandra/types.rb', line 1061

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



1038
1039
1040
# File 'lib/cassandra/types.rb', line 1038

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

#kindSymbol

Returns :udt.

Returns:

  • (Symbol)

    :udt

See Also:



1067
1068
1069
# File 'lib/cassandra/types.rb', line 1067

def kind
  :udt
end

#new(*value) ⇒ Cassandra::UDT

Coerces the value to Cassandra::UDT

Parameters:

  • value (Object)

    original value

Returns:

See Also:



1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/cassandra/types.rb', line 1075

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

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

  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



1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'lib/cassandra/types.rb', line 1133

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:



1120
1121
1122
# File 'lib/cassandra/types.rb', line 1120

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