inscriber 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/inscriber/config.rb +25 -0
- data/lib/inscriber/connection_string.rb +17 -0
- data/lib/inscriber/database.rb +38 -0
- data/lib/inscriber/downloader.rb +41 -0
- data/lib/inscriber/exporter.rb +17 -0
- data/lib/inscriber/inserter.rb +35 -0
- data/lib/inscriber/table_helper.rb +11 -0
- data/lib/inscriber/uploader.rb +59 -0
- data/lib/inscriber/version.rb +3 -0
- data/lib/inscriber.rb +32 -0
- data/spec/config_spec.rb +29 -0
- data/spec/connection_string_spec.rb +19 -0
- data/spec/database_spec.rb +12 -0
- data/spec/downloader_spec.rb +26 -0
- data/spec/exporter_spec.rb +35 -0
- data/spec/inserter_spec.rb +54 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/spec_helpers/config_helpers.rb +28 -0
- data/spec/spec_helpers/test_database.rb +79 -0
- data/spec/uploader_spec.rb +44 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 25cd71662f1327ab02f6eaac583c1ae8727e0036
|
4
|
+
data.tar.gz: a03f95e61cda151ca45875d2d606830ef9c91097
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa1a6431171baa7fb49b6809f31d8491ab9ac74e034638eb446ab3d91647b2b9aa5dcf0d4e416c5005c76ff41e3f5cbbc83f46e6a2629730e785a9a96b303c80
|
7
|
+
data.tar.gz: 53d2a1fcd1fd1183eb872bb8e9c28e5fa7756a18bf2a38eb025080ab3863588bd9e5e33472b9ff6ab8cf9cc6d984e18ae77f1495879b18bce153edebb15d3e15
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Inscriber
|
4
|
+
class Config
|
5
|
+
class << self
|
6
|
+
def data
|
7
|
+
path = ENV['INSCRIBER_CONFIG'] || 'config/inscriber.yml'
|
8
|
+
@data = symbolize_all_keys(YAML.load_file(path))
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def symbolize_all_keys(obj)
|
14
|
+
case obj
|
15
|
+
when Hash
|
16
|
+
obj.each_with_object({}){ |(k,v), h| h[k.to_sym] = symbolize_all_keys(v) }
|
17
|
+
when Array
|
18
|
+
obj.map{ |elem| symbolize_all_keys(elem) }
|
19
|
+
else
|
20
|
+
obj
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
|
3
|
+
module Inscriber
|
4
|
+
class ConnectionString
|
5
|
+
ADAPTER_STRINGS = {
|
6
|
+
mysql: "%{adapter}://%{username}:%{password}@%{host}/%{database_name}",
|
7
|
+
postgres: "%{adapter}://%{username}:%{password}@%{host}/%{database_name}",
|
8
|
+
sqlite: "%{adapter}://%{database_name}"
|
9
|
+
}
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def generate(options)
|
13
|
+
ADAPTER_STRINGS[options[:adapter].to_sym] % options
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Inscriber
|
2
|
+
class Database
|
3
|
+
attr_accessor :adapter, :host, :port, :username, :password, :source_lang
|
4
|
+
attr_accessor :output_dir, :input_dir, :file_name, :tables, :locales, :database_name
|
5
|
+
|
6
|
+
DEFAULT_HOST = '127.0.0.1'
|
7
|
+
DEFAULT_PORT = 5432
|
8
|
+
|
9
|
+
def initialize(options={})
|
10
|
+
@adapter = options.fetch(:adapter, nil)
|
11
|
+
@host = options.fetch(:host, DEFAULT_HOST)
|
12
|
+
@port = options.fetch(:port, DEFAULT_PORT)
|
13
|
+
@database_name = options.fetch(:database_name, nil)
|
14
|
+
@username = options.fetch(:username, '')
|
15
|
+
@password = options.fetch(:password, '')
|
16
|
+
@source_lang = options.fetch(:source_lang, 'en')
|
17
|
+
@output_dir = options.fetch(:output_dir, '.')
|
18
|
+
@input_dir = options.fetch(:input_dir, @output_dir)
|
19
|
+
@file_name = options.fetch(:file_name, 'translations')
|
20
|
+
@tables = options.fetch(:tables, nil)
|
21
|
+
@locales = options.fetch(:locales, nil)
|
22
|
+
end
|
23
|
+
|
24
|
+
def connection
|
25
|
+
@connection ||= Sequel.connect(connection_string)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def connection_string
|
31
|
+
Inscriber::ConnectionString.generate(connection_opts)
|
32
|
+
end
|
33
|
+
|
34
|
+
def connection_opts
|
35
|
+
{ adapter: adapter, host: host, port: port, database_name: database_name, username: username, password: password }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Inscriber
|
2
|
+
class Downloader
|
3
|
+
include TableHelpers
|
4
|
+
|
5
|
+
def initialize(database)
|
6
|
+
@db ||= connect_to_db(database)
|
7
|
+
@database = database
|
8
|
+
@result_hash = Hash.new(0)
|
9
|
+
end
|
10
|
+
|
11
|
+
def download
|
12
|
+
@database.tables.each do |table|
|
13
|
+
record_hash = Hash.new(0)
|
14
|
+
records = records_from_table(table[:name]).all
|
15
|
+
unless records.empty?
|
16
|
+
records.each do |record|
|
17
|
+
record_hash[record[:id]] = generate_hash_from_record(record, table)
|
18
|
+
end
|
19
|
+
@result_hash[table[:name]] = record_hash
|
20
|
+
end
|
21
|
+
end
|
22
|
+
@result_hash
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def connect_to_db(database)
|
28
|
+
database.connection
|
29
|
+
end
|
30
|
+
|
31
|
+
def records_from_table(table_name)
|
32
|
+
@db.from(table_name).where(locale: @database.source_lang)
|
33
|
+
end
|
34
|
+
|
35
|
+
def generate_hash_from_record(record, table)
|
36
|
+
record.select{ |k,v| table[:columns].include? k.to_s }
|
37
|
+
.merge(original_column_name(table[:name]).to_s => record[original_column_name(table[:name])])
|
38
|
+
.inject({}){ |h, (k,v)| h[k.to_s] = v; h }.to_h
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Inscriber
|
2
|
+
class Exporter
|
3
|
+
class << self
|
4
|
+
def export(database)
|
5
|
+
data = { "#{database.source_lang}" => download_from_db(database) }
|
6
|
+
File.open("#{database.output_dir}/#{database.file_name}.yml", "w") { |f| f.write data.to_yaml }
|
7
|
+
{ status: true }
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def download_from_db(database)
|
13
|
+
Inscriber::Downloader.new(database).download
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Inscriber
|
4
|
+
class Inserter
|
5
|
+
class << self
|
6
|
+
def insert(database)
|
7
|
+
database.locales.each do |locale|
|
8
|
+
file_path = "#{database.input_dir}/#{database.file_name}"
|
9
|
+
if File.exist? "#{file_path}.#{locale}.yml"
|
10
|
+
data = YAML.load_file("#{file_path}.#{locale}.yml")
|
11
|
+
source_data = YAML.load_file("#{file_path}.yml")
|
12
|
+
records_array = []
|
13
|
+
|
14
|
+
data[locale].each do |table, records|
|
15
|
+
records.each do |k,v|
|
16
|
+
records_array << source_data[database.source_lang][table][k].merge(v)
|
17
|
+
end
|
18
|
+
opts = { database: database, table: table, records: records_array, locale: locale }
|
19
|
+
upload_data_to_db(opts)
|
20
|
+
end
|
21
|
+
else
|
22
|
+
{ status: false, error: 'File not found' }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
{ status: true }
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def upload_data_to_db(opts)
|
31
|
+
Inscriber::Uploader.new(opts).upload
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Inscriber
|
2
|
+
class Uploader
|
3
|
+
include TableHelpers
|
4
|
+
attr_accessor :database, :table, :records, :locale
|
5
|
+
|
6
|
+
def initialize(opts)
|
7
|
+
@database = opts[:database]
|
8
|
+
@table = opts[:table]
|
9
|
+
@records = opts[:records]
|
10
|
+
@locale = opts[:locale]
|
11
|
+
end
|
12
|
+
|
13
|
+
def upload
|
14
|
+
records.each do |record|
|
15
|
+
update_or_create_record(record)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def db_connection
|
22
|
+
@db ||= database.connection
|
23
|
+
end
|
24
|
+
|
25
|
+
def db_from_table
|
26
|
+
db_connection.from(table)
|
27
|
+
end
|
28
|
+
|
29
|
+
def original_column
|
30
|
+
original_column_name(table)
|
31
|
+
end
|
32
|
+
|
33
|
+
def update_or_create_record(record)
|
34
|
+
row = db_from_table.where(
|
35
|
+
original_column => record[original_column.to_s],
|
36
|
+
:locale => locale)
|
37
|
+
|
38
|
+
if row.empty?
|
39
|
+
db_from_table.insert({
|
40
|
+
locale: locale}
|
41
|
+
.merge(created_at)
|
42
|
+
.merge(updated_at)
|
43
|
+
.merge(record))
|
44
|
+
else
|
45
|
+
row.update(record)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def created_at
|
50
|
+
return {} unless db_from_table.columns.include?(:created_at)
|
51
|
+
{ created_at: Time.now }
|
52
|
+
end
|
53
|
+
|
54
|
+
def updated_at
|
55
|
+
return {} unless db_from_table.columns.include?(:updated_at)
|
56
|
+
{ updated_at: Time.now }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/inscriber.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'inscriber/config'
|
2
|
+
require 'inscriber/connection_string'
|
3
|
+
require 'inscriber/database'
|
4
|
+
require 'inscriber/table_helper'
|
5
|
+
require 'inscriber/downloader'
|
6
|
+
require 'inscriber/uploader'
|
7
|
+
require 'inscriber/exporter'
|
8
|
+
require 'inscriber/inserter'
|
9
|
+
|
10
|
+
module Inscriber
|
11
|
+
class << self
|
12
|
+
def export
|
13
|
+
begin
|
14
|
+
@config ||= Inscriber::Config.data
|
15
|
+
@database ||= Inscriber::Database.new(@config)
|
16
|
+
Inscriber::Exporter.export(@database)
|
17
|
+
rescue => e
|
18
|
+
{ status: false, error: e }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def insert
|
23
|
+
begin
|
24
|
+
@config ||= Inscriber::Config.data
|
25
|
+
@database ||= Inscriber::Database.new(@config)
|
26
|
+
Inscriber::Inserter.insert(@database)
|
27
|
+
rescue => e
|
28
|
+
{ status: false, error: e }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'spec_helpers/test_database'
|
3
|
+
require 'spec_helpers/config_helpers'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'yaml'
|
6
|
+
|
7
|
+
describe Inscriber::Config do
|
8
|
+
let!(:file) do
|
9
|
+
file = Tempfile.new('inscriber.yml')
|
10
|
+
file.write(YAML.dump(Inscriber::ConfigHelpers.config))
|
11
|
+
file.rewind
|
12
|
+
file
|
13
|
+
end
|
14
|
+
|
15
|
+
let!(:data) do
|
16
|
+
ENV['INSCRIBER_CONFIG'] = "#{file.path}"
|
17
|
+
Inscriber::Config.data
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '.data' do
|
21
|
+
it 'loads object from inscriber.yml' do
|
22
|
+
expect(data[:adapter]).to eq('sqlite')
|
23
|
+
expect(data[:host]).to eq('localhost')
|
24
|
+
expect(data[:database_name]).to eq('test.sqlite')
|
25
|
+
file.close
|
26
|
+
file.unlink
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Inscriber::ConnectionString do
|
4
|
+
let!(:config) { Inscriber::ConfigHelpers.config }
|
5
|
+
describe '.generate' do
|
6
|
+
it 'returns mysql string when mysql adapter' do
|
7
|
+
config[:adapter] = 'mysql'
|
8
|
+
expect(Inscriber::ConnectionString.generate(config)).to be_a String
|
9
|
+
end
|
10
|
+
it 'returns postgres string when postgres adapter' do
|
11
|
+
config[:adapter] = 'postgres'
|
12
|
+
expect(Inscriber::ConnectionString.generate(config)).to be_a String
|
13
|
+
end
|
14
|
+
it 'returns sqlite string when sqlite adapter' do
|
15
|
+
config[:adapter] = 'sqlite'
|
16
|
+
expect(Inscriber::ConnectionString.generate(config)).to be_a String
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'spec_helpers/test_database'
|
3
|
+
|
4
|
+
describe Inscriber::Database do
|
5
|
+
let!(:database) { Inscriber::TestDatabase.setup }
|
6
|
+
|
7
|
+
describe '.connection' do
|
8
|
+
it 'should return a connection to a db' do
|
9
|
+
expect(database.connection).to be_a(Sequel::SQLite::Database)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
describe Inscriber::Downloader do
|
5
|
+
let!(:database) do
|
6
|
+
Inscriber::TestDatabase.setup do
|
7
|
+
create_table :test_translations do
|
8
|
+
primary_key :id
|
9
|
+
integer :test_id
|
10
|
+
string :body
|
11
|
+
string :locale
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '.download' do
|
17
|
+
it 'should download a hash of the data in the db' do
|
18
|
+
database.connection.from(:test_translations).insert(:body => 'test string', :test_id => 1, :locale => 'en')
|
19
|
+
download = Inscriber::Downloader.new(database).download
|
20
|
+
|
21
|
+
expect(download.keys).to include 'test_translations'
|
22
|
+
expect(download['test_translations'].values.first["body"]).to eq 'test string'
|
23
|
+
expect(download['test_translations'].values.first["test_id"]).to eq 1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Inscriber::Exporter do
|
4
|
+
let!(:database) do
|
5
|
+
Inscriber::TestDatabase.setup do
|
6
|
+
create_table :test_translations do
|
7
|
+
primary_key :id
|
8
|
+
integer :test_id
|
9
|
+
string :body
|
10
|
+
string :locale
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let :file do
|
16
|
+
database.connection.from(:test_translations).insert(:body => 'test string', :test_id => 1, :locale => 'en')
|
17
|
+
Inscriber::Exporter.export(database)
|
18
|
+
"#{database.file_name}.yml"
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '.export' do
|
22
|
+
let!(:data) { YAML.load_file(file) }
|
23
|
+
|
24
|
+
it 'should create a yml file from the downloaded data' do
|
25
|
+
expect(File).to exist(file)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should have the data in the yml file' do
|
29
|
+
expect(data.has_key? "en").to be true
|
30
|
+
expect(data["en"].has_key? "test_translations").to be true
|
31
|
+
expect(data["en"]["test_translations"].values.first["body"]).to eq 'test string'
|
32
|
+
File.unlink(file)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'spec_helpers/config_helpers'
|
3
|
+
|
4
|
+
describe Inscriber::Inserter do
|
5
|
+
let!(:database) do
|
6
|
+
Inscriber::TestDatabase.setup do
|
7
|
+
create_table :test_translations do
|
8
|
+
primary_key :id
|
9
|
+
integer :test_id
|
10
|
+
string :body
|
11
|
+
string :locale
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
let!(:config) do
|
17
|
+
{
|
18
|
+
"fr" => {
|
19
|
+
"test_translations" => {
|
20
|
+
1 => {
|
21
|
+
"body" => "Chaîne de test"
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
let! :file do
|
29
|
+
database.connection.from(:test_translations).insert(:body => 'test string', :test_id => 1, :locale => 'en')
|
30
|
+
Inscriber::Exporter.export(database)
|
31
|
+
"#{database.file_name}.yml"
|
32
|
+
end
|
33
|
+
|
34
|
+
let!(:translated_file) do
|
35
|
+
File.open('test.fr.yml', 'w') { |f| f.write config.to_yaml }
|
36
|
+
end
|
37
|
+
|
38
|
+
let(:db) do
|
39
|
+
database.connection.from(config["fr"].keys.first)
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '.insert' do
|
43
|
+
it 'should read the yml file and insert the data into the db' do
|
44
|
+
Inscriber::Inserter.insert(database)
|
45
|
+
query = {
|
46
|
+
:locale => config.keys.first,
|
47
|
+
:body => config["fr"]["test_translations"].values.first["body"]
|
48
|
+
}
|
49
|
+
expect(db.where(query).all.length).to be 1
|
50
|
+
File.unlink('test.fr.yml')
|
51
|
+
File.unlink('test.yml')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'sequel'
|
6
|
+
require 'sqlite3'
|
7
|
+
require 'inscriber'
|
8
|
+
|
9
|
+
require 'spec_helpers/test_database'
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.before(:each) do
|
13
|
+
Inscriber::TestDatabase.reset
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Inscriber
|
2
|
+
class ConfigHelpers
|
3
|
+
class << self
|
4
|
+
def config
|
5
|
+
{
|
6
|
+
adapter: 'sqlite',
|
7
|
+
host: 'localhost',
|
8
|
+
database_name: 'test.sqlite',
|
9
|
+
username: '',
|
10
|
+
password: '',
|
11
|
+
source_lang: 'en',
|
12
|
+
file_name: 'test',
|
13
|
+
input_dir: '.',
|
14
|
+
output_dir: '.',
|
15
|
+
tables: [
|
16
|
+
{
|
17
|
+
name: 'test_translations',
|
18
|
+
columns: ['body']
|
19
|
+
}
|
20
|
+
],
|
21
|
+
locales: [
|
22
|
+
'fr'
|
23
|
+
]
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require 'rspec'
|
3
|
+
require 'spec_helpers/config_helpers'
|
4
|
+
|
5
|
+
module Inscriber
|
6
|
+
# The TestDatabase is responsible for creating a SQLite database with tables and columns for testing.
|
7
|
+
# It is dumped after every example. To create a database, use the following format:
|
8
|
+
#
|
9
|
+
# Inscriber::TestDatabase.setup do
|
10
|
+
# create_table :test do
|
11
|
+
# primary_key :id
|
12
|
+
# string :body
|
13
|
+
# integer :test_app_id
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# Inscriber::TestDatabase.setup will return an instance of Inscriber::Database, which can be used for testing:
|
18
|
+
# database = Inscriber::TestDatabase.setup
|
19
|
+
# it 'should do something' do
|
20
|
+
# Inscriber::Downloader.new(database).download
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
class TestDatabase
|
24
|
+
class << self
|
25
|
+
def setup(&block)
|
26
|
+
self_config = new.tap do |config|
|
27
|
+
config.instance_eval(&block) if block
|
28
|
+
end
|
29
|
+
self_config.database
|
30
|
+
end
|
31
|
+
|
32
|
+
def reset
|
33
|
+
location = "./#{Inscriber::ConfigHelpers.config[:database_name]}"
|
34
|
+
File.unlink(location) if File.exists?(location)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_accessor :database
|
39
|
+
|
40
|
+
def initialize
|
41
|
+
@database ||= Inscriber::Database.new(Inscriber::ConfigHelpers.config)
|
42
|
+
end
|
43
|
+
|
44
|
+
def db_connection
|
45
|
+
database.connection
|
46
|
+
end
|
47
|
+
|
48
|
+
def create_table(name, &block)
|
49
|
+
opts = { name: name, database: database }
|
50
|
+
TableCreator.new(opts).setup(&block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class TableCreator
|
55
|
+
attr_accessor :name, :database
|
56
|
+
|
57
|
+
def initialize(opts)
|
58
|
+
@name = opts[:name]
|
59
|
+
@database = opts[:database]
|
60
|
+
end
|
61
|
+
|
62
|
+
def setup(&block)
|
63
|
+
db_connection.create_table(name) { instance_eval &block }
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def db_connection
|
69
|
+
database.connection
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
RSpec.configure do |config|
|
75
|
+
config.around(:each) do |example|
|
76
|
+
Inscriber::TestDatabase.reset
|
77
|
+
example.run
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'spec_helpers/config_helpers'
|
3
|
+
|
4
|
+
describe Inscriber::Uploader do
|
5
|
+
let!(:database) do
|
6
|
+
Inscriber::TestDatabase.setup do
|
7
|
+
create_table :test_translations do
|
8
|
+
primary_key :id
|
9
|
+
integer :test_id
|
10
|
+
string :body
|
11
|
+
string :locale
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
let!(:config) do
|
17
|
+
{
|
18
|
+
"fr" => {
|
19
|
+
"test_translations" => [
|
20
|
+
{
|
21
|
+
"body" => "Chaîne de test",
|
22
|
+
"test_id" => 1
|
23
|
+
}
|
24
|
+
]
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:db) do
|
30
|
+
database.connection.from(config["fr"].keys.first)
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '.upload' do
|
34
|
+
it 'should upload the data to the database' do
|
35
|
+
opts = { database: database, table: config["fr"].keys.first, records: config["fr"]["test_translations"], locale: "fr" }
|
36
|
+
Inscriber::Uploader.new(opts).upload
|
37
|
+
query = {
|
38
|
+
:locale => config.keys.first,
|
39
|
+
:body => config["fr"]["test_translations"].first["body"]
|
40
|
+
}
|
41
|
+
expect(db.where(query).all.length).to be 1
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inscriber
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ian Florentino
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A tool for downloading localized data from your database and creating
|
70
|
+
a locale file for translations
|
71
|
+
email:
|
72
|
+
- ian@pryntcases.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- lib/inscriber.rb
|
78
|
+
- lib/inscriber/config.rb
|
79
|
+
- lib/inscriber/connection_string.rb
|
80
|
+
- lib/inscriber/database.rb
|
81
|
+
- lib/inscriber/downloader.rb
|
82
|
+
- lib/inscriber/exporter.rb
|
83
|
+
- lib/inscriber/inserter.rb
|
84
|
+
- lib/inscriber/table_helper.rb
|
85
|
+
- lib/inscriber/uploader.rb
|
86
|
+
- lib/inscriber/version.rb
|
87
|
+
- spec/config_spec.rb
|
88
|
+
- spec/connection_string_spec.rb
|
89
|
+
- spec/database_spec.rb
|
90
|
+
- spec/downloader_spec.rb
|
91
|
+
- spec/exporter_spec.rb
|
92
|
+
- spec/inserter_spec.rb
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- spec/spec_helpers/config_helpers.rb
|
95
|
+
- spec/spec_helpers/test_database.rb
|
96
|
+
- spec/uploader_spec.rb
|
97
|
+
homepage: https://github.com/prynt/inscriber
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.6.10
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: A tool for downloading localized data from your database and creating a locale
|
121
|
+
file for translations
|
122
|
+
test_files: []
|