File: reader.rb

package info (click to toggle)
ruby-dataobjects 0.10.8-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 416 kB
  • sloc: ruby: 2,917; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,100 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module DataObjects
  # Abstract class to read rows from a query result
  class Reader

    include Enumerable

    # Return the array of field names
    def fields
      raise NotImplementedError.new
    end

    # Return the array of field values for the current row. Not legal after next! has returned false or before it's been called
    def values
      raise NotImplementedError.new
    end

    # Close the reader discarding any unread results.
    def close
      raise NotImplementedError.new
    end

    # Discard the current row (if any) and read the next one (returning true), or return nil if there is no further row.
    def next!
      raise NotImplementedError.new
    end

    # Return the number of fields in the result set.
    def field_count
      raise NotImplementedError.new
    end

    # Yield each row to the given block as a Hash
    def each
      begin
        while next!
          row = {}
          fields.each_with_index { |field, index| row[field] = values[index] }
          yield row
        end
      ensure
        close
      end
      self
    end

  end
end