Class: Jvertica::Row

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jvertica.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(col_labels, col_labels_d, values, rownum) ⇒ Row

Returns a new instance of Row.



335
336
337
338
339
340
# File 'lib/jvertica.rb', line 335

def initialize col_labels, col_labels_d, values, rownum
  @labels = col_labels
  @labels_d = col_labels_d
  @values = values
  @rownum = rownum
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symb, *args) ⇒ Object



342
343
344
345
346
347
348
349
350
# File 'lib/jvertica.rb', line 342

def method_missing symb, *args
  if vidx = @labels_d.index(symb.to_s.downcase)
    @values[vidx]
  elsif @values.respond_to? symb
    @values.send symb, *args
  else
    raise NoMethodError.new("undefined method or attribute `#{symb}'")
  end
end

Instance Attribute Details

#labelsObject (readonly) Also known as: keys

Returns the value of attribute labels.



277
278
279
# File 'lib/jvertica.rb', line 277

def labels
  @labels
end

#rownumObject (readonly)

Returns the value of attribute rownum.



277
278
279
# File 'lib/jvertica.rb', line 277

def rownum
  @rownum
end

#valuesObject (readonly)

Returns the value of attribute values.



277
278
279
# File 'lib/jvertica.rb', line 277

def values
  @values
end

Instance Method Details

#[](*idx) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/jvertica.rb', line 282

def [] *idx
  return @values[*idx] if idx.length > 1

  idx = idx.first
  case idx
  when Fixnum
    raise RangeError.new("Index out of bound") if idx >= @values.length
    @values[idx]
  when String, Symbol
    vidx = @labels_d.index(idx.to_s.downcase) or
      raise NameError.new("Unknown column label: #{idx}")
    @values[vidx]
  else
    @values[idx]
  end
end

#each(&blk) ⇒ Object



299
300
301
302
303
# File 'lib/jvertica.rb', line 299

def each(&blk)
  @values.each do |v|
    yield v
  end
end

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

Returns:

  • (Boolean)


321
322
323
# File 'lib/jvertica.rb', line 321

def eql? other
  self.hash == other.hash
end

#hashObject



325
326
327
# File 'lib/jvertica.rb', line 325

def hash
  @labels.zip(@values).sort.hash
end

#inspectObject



305
306
307
308
309
310
311
# File 'lib/jvertica.rb', line 305

def inspect
  strs = []
  @labels.each do |col|
    strs << "#{col}: #{self[col] || '(null)'}"
  end
  '[' + strs.join(', ') + ']'
end

#join(sep = $OUTPUT_FIELD_SEPARATOR) ⇒ Object



317
318
319
# File 'lib/jvertica.rb', line 317

def join sep = $OUTPUT_FIELD_SEPARATOR
  to_a.join sep
end

#to_aObject



313
314
315
# File 'lib/jvertica.rb', line 313

def to_a
  @values
end

#to_hObject



329
330
331
# File 'lib/jvertica.rb', line 329

def to_h
  Hash[@labels.zip @values]
end