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



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

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



330
331
332
333
334
335
336
337
338
# File 'lib/jvertica.rb', line 330

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.



265
266
267
# File 'lib/jvertica.rb', line 265

def labels
  @labels
end

#rownumObject (readonly)

Returns the value of attribute rownum.



265
266
267
# File 'lib/jvertica.rb', line 265

def rownum
  @rownum
end

#valuesObject (readonly)

Returns the value of attribute values.



265
266
267
# File 'lib/jvertica.rb', line 265

def values
  @values
end

Instance Method Details

#[](*idx) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/jvertica.rb', line 270

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



287
288
289
290
291
# File 'lib/jvertica.rb', line 287

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

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



309
310
311
# File 'lib/jvertica.rb', line 309

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

#hashObject



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

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

#inspectObject



293
294
295
296
297
298
299
# File 'lib/jvertica.rb', line 293

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

#join(sep = $OUTPUT_FIELD_SEPARATOR) ⇒ Object



305
306
307
# File 'lib/jvertica.rb', line 305

def join sep = $OUTPUT_FIELD_SEPARATOR
  to_a.join sep
end

#to_aObject



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

def to_a
  @values
end

#to_hObject



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

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