Class: Libis::Format::Converter::Chain
- Inherits:
-
Object
- Object
- Libis::Format::Converter::Chain
- Includes:
- DeepDive, Tools::Logger
- Defined in:
- lib/libis/format/converter/chain.rb
Instance Method Summary collapse
- #add_chain_node(node = {}) ⇒ Object
- #append(converter) ⇒ Array[Hash]
- #apply_operations ⇒ Object
- #closed? ⇒ Boolean
- #convert(src_file, target_file) ⇒ Object
-
#initialize(source_format, target_format, operations = {}) ⇒ Chain
constructor
A new instance of Chain.
- #size ⇒ Object (also: #length)
- #to_array ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #valid_chain_nodes(converter) ⇒ Object
Constructor Details
#initialize(source_format, target_format, operations = {}) ⇒ Chain
Returns a new instance of Chain.
17 18 19 20 21 22 |
# File 'lib/libis/format/converter/chain.rb', line 17 def initialize(source_format, target_format, operations = {}) @source_format = source_format.to_sym @target_format = target_format.to_sym @operations = operations || {} @converter_chain = [] end |
Instance Method Details
#add_chain_node(node = {}) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/libis/format/converter/chain.rb', line 128 def add_chain_node(node = {}) last_converter = @converter_chain.last source_format = last_converter ? last_converter[:output] : @source_format node[:input] ||= source_format return nil unless node[:input] == source_format return nil unless node[:output] && node[:converter].output_types(source_format).include?(node[:output]) return nil unless node[:converter].input_types.include? source_format return nil if node_exists?(node) @converter_chain << node # debug "Chain: #{self}" self end |
#append(converter) ⇒ Array[Hash]
26 27 28 29 30 31 32 |
# File 'lib/libis/format/converter/chain.rb', line 26 def append(converter) return [] unless converter valid_chain_nodes(converter).map do |node| ddup.add_chain_node(node) end.compact end |
#apply_operations ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/libis/format/converter/chain.rb', line 142 def apply_operations temp_chain = @converter_chain.reverse.ddup applied = true operations = @operations&.ddup || {} while (operation = operations.shift) method = operation.first.to_s.to_sym applied &&= temp_chain.each do |node| next unless node[:converter].instance_methods.include?(method) node[:operations] ||= [] node[:operations] << { method:, argument: operation.last } break :found end == :found end if applied && operations.empty? @converter_chain = temp_chain.reverse @operations.clear return true end false end |
#closed? ⇒ Boolean
34 35 36 37 38 |
# File 'lib/libis/format/converter/chain.rb', line 34 def closed? !@converter_chain.empty? && @converter_chain.first[:input].to_sym == @source_format && @converter_chain.last[:output].to_sym == @target_format end |
#convert(src_file, target_file) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/libis/format/converter/chain.rb', line 59 def convert(src_file, target_file) unless valid? error 'Converter chain is not valid' return nil end temp_files = [] xtra_files = [] result = { commands: [] } # noinspection RubyParenthesesAroundConditionInspection conversion_success = @converter_chain.each_with_index do |node, i| target_type = node[:output] converter_class = node[:converter] converter = converter_class.new node[:operations]&.each do |operation| converter.send operation[:method], operation[:argument] end target = target_file if i < size - 1 target += ".temp.#{TypeDatabase.type_extentions(target_type).first}" target += ".#{TypeDatabase.type_extentions(target_type).first}" while File.exist? target temp_files << target end FileUtils.mkdir_p File.dirname(target) r = converter.convert(src_file, target, target_type) src_file = r[:files].first xtra_files += r[:files][1..] break :failed unless src_file result[:commands] << r.merge(converter: converter_class.name) :success end result[:files] = [src_file] + xtra_files temp_files.each do |f| FileUtils.rm(f, force: true) end conversion_success == :failed ? nil : result end |
#size ⇒ Object Also known as: length
48 49 50 |
# File 'lib/libis/format/converter/chain.rb', line 48 def size @converter_chain.size end |
#to_array ⇒ Object
44 45 46 |
# File 'lib/libis/format/converter/chain.rb', line 44 def to_array @converter_chain end |
#to_s ⇒ Object
54 55 56 57 |
# File 'lib/libis/format/converter/chain.rb', line 54 def to_s result = @source_format.to_s result << @converter_chain.map { |node| node_to_s(node) }.join('->-') end |
#valid? ⇒ Boolean
40 41 42 |
# File 'lib/libis/format/converter/chain.rb', line 40 def valid? closed? && apply_operations end |
#valid_chain_nodes(converter) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/libis/format/converter/chain.rb', line 110 def valid_chain_nodes(converter) source_format = begin @converter_chain.last[:output] rescue StandardError @source_format end nodes = [] if converter.input_types.include? source_format converter.output_types(source_format).each do |format| node = { converter:, input: source_format, output: format } next if node_exists?(node) nodes << node end end nodes end |