class Sapristi::ConfigurationLoader
Constants
- SEPARATOR
Public Class Methods
new()
click to toggle source
# File lib/sapristi/configuration_loader.rb, line 8 def initialize @definition_parser = DefinitionParser.new end
Public Instance Methods
create_empty_configuration(conf_file)
click to toggle source
# File lib/sapristi/configuration_loader.rb, line 18 def create_empty_configuration(conf_file) raise Error, "Trying to write empty configuration on existing file #{conf_file}" if File.exist? conf_file File.write(conf_file, Definition::HEADERS.join(SEPARATOR)) end
load(file_path)
click to toggle source
# File lib/sapristi/configuration_loader.rb, line 12 def load(file_path) csv_rows = load_csv(file_path) parse_rows(csv_rows, file_path) end
save(conf_file, definitions)
click to toggle source
# File lib/sapristi/configuration_loader.rb, line 24 def save(conf_file, definitions) raise Error, "Trying to write configuration on existing file #{conf_file}" if File.exist? conf_file serialized = definitions.map { |definition| serialize definition } write_to_csv conf_file, serialized end
Private Instance Methods
load_csv(csv_file)
click to toggle source
# File lib/sapristi/configuration_loader.rb, line 56 def load_csv(csv_file) table = CSV.read(csv_file, headers: true, col_sep: SEPARATOR) rescue Errno::ENOENT raise Error, "Configuration file not found: #{csv_file}" else raise Error, "Invalid configuration file: Empty file #{csv_file}" if table.eql? [] table.map(&:to_h) end
parse_rows(csv_rows, file)
click to toggle source
# File lib/sapristi/configuration_loader.rb, line 46 def parse_rows(csv_rows, file) csv_rows.each_with_index.map do |definition, line| @definition_parser.parse(definition) rescue Error => e raise Error, "Invalid configuration file: #{e.message}, line=#{line}, file=#{file}" rescue StandardError => e raise Error, "Unable to process configuration file: #{file}, line=#{line}, error=#{e.message}" end end
serialize(definition)
click to toggle source
# File lib/sapristi/configuration_loader.rb, line 40 def serialize(definition) Definition::HEADERS.map do |field| definition.raw_definition[field] end end
write_to_csv(conf_file, serialized)
click to toggle source
# File lib/sapristi/configuration_loader.rb, line 34 def write_to_csv(conf_file, serialized) CSV.open(conf_file, 'wb', write_headers: true, headers: Definition::HEADERS, col_sep: SEPARATOR) do |csv| serialized.each { |definition| csv << definition } end end