Class: Jvertica::Row

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jvertica/row.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.



8
9
10
11
12
13
# File 'lib/jvertica/row.rb', line 8

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



68
69
70
71
72
73
74
75
76
# File 'lib/jvertica/row.rb', line 68

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.



3
4
5
# File 'lib/jvertica/row.rb', line 3

def labels
  @labels
end

#rownumObject (readonly)

Returns the value of attribute rownum.



3
4
5
# File 'lib/jvertica/row.rb', line 3

def rownum
  @rownum
end

#valuesObject (readonly)

Returns the value of attribute values.



3
4
5
# File 'lib/jvertica/row.rb', line 3

def values
  @values
end

Instance Method Details

#[](*idx) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jvertica/row.rb', line 15

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



32
33
34
35
36
# File 'lib/jvertica/row.rb', line 32

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

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/jvertica/row.rb', line 54

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

#hashObject



58
59
60
# File 'lib/jvertica/row.rb', line 58

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

#inspectObject



38
39
40
41
42
43
44
# File 'lib/jvertica/row.rb', line 38

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

#join(sep = $OUTPUT_FIELD_SEPARATOR) ⇒ Object



50
51
52
# File 'lib/jvertica/row.rb', line 50

def join(sep = $OUTPUT_FIELD_SEPARATOR)
  to_a.join(sep)
end

#to_aObject



46
47
48
# File 'lib/jvertica/row.rb', line 46

def to_a
  @values
end

#to_hObject



62
63
64
# File 'lib/jvertica/row.rb', line 62

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