Class: JDBCHelper::Connection::Row
- Inherits:
-
Object
- Object
- JDBCHelper::Connection::Row
- Includes:
- Enumerable
- Defined in:
- lib/jdbc-helper/connection/row.rb
Overview
end
Instance Attribute Summary collapse
-
#labels ⇒ Array
(also: #keys)
readonly
Labels of the columns.
-
#rownum ⇒ Fixnum
readonly
Sequential row number assigned within the scope of the query.
-
#values ⇒ Array
readonly
Values in Array.
Instance Method Summary collapse
- #[](*idx) ⇒ Object
- #each {|Object| ... } ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#hash ⇒ Object
:nodoc:.
- #inspect ⇒ String
- #join(sep = $,) ⇒ String
- #to_a ⇒ Array
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symb, *args) ⇒ Object (private)
Performs better than defining methods
119 120 121 122 123 124 125 126 127 |
# File 'lib/jdbc-helper/connection/row.rb', line 119 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
#labels ⇒ Array (readonly) Also known as: keys
Returns Labels of the columns.
28 29 30 |
# File 'lib/jdbc-helper/connection/row.rb', line 28 def labels @labels end |
#rownum ⇒ Fixnum (readonly)
Returns 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 |
#values ⇒ Array (readonly)
Returns 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
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
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: ==
92 93 94 |
# File 'lib/jdbc-helper/connection/row.rb', line 92 def eql?(other) self.hash == other.hash end |
#hash ⇒ Object
:nodoc:
96 97 98 |
# File 'lib/jdbc-helper/connection/row.rb', line 96 def hash # :nodoc: @labels.zip(@values).sort.hash end |
#inspect ⇒ 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
87 88 89 |
# File 'lib/jdbc-helper/connection/row.rb', line 87 def join(sep = $,) to_a.join(sep) end |
#to_a ⇒ Array
82 83 84 |
# File 'lib/jdbc-helper/connection/row.rb', line 82 def to_a @values end |