Class: RDBI::Result::Driver::JSON
- Inherits:
-
RDBI::Result::Driver
- Object
- RDBI::Result::Driver
- RDBI::Result::Driver::JSON
- Defined in:
- lib/rdbi/result/driver/json.rb
Instance Method Summary collapse
- #fetch(row_count) ⇒ Object
-
#initialize(result, *args) ⇒ JSON
constructor
A new instance of JSON.
Constructor Details
#initialize(result, *args) ⇒ JSON
Returns a new instance of JSON.
4 5 6 7 8 9 10 11 12 |
# File 'lib/rdbi/result/driver/json.rb', line 4 def initialize(result, *args) super @as_object = false if args[0].kind_of?(Hash) @as_object = args[0][:as_hash] || args[0][:as_object] end end |
Instance Method Details
#fetch(row_count) ⇒ Object
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 48 |
# File 'lib/rdbi/result/driver/json.rb', line 14 def fetch(row_count) if @as_object obj_hash = { } index = [] results = [] @result.schema.columns.map(&:name).each { |name| index.push(name); obj_hash[name] = nil } if [:first, :last].include?(row_count) my_hash = obj_hash.dup @result.raw_fetch(row_count)[0].each_with_index do |col, i| my_hash[index[i]] = col end return ::JSON.dump(my_hash) else @result.raw_fetch(row_count).each do |row| my_hash = obj_hash.dup row.each_with_index do |col, i| my_hash[index[i]] = col end results.push my_hash end File.open('/tmp/foo', 'w') << results.inspect return ::JSON.dump(results) end else if [:first, :last].include?(row_count) return ::JSON.dump(@result.raw_fetch(row_count)[0]) else return ::JSON.dump(@result.raw_fetch(row_count)) end end end |