-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.0.3: def_prepared/statement.as(:csv)
- Loading branch information
Showing
11 changed files
with
246 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
0.0.3 (2015-10-13) | ||
================== | ||
|
||
* Configurable method framework. Adds `def_prepared/statement.as(:csv)` - [@dslh](https://github.com/dslh) | ||
|
||
0.0.2 (2015-10-12) | ||
================== | ||
|
||
* Adds `def_statement` to compliment `def_prepared` - [@dslh](https://github.com/dslh). | ||
|
||
0.0.1 (2015-10-11) | ||
================== | ||
|
||
* Initial release - [@dslh](https://github.com/dslh). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require_relative 'as/csv' | ||
|
||
module DbMod | ||
# Contains coercers and other functions that allow | ||
# module instance methods returning an SQL result set | ||
# to be extended with additional result coercion and | ||
# formatting. The normal way to access this functionality | ||
# is via {DbMod::Statements::ConfigurableMethod#as}, | ||
# which is available when defining a statement method | ||
# or prepared method: | ||
# | ||
# def_statement(:a, 'SELECT a, b, c FROM foo').as(:csv) | ||
# def_prepared(:b, 'SELECT d, e, f FROM bar').as(:csv) | ||
module As | ||
# List of available result coercion methods. | ||
# Only keys defined here are allowed as arguments | ||
# to {DbMod::Statements::ConfigurableMethod#as}. | ||
COERCERS = { | ||
csv: DbMod::As::Csv | ||
} | ||
|
||
# Extend a method so that the SQL result set it | ||
# returns will be coerced to the given type. | ||
# See {COERCERS} for a list of defined coercion | ||
# methods. | ||
# | ||
# @param mod [Module] module where the method has been defined | ||
# @param name [Symbol] method name | ||
# @param type [Symbol] type to which result set should be coerced | ||
def self.extend_method(mod, name, type) | ||
unless COERCERS.key? type | ||
fail ArgumentError, "#{type} not in #{COERCERS.keys.join ', '}" | ||
end | ||
|
||
Statements.extend_method(mod, name, COERCERS[type]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module DbMod | ||
module As | ||
# Coercer which converts an SQL result set | ||
# into a string formatted as a CSV document. | ||
# May be enabled for a prepared method or | ||
# statement method using +.as(:csv)+: | ||
# | ||
# def_statement(:a, 'SELECT a, b FROM foo').as(:csv) | ||
# def_prepared(:b, 'SELECT b, c FROM bar').as(:csv) | ||
# | ||
# def do_stuff | ||
# a # => "a,b\r\n1,2\r\n3,4\r\n..." | ||
# end | ||
module Csv | ||
# Enables this module to be passed to {DbMod::Statements.extend_method} | ||
# as the +wrapper+ function, in which case it will retrieve the results | ||
# and format them as a CSV document using the column names from the | ||
# result set. | ||
# | ||
# @param wrapped_method [Method] the method that has been wrapped | ||
# @param args [*] arguments expected to be passed to the wrapped method | ||
# @return [String] a CSV formatted document | ||
def self.call(wrapped_method, *args) | ||
results = wrapped_method.call(*args) | ||
|
||
headers = nil | ||
CSV.generate do |csv| | ||
results.each do |row| | ||
csv << (headers = row.keys) unless headers | ||
|
||
csv << headers.map { |col| row[col] } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'db_mod/as' | ||
|
||
module DbMod | ||
module Statements | ||
# Encapsulates a method that has just been defined | ||
# via the dsl exposed in {DbMod::Statements} so that | ||
# it can be extended with additional processing such | ||
# as result coercion. | ||
# | ||
# The pattern here is something similar to rack's middleware. | ||
# Calling any of the extension methods below will replace | ||
# the original method defined by +def_prepared+ or +def_statement+ | ||
# with a wrapper function that may perform processing on given | ||
# arguments, pass them to the original function, then perform | ||
# additional processing on the result. | ||
class ConfigurableMethod | ||
# Encapsulate a method that has been newly defined | ||
# by a {DbMod} dsl function, for additional configuration. | ||
# | ||
# @param mod [Module] the {DbMod} enabled module | ||
# where the method was defined | ||
# @param name [Symbol] the method name | ||
def initialize(mod, name) | ||
@mod = mod | ||
@name = name | ||
end | ||
|
||
# Extend the method by converting results into a given | ||
# format, using one of the coercion methods defined | ||
# under {DbMod::As}. | ||
# | ||
# @param type [Symbol] for now, only :csv is accepted | ||
# @return [self] | ||
def as(type) | ||
DbMod::As.extend_method(@mod, @name, type) | ||
|
||
self | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Version information | ||
module DbMod | ||
# The current version of db_mod. | ||
VERSION = '0.0.2' | ||
VERSION = '0.0.3' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'spec_helper' | ||
require 'csv' | ||
|
||
describe DbMod::As::Csv do | ||
subject do | ||
Module.new do | ||
include DbMod | ||
|
||
def_statement(:statement, 'SELECT a, b FROM foo').as(:csv) | ||
def_prepared(:prepared, 'SELECT c, d FROM bar').as(:csv) | ||
end | ||
end | ||
|
||
before do | ||
@conn = instance_double 'PGconn' | ||
allow(@conn).to receive(:prepare) | ||
allow(PGconn).to receive(:connect).and_return @conn | ||
end | ||
|
||
{ | ||
statement: :query, | ||
prepared: :exec_prepared | ||
}.each do |method_type, exec_type| | ||
context "#{method_type} methods" do | ||
it 'coerces results to csv' do | ||
expect(@conn).to receive(exec_type).and_return([ | ||
{ 'a' => '1', 'b' => '2' }, | ||
{ 'a' => '3', 'b' => '4' } | ||
]) | ||
|
||
csv = subject.create(db: 'testdb').send(method_type) | ||
expect(csv).to eq("a,b\n1,2\n3,4\n") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'spec_helper' | ||
|
||
# See submodules for more | ||
describe DbMod::As do | ||
it 'disallows unknown coercions' do | ||
expect do | ||
Module.new do | ||
include DbMod | ||
|
||
def_statement(:foo, 'SELECT 1').as(:lolwut) | ||
end | ||
end.to raise_exception ArgumentError | ||
end | ||
end |