Class: RDBI::Result::Driver::YAML

Inherits:
RDBI::Result::Driver show all
Defined in:
lib/rdbi/result.rb

Overview

Yields YAML representations of rows, each as an array keyed by the column name (as symbol, not string).

For example, a table:

create table foo (i integer, x varchar);
insert into foo (i, x) values (1, "bar");
insert into foo (i, x) values (2, "foo");
insert into foo (i, x) values (3, "quux");

With a query as such:

dbh.execute("select * from foo").as(:YAML).fetch(:all)

Will yield:

--- 
- :i: 1
  :x: bar 
- :i: 2
  :x: foo
- :i: 3 
  :x: quux

Instance Method Summary collapse

Constructor Details

#initialize(result, *args) ⇒ YAML

Returns a new instance of YAML.



464
465
466
467
# File 'lib/rdbi/result.rb', line 464

def initialize(result, *args)
  super
  RDBI::Util.optional_require('yaml')
end

Instance Method Details

#format_multiple_rows(raw_rows) ⇒ Object



473
474
475
# File 'lib/rdbi/result.rb', line 473

def format_multiple_rows(raw_rows)
  raw_rows.collect { |row| ::Hash[column_names.zip(row)] }.to_yaml
end

#format_single_row(raw) ⇒ Object



469
470
471
# File 'lib/rdbi/result.rb', line 469

def format_single_row(raw)
  ::Hash[column_names.zip(raw)].to_yaml
end