module Bismas::XML
Public Instance Methods
run(options, &block)
click to toggle source
# File lib/bismas/xml.rb 37 def run(options, &block) 38 block ||= method(:abort) 39 40 require_gem 'builder' 41 42 block['Schema file is required'] unless schema_file = options[:schema] 43 block["No such file: #{schema_file}"] unless File.readable?(schema_file) 44 45 schema = Schema.parse_file(schema_file) 46 47 execute = execute_options(options, &block) 48 mapping = mapping(options[:mapping], &block) 49 50 records_element, record_element = case options[:type].to_s 51 when 'solr' then %w[add doc] 52 else %w[records record] 53 end 54 55 records_attributes = { 56 name: schema.name, 57 description: schema.title, 58 mtime: File.mtime(options[:input]).xmlschema 59 } 60 61 reader_options = input_options(options, schema.category_length) 62 63 schema, encoding = mapping.apply(schema), options[:output_encoding] 64 65 execute[0][bind1 = binding] 66 67 File.open_file(options[:output], {}, 'wb') { |f| 68 xml = Builder::XmlMarkup.new(indent: 2, target: f) 69 xml.instance_eval("@encoding = #{encoding.inspect}") if encoding 70 xml.instruct! 71 72 xml.method_missing(records_element, records_attributes) { 73 Reader.parse_file(options[:input], reader_options) { |id, record| 74 xml.method_missing(record_element, id: id) { 75 execute[1][bind2 = binding] 76 record = mapping.apply(encode(record, encoding)) 77 78 execute[2][bind2] 79 record.sort_by { |key,| key }.each { |key, values| 80 field_attributes = { name: key } 81 desc = Array(schema[key]).join('/') 82 field_attributes[:description] = desc unless desc.empty? 83 84 Array(values).each { |value| xml.field(value, field_attributes) } 85 } 86 } 87 } 88 } 89 } 90 91 execute[3][bind1] 92 end