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.
-
#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.
-
#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
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/cassandra/udt.rb', line 223 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.even?, '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.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/cassandra/udt.rb', line 270 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.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/cassandra/udt.rb', line 309 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.
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/cassandra/udt.rb', line 380 def []=(field, value) case field when ::Integer raise ::IndexError, "Field index #{field.inspect} is not present" if field < 0 || field >= @values.size @values[field][1] = value when ::String index = @values.index {|(n, _)| field == n} raise ::KeyError, "Unsupported field #{field.inspect}" unless index @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
401 402 403 404 |
# File 'lib/cassandra/udt.rb', line 401 def each(&block) @values.each {|(n, v)| yield(n, v)} self end |
#fetch(field) ⇒ Object
Returns value of the field.
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/cassandra/udt.rb', line 334 def fetch(field) case field when ::Integer raise ::IndexError, "Field index #{field.inspect} is not present" if field >= 0 && field < @values.size @values[field][1] when ::String index = @values.index {|(n, _)| field == n} raise ::KeyError, "Unsupported field #{field.inspect}" unless index @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.
355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/cassandra/udt.rb', line 355 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 |
#respond_to?(field) ⇒ Boolean
Returns true if a field with a given name is present
293 294 295 296 297 298 299 |
# File 'lib/cassandra/udt.rb', line 293 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
408 409 410 |
# File 'lib/cassandra/udt.rb', line 408 def size @values.size end |
#to_h ⇒ Object
Hash representation of the UDT
413 414 415 416 417 |
# File 'lib/cassandra/udt.rb', line 413 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
420 421 422 |
# File 'lib/cassandra/udt.rb', line 420 def to_s '{ ' + @values.map {|(n, v)| "#{n}: #{v.inspect}"}.join(', ') + ' }' end |