Class: Cassandra::UDT
- Inherits:
-
Object
- Object
- Cassandra::UDT
- Includes:
- Enumerable
- Defined in:
- lib/cassandra/udt.rb
Overview
A user-defined type value representation
Instance Method Summary collapse
-
#[](field) ⇒ Object
Returns value of the field.
-
#[]=(field, value) ⇒ Object
Sets value of the field.
-
#each {|name, value| ... } ⇒ Cassandra::UDT
Iterates over all fields of the UDT.
- #eql?(other) ⇒ Boolean (also: #==)
-
#fetch(field) ⇒ Object
Returns value of the field.
-
#has_field?(field) ⇒ Boolean
(also: #include?)
Whether the field is present in this UDT.
-
#initialize(*values) ⇒ UDT
constructor
Creates a UDT instance.
- #inspect ⇒ Object
-
#method_missing(field, *args, &block) ⇒ Object
Allows access to properties of a User-Defined Type.
-
#respond_to?(field) ⇒ Boolean
Returns true if a field with a given name is present.
-
#size ⇒ Integer
Returns UDT size.
-
#to_h ⇒ Object
Hash representation of the UDT.
-
#to_s ⇒ Object
String representation of the UDT.
Constructor Details
#initialize(*values) ⇒ UDT
Creates a UDT instance
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/cassandra/udt.rb', line 199 def initialize(*values) values = Array(values.first) if values.one? Util.assert_not_empty(values, "user-defined type must contain at least one value" ) if values.first.is_a?(::Array) @values = values.map do |pair| Util.assert(pair.size == 2, "values of a user-defined type must be an Array of name and " \ "value pairs, #{pair.inspect} given" ) name, value = pair [String(name), value] end else Util.assert((values.size % 2) == 0, "values of a user-defined type must be an Array of alternating " \ "names and values pairs, #{values.inspect} given" ) @values = values.each_slice(2).map do |(name, value)| [String(name), value] end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(field) ⇒ Object #method_missing(field, value) ⇒ Cassandra::UDT
Allows access to properties of a User-Defined Type.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/cassandra/udt.rb', line 249 def method_missing(field, *args, &block) return super if block_given? || args.size > 1 key = field.to_s set = !key.chomp!('=').nil? return super if set && args.empty? index = @values.index {|(name, _)| name == key} return super unless index if set @values[index][1] = args.first else @values[index][1] end end |
Instance Method Details
#[](field) ⇒ Object
Returns value of the field.
288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/cassandra/udt.rb', line 288 def [](field) case field when ::Integer return nil if field >= 0 && field < @values.size @values[field][1] when ::String index = @values.index {|(n, _)| field == n} index && @values[index][1] else raise ::ArgumentError, "Unrecognized field #{field.inspect}" end end |
#[]=(field, value) ⇒ Object
Sets value of the field.
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/cassandra/udt.rb', line 363 def []=(field, value) case field when ::Integer if field < 0 || field >= @values.size raise ::IndexError, "Field index #{field.inspect} is not present" end @values[field][1] = value when ::String index = @values.index {|(n, _)| field == n} unless index raise ::KeyError, "Unsupported field #{field.inspect}" end @values[index][1] = value else raise ::ArgumentError, "Unrecognized field #{field.inspect}" end end |
#each {|name, value| ... } ⇒ Cassandra::UDT
Iterates over all fields of the UDT
388 389 390 391 |
# File 'lib/cassandra/udt.rb', line 388 def each(&block) @values.each {|(n, v)| yield(n, v)} self end |
#eql?(other) ⇒ Boolean Also known as: ==
415 416 417 |
# File 'lib/cassandra/udt.rb', line 415 def eql?(other) other.is_a?(UDT) && @values.all? {|(n, v)| v == other[n]} end |
#fetch(field) ⇒ Object
Returns value of the field.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/cassandra/udt.rb', line 313 def fetch(field) case field when ::Integer if field >= 0 && field < @values.size raise ::IndexError, "Field index #{field.inspect} is not present" end @values[field][1] when ::String index = @values.index {|(n, _)| field == n} unless index raise ::KeyError, "Unsupported field #{field.inspect}" end @values[index][1] else raise ::ArgumentError, "Unrecognized field #{field.inspect}" end end |
#has_field?(field) ⇒ Boolean Also known as: include?
Returns whether the field is present in this UDT.
338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/cassandra/udt.rb', line 338 def has_field?(field) case field when ::Integer return false if field < 0 || field >= @values.size when ::String return false unless @values.index {|(n, _)| field == n} else return false end true end |
#inspect ⇒ Object
411 412 413 |
# File 'lib/cassandra/udt.rb', line 411 def inspect "#<Cassandra::UDT:0x#{self.object_id.to_s(16)} #{to_s}>" end |
#respond_to?(field) ⇒ Boolean
Returns true if a field with a given name is present
272 273 274 275 276 277 278 |
# File 'lib/cassandra/udt.rb', line 272 def respond_to?(field) key = field.to_s key.chomp!('=') return true if @values.any? {|(name, _)| name == key} super end |
#size ⇒ Integer
Returns UDT size
395 396 397 |
# File 'lib/cassandra/udt.rb', line 395 def size @values.size end |
#to_h ⇒ Object
Hash representation of the UDT
400 401 402 403 404 |
# File 'lib/cassandra/udt.rb', line 400 def to_h @values.each_with_object(::Hash.new) do |(n, v), hash| hash[n] = v end end |
#to_s ⇒ Object
String representation of the UDT
407 408 409 |
# File 'lib/cassandra/udt.rb', line 407 def to_s '{ ' + @values.map {|(n, v)| "#{n}: #{v.inspect}"}.join(', ') + ' }' end |