Class: Cassandra::Types::UserDefined
- Inherits:
-
Cassandra::Type
- Object
- Cassandra::Type
- Cassandra::Types::UserDefined
- Defined in:
- lib/cassandra/types.rb
Defined Under Namespace
Classes: Field
Instance Attribute Summary collapse
-
#keyspace ⇒ String
readonly
Keyspace where this type is defined.
-
#name ⇒ String
readonly
Name of this type.
Attributes inherited from Cassandra::Type
Instance Method Summary collapse
-
#assert(value, message = nil, &block) ⇒ void
Asserts that a given value is an Cassandra::UDT.
-
#each_field(&block) ⇒ Object
(also: #fields)
Yield or enumerate each field defined in this type.
- #eql?(other) ⇒ Boolean (also: #==)
-
#field(name) ⇒ Cassandra::UserDefined::Field?
A field with this name or nil.
-
#has_field?(name) ⇒ Boolean
Whether this type has a given field.
- #hash ⇒ Object
-
#new(*value) ⇒ Cassandra::UDT
Coerces the value to Cassandra::UDT.
-
#to_cql ⇒ Object
Output this type in CQL.
-
#to_s ⇒ String
"keyspace.name"
.
Instance Attribute Details
#keyspace ⇒ String (readonly)
Returns keyspace where this type is defined.
1207 1208 1209 |
# File 'lib/cassandra/types.rb', line 1207 def keyspace @keyspace end |
#name ⇒ String (readonly)
Returns 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
1293 1294 1295 1296 1297 1298 1299 1300 |
# File 'lib/cassandra/types.rb', line 1293 def assert(value, = nil, &block) Util.assert_instance_of(Cassandra::UDT, value, , &block) Util.assert(value.size <= @fields.size, , &block) @fields.each do |field| Util.assert_type(field.type, value[field.name], , &block) end nil end |
#each_field {|field| ... } ⇒ Cassandra::Types::UserDefined #each_field ⇒ Array<Array<String, Cassandra::Type>> Also known as: fields
Yield or enumerate each field defined in this type
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: ==
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.
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.
1225 1226 1227 |
# File 'lib/cassandra/types.rb', line 1225 def has_field?(name) @fields.any? { |f| f.name == name } end |
#hash ⇒ Object
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
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_cql ⇒ Object
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_s ⇒ String
Returns "keyspace.name"
.
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 |