module Bismas::XML
Public Instance Methods
run(options, &block)
click to toggle source
# File lib/bismas/xml.rb, line 37 def run(options, &block) block ||= method(:abort) require_gem 'builder' block['Schema file is required'] unless schema_file = options[:schema] block["No such file: #{schema_file}"] unless File.readable?(schema_file) schema = Schema.parse_file(schema_file) execute = execute(options.values_at(*%[execute execute_mapped]), &block) mapping = mapping(options[:mapping], &block) records_attributes = { name: schema.name, description: schema.title, mtime: File.mtime(options[:input]).xmlschema } reader_options = { encoding: options[:encoding], key: options[:key], strict: options[:strict], silent: options[:silent], category_length: schema.category_length } schema = mapping.apply(schema) File.open_file(options[:output], {}, 'wb') { |f| xml = Builder::XmlMarkup.new(indent: 2, target: f) xml.instruct! xml.records(records_attributes) { Reader.parse_file(options[:input], reader_options) { |id, record| xml.record(id: id) { execute[0][bind = binding] record = mapping.apply(record) execute[1][bind] record.sort_by { |key,| key }.each { |key, values| field_attributes = { name: key, description: Array(schema[key]).join('/') } Array(values).each { |value| xml.field(value, field_attributes) } } } } } } end