lib/databasion/yaml_builder.rb in databasion-0.1.1 vs lib/databasion/yaml_builder.rb in databasion-0.2.0

- old
+ new

@@ -4,13 +4,14 @@ class YamlBuilderError < StandardError; end class YamlBuilder - def self.run(data_hash, output_path=nil) - raise YamalizeError, 'Databasion::YamlBuilder requires an output path.' if output_path.nil? - @@output_path = output_path + def self.run(data_hash, config=nil, opts=nil) + raise YamalizeError, 'Databasion::YamlBuilder requires an output path.' if config['output']['yaml_path'].nil? + @@config = config + @@environment = opts[:env] Databasion::LOGGER.info "Yamlbating %s..." % data_hash['name'] yaml_output = process(data_hash) write(data_hash['name'], yaml_output) @@ -29,18 +30,23 @@ type_data = data_hash['types'][index].split(',') yaml_output += " - field: %s\n type: %s\n" % [field, type_data[0]] yaml_output += " size: %s\n" % type_data[1] if type_data[1] yaml_output += " default: %s\n" % type_data[2] if type_data[2] end - indexes = '' - data_hash['indexes'].each_with_index do |index, i| - indexes += data_hash['fields'][i] + "," + yaml_output += " auto: %s\n" % data_hash['auto'] + yaml_output += " indexes:\n" + data_hash['indexes'].each do |key, indexes| + index_list = [] + indexes.each do |index| + index_list.push data_hash['fields'][index] unless data_hash['ignore_cols'].include?(index) + end + yaml_output += " - index: [%s]\n" % index_list.join(", ") end - yaml_output += " indexes: [%s]\n" % indexes.chop + yaml_output += " primaries: [%s]\n" % data_hash['primaries'].join(", ") unless data_hash['primaries'].empty? yaml_output += " connection:\n" data_hash['connection'].each do |key, value| - yaml_output += " %s: %s\n" % [key, value] unless ['spreadsheet', 'options'].include?(key) + yaml_output += " %s: %s\n" % [key, value] unless ['spreadsheet'].include?(key) end yaml_output += "\n" end def self.database_rows(data_hash) @@ -59,18 +65,19 @@ end yaml_output += "\n" end def self.write(file_name, yaml_output) - check_output_path - f = File.new("%s/%s.yml" % [@@output_path, file_name], 'w') + path = @@environment + "/" + @@config['output']['yaml_path'] + check_output_path(path) + f = File.new("%s/%s.yml" % [path, file_name], 'w') f.write(yaml_output) f.close end - def self.check_output_path - unless File.exist?(@@output_path) - FileUtils.mkdir_p(@@output_path) + def self.check_output_path(path) + unless File.exist?(path) + FileUtils.mkdir_p(path) end end end \ No newline at end of file