Class: Simmer::Externals::SqlWriters::SqlFixture

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/simmer/externals/sql_writers/sql_fixture.rb

Overview

This class knows how to turn a fixture into sql.

Instance Method Summary collapse

Constructor Details

#initialize(client, fixture) ⇒ SqlFixture

Returns a new instance of SqlFixture.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
# File 'lib/simmer/externals/sql_writers/sql_fixture.rb', line 21

def initialize(client, fixture)
  raise ArgumentError, 'fixture is required' unless fixture

  @client  = client
  @fixture = fixture

  freeze
end

Instance Method Details

#to_sqlObject



30
31
32
33
34
35
# File 'lib/simmer/externals/sql_writers/sql_fixture.rb', line 30

def to_sql
  sql_columns = fields.keys.map { |k| "`#{k}`" }.join(',')
  sql_values  = fields.values.map { |v| "'#{client.escape(v.to_s)}'" }.join(',')

  "INSERT INTO `#{table}` (#{sql_columns}) VALUES (#{sql_values})"
end