Class: DbMod::Statements::Configuration::As::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/db_mod/statements/configuration/as/json.rb

Overview

Coercer which converts an SQL result set into a string formatted as a JSON array. May be enabled for a prepared method or statement method using .as(:json):

def_statement(:a, 'SELECT a, b FROM foo') { as(:json) }
def_prepared(:b, 'SELECT b, c FROM bar') { as(:json) }

def do_stuff
 a # => '[{"a":"x","b":"y"},...]'
end

Class Method Summary collapse

Class Method Details

.call(results) ⇒ String

Formats the SQL results as a JSON object.

Parameters:

  • results (Object)

    SQL result set

Returns:

  • (String)

    a JSON formatted string



21
22
23
24
25
26
# File 'lib/db_mod/statements/configuration/as/json.rb', line 21

def self.call(results)
  # For compatibility with single(:row)
  return results.to_json if results.is_a? Hash

  results.to_a.to_json
end