Class: JDBCHelper::Connection::Row

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jdbc-helper/connection/row.rb

Overview

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symb, *args) ⇒ Object (private)

Performs better than defining methods



124
125
126
127
128
129
130
131
132
# File 'lib/jdbc-helper/connection/row.rb', line 124

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

#labelsArray (readonly) Also known as: keys

Returns Labels of the columns.

Returns:

  • (Array)

    Labels of the columns



28
29
30
# File 'lib/jdbc-helper/connection/row.rb', line 28

def labels
  @labels
end

#rownumFixnum (readonly)

Returns Sequential row number assigned within the scope of the query.

Returns:

  • (Fixnum)

    Sequential row number assigned within the scope of the query



32
33
34
# File 'lib/jdbc-helper/connection/row.rb', line 32

def rownum
  @rownum
end

#valuesArray (readonly)

Returns Values in Array.

Returns:

  • (Array)

    Values in Array



30
31
32
# File 'lib/jdbc-helper/connection/row.rb', line 30

def values
  @values
end

Instance Method Details

#[](idx) ⇒ Object #[](offset, len) ⇒ Array

Overloads:

  • #[](idx) ⇒ Object

    Parameters:

    • idx (Fixnum/String/Symbol/Range)

      Access index

    Returns:

    • (Object)
  • #[](offset, len) ⇒ Array

    Parameters:

    • offset (Fixnum)

      Start offset

    • len (Fixnum)

      Length of Array

    Returns:

    • (Array)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jdbc-helper/connection/row.rb', line 44

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
    # case-insensitive, assuming no duplicate labels
    vidx = @labels_d.index(idx.to_s.downcase) or
      raise NameError.new("Unknown column label: #{idx}")
    @values[vidx]
  else
    # See how it goes...
    @values[idx]
  end
end

#each {|Object| ... } ⇒ Object

Yields:

  • (Object)


64
65
66
67
68
69
70
# File 'lib/jdbc-helper/connection/row.rb', line 64

def each(&blk)
  @values.each { | v | yield v }

  # @labels.each_with_index do | label, idx |
  #   yield label, @values[idx]
  # end
end

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

Returns:

  • (Boolean)


92
93
94
# File 'lib/jdbc-helper/connection/row.rb', line 92

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

#hashObject

:nodoc:



96
97
98
# File 'lib/jdbc-helper/connection/row.rb', line 96

def hash # :nodoc:
  @labels.zip(@values).sort.hash
end

#inspectString

Returns:

  • (String)


73
74
75
76
77
78
79
# File 'lib/jdbc-helper/connection/row.rb', line 73

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

#join(sep = $,) ⇒ String

Returns:

  • (String)


87
88
89
# File 'lib/jdbc-helper/connection/row.rb', line 87

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

#to_aArray

Returns:

  • (Array)


82
83
84
# File 'lib/jdbc-helper/connection/row.rb', line 82

def to_a
  @values
end

#to_hHash

Returns:

  • (Hash)


101
102
103
# File 'lib/jdbc-helper/connection/row.rb', line 101

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