Skip to content

Commit

Permalink
WIP Database#to_xlsx
Browse files Browse the repository at this point in the history
  • Loading branch information
blambeau committed Jun 21, 2024
1 parent 1143720 commit c8b15ee
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 159 deletions.
9 changes: 9 additions & 0 deletions lib/bmg/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ def self.sequel(*args)
Sequel.new(*args)
end

def to_xlsx(*args)
require 'bmg/xlsx'
Writer::Xlsx.to_xlsx(self, *args)
end

def each_relation_pair
raise NotImplementedError
end

end # class Database
end # module Bmg
20 changes: 18 additions & 2 deletions lib/bmg/database/data_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ def initialize(folder, options = {})
def method_missing(name, *args, &bl)
return super(name, *args, &bl) unless args.empty? && bl.nil?
raise NotSuchRelationError(name.to_s) unless file = find_file(name)
read_file(file)
end

def each_relation_pair
return to_enum(:each_relation_pair) unless block_given?

@folder.glob('*') do |path|
next unless path.file?
next unless @options[:data_extensions].find {|ext|
path.ext == ".#{ext}" || path.ext == ext
}
yield(path.basename.rm_ext.to_sym, read_file(path))
end
end

private

def read_file(file)
case file.ext.to_s
when '.json'
Bmg.json(file)
Expand All @@ -26,8 +44,6 @@ def method_missing(name, *args, &bl)
end
end

private

def find_file(name)
exts = @options[:data_extensions]
exts.each do |ext|
Expand Down
16 changes: 15 additions & 1 deletion lib/bmg/database/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ def method_missing(name, *args, &bl)
return super(name, *args, &bl) unless args.empty? && bl.nil?
raise NotSuchRelationError(name.to_s) unless @sequel_db.table_exists?(name)
table = @sequel_db[name]
Bmg.sequel(table, @sequel_db)
rel_for(table)
end

def each_relation_pair
return to_enum(:each_relation_pair) unless block_given?

@sequel_db.tables.each do |table|
yield(table, rel_for(table))
end
end

protected

def rel_for(table_name)
Bmg.sequel(table_name, @sequel_db)
end

end # class Sequel
Expand Down
16 changes: 15 additions & 1 deletion lib/bmg/writer/xlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,36 @@ class Xlsx
}

def initialize(xlsx_options, output_preferences = nil)
require 'write_xlsx'
@xlsx_options = DEFAULT_OPTIONS.merge(xlsx_options)
@output_preferences = OutputPreferences.dress(output_preferences)
end
attr_reader :xlsx_options, :output_preferences

def call(relation, path)
require 'write_xlsx'
dup._call(relation, path)
end

def self.to_xlsx(database, path)
require 'write_xlsx'
workbook = WriteXLSX.new(path)
database.each_relation_pair do |name, rel|
worksheet = workbook.add_worksheet(name)
rel.to_xlsx({
workbook: workbook,
worksheet: worksheet,
})
end
workbook.close
end

protected
attr_reader :workbook, :worksheet

def _call(relation, path)
@workbook = xlsx_options[:workbook] || WriteXLSX.new(path)
@worksheet = xlsx_options[:worksheet] || workbook.add_worksheet
@worksheet = workbook.add_worksheet(@worksheet) if @worksheet.is_a?(String)

headers = infer_headers(relation.type)
before = nil
Expand Down
16 changes: 14 additions & 2 deletions spec/integration/database/test_data_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Database
describe DataFolder do

subject {
Database.data_folder(fixtures/'suppliers-and-parts')
Database.data_folder(Path.dir.parent.parent/'suppliers-and-parts')
}

it 'is served by Database.data_folder' do
Expand All @@ -27,9 +27,21 @@ class Database
expect(subject.countries.count).to eql(3)
end

it 'supports eaching relations' do
seen = subject.each_relation_pair.map{|name, rel|
expect(rel).to be_a(Relation)
name
}.sort_by(&:to_s)
expect(seen).to eql([:cities, :countries, :parts, :suppliers, :supplies])
end

it 'supports to_xlsx' do
subject.to_xlsx(Path.tempfile)
end

context 'when using a String path' do
subject {
Database.data_folder((fixtures/'suppliers-and-parts').to_s)
Database.data_folder((Path.dir.parent.parent/'suppliers-and-parts').to_s)
}

it 'is served by Database.data_folder' do
Expand Down
12 changes: 12 additions & 0 deletions spec/integration/database/test_sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ class Database
expect(subject.suppliers.count).to eql(5)
end

it 'supports eaching_relations' do
seen = subject.each_relation_pair.map{|name, rel|
expect(rel).to be_a(Relation)
name
}.sort_by(&:to_s)
expect(seen).to eql([:cities, :parts, :schema_migrations, :suppliers, :supplies])
end

it 'supports to_xlsx' do
subject.to_xlsx(Path.tempfile)
end

end
end # class Database
end # module Bmg
4 changes: 0 additions & 4 deletions spec/integration/fixtures/suppliers-and-parts/cities.csv

This file was deleted.

7 changes: 0 additions & 7 deletions spec/integration/fixtures/suppliers-and-parts/countries.yml

This file was deleted.

44 changes: 0 additions & 44 deletions spec/integration/fixtures/suppliers-and-parts/parts.json

This file was deleted.

32 changes: 0 additions & 32 deletions spec/integration/fixtures/suppliers-and-parts/suppliers.json

This file was deleted.

62 changes: 0 additions & 62 deletions spec/integration/fixtures/suppliers-and-parts/supplies.json

This file was deleted.

4 changes: 0 additions & 4 deletions spec/integration/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ def sequel_db
SEQUEL_DB
end

def fixtures
Path.dir/'fixtures'
end

class Context

def initialize(sequel_db)
Expand Down

0 comments on commit c8b15ee

Please sign in to comment.