-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Database abstraction and helpers for various data files.
- Loading branch information
Showing
22 changed files
with
670 additions
and
95 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 |
---|---|---|
|
@@ -5,3 +5,5 @@ Gemfile.lock | |
/spec/regression/**/*.db | ||
/spec/unit/writer/result.xlsx | ||
.world | ||
.ruby-version | ||
.DS_Store |
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
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,35 @@ | ||
module Bmg | ||
class Database | ||
|
||
def self.data_folder(*args) | ||
require_relative 'database/data_folder' | ||
DataFolder.new(*args) | ||
end | ||
|
||
def self.sequel(*args) | ||
require 'bmg/sequel' | ||
require_relative 'database/sequel' | ||
Sequel.new(*args) | ||
end | ||
|
||
def self.xlsx(*args) | ||
require 'bmg/xlsx' | ||
require_relative 'database/xlsx' | ||
Xlsx.new(*args) | ||
end | ||
|
||
def to_xlsx(*args) | ||
require 'bmg/xlsx' | ||
Writer::Xlsx.to_xlsx(self, *args) | ||
end | ||
|
||
def to_data_folder(*args) | ||
DataFolder.dump(self, *args) | ||
end | ||
|
||
def each_relation_pair | ||
raise NotImplementedError | ||
end | ||
|
||
end # class Database | ||
end # module Bmg |
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,67 @@ | ||
module Bmg | ||
class Database | ||
class DataFolder < Database | ||
|
||
DEFAULT_OPTIONS = { | ||
data_extensions: ['json', 'csv', 'yml', 'yaml'] | ||
} | ||
|
||
def initialize(folder, options = {}) | ||
@folder = Path(folder) | ||
@options = DEFAULT_OPTIONS.merge(options) | ||
end | ||
|
||
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 | ||
|
||
def self.dump(database, path, ext = :json) | ||
path = Path(path) | ||
path.mkdir_p | ||
database.each_relation_pair do |name, rel| | ||
(path/"#{name}.#{ext}").write(rel.public_send(:"to_#{ext}")) | ||
end | ||
path | ||
end | ||
|
||
private | ||
|
||
def read_file(file) | ||
case file.ext.to_s | ||
when '.json' | ||
Bmg.json(file) | ||
when '.csv' | ||
Bmg.csv(file) | ||
when '.yaml', '.yml' | ||
Bmg.yaml(file) | ||
else | ||
raise NotSupportedError, "Unable to use #{file} as a relation" | ||
end | ||
end | ||
|
||
def find_file(name) | ||
exts = @options[:data_extensions] | ||
exts.each do |ext| | ||
target = @folder/"#{name}.#{ext}" | ||
return target if target.file? | ||
end | ||
raise NotSuchRelationError, "#{@folder}/#{name}.#{exts.join(',')}" | ||
end | ||
|
||
end # class DataFolder | ||
end # class Database | ||
end # module Bmg |
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,35 @@ | ||
module Bmg | ||
class Database | ||
class Sequel < Database | ||
|
||
DEFAULT_OPTIONS = { | ||
} | ||
|
||
def initialize(sequel_db, options = {}) | ||
@sequel_db = sequel_db | ||
end | ||
|
||
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] | ||
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 | ||
end # class Database | ||
end # module Bmg |
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 @@ | ||
module Bmg | ||
class Database | ||
class Xlsx < Database | ||
|
||
DEFAULT_OPTIONS = { | ||
} | ||
|
||
def initialize(path, options = {}) | ||
path = Path(path) if path.is_a?(String) | ||
@path = path | ||
@options = options.merge(DEFAULT_OPTIONS) | ||
end | ||
|
||
def method_missing(name, *args, &bl) | ||
return super(name, *args, &bl) unless args.empty? && bl.nil? | ||
rel = rel_for(name) | ||
raise NotSuchRelationError(name.to_s) unless rel | ||
rel | ||
end | ||
|
||
def each_relation_pair | ||
return to_enum(:each_relation_pair) unless block_given? | ||
|
||
spreadsheet.sheets.each do |sheet_name| | ||
yield(sheet_name.to_sym, rel_for(sheet_name)) | ||
end | ||
end | ||
|
||
protected | ||
|
||
def spreadsheet | ||
@spreadsheet ||= Roo::Spreadsheet.open(@path, @options) | ||
end | ||
|
||
def rel_for(sheet_name) | ||
Bmg.excel(@path, { sheet: sheet_name.to_s }) | ||
end | ||
|
||
end # class Sequel | ||
end # class Database | ||
end # module Bmg |
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
Oops, something went wrong.