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.
18 19 20 21 22 23 |
# File 'lib/libis/format/converter/chain.rb', line 18 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
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/libis/format/converter/chain.rb', line 120 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]
27 28 29 30 31 32 |
# File 'lib/libis/format/converter/chain.rb', line 27 def append(converter) return [] unless converter valid_chain_nodes(converter).map do |node| self.ddup.add_chain_node(node) end.compact end |
#apply_operations ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/libis/format/converter/chain.rb', line 133 def apply_operations temp_chain = @converter_chain.reverse.ddup applied = true operations = @operations && @operations.ddup || {} while (operation = operations.shift) method = operation.first.to_s.to_sym applied &&= :found == temp_chain.each do |node| next unless node[:converter].instance_methods.include?(method) node[:operations] ||= [] node[:operations] << {method: method, argument: operation.last} break :found end 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
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 |
# File 'lib/libis/format/converter/chain.rb', line 63 def convert(src_file, target_file) unless valid? error 'Converter chain is not valid' return nil end temp_files = [] # noinspection RubyParenthesesAroundConditionInspection result = @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 if node[:operations] target = target_file if i < size - 1 target += ".temp.#{Libis::Format::Library.get_field(target_type, :extensions).first}" target += ".#{Libis::Format::Library.get_field(target_type, :extensions).first}" while File.exist? target temp_files << target end FileUtils.mkdir_p File.dirname(target) src_file = converter.convert(src_file, target, target_type) break :failed unless src_file end temp_files.each do |f| FileUtils.rm(f, force: true) end result == :failed ? nil : target_file 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 58 59 60 61 |
# File 'lib/libis/format/converter/chain.rb', line 54 def to_s "#{@source_format}->-#{@converter_chain.map do |node| "#{node[:converter].name.gsub(/^.*::/, '')}#{node[:operations].empty? ? '' : "(#{node[:operations].each do |operation| "#{operation[:method]}:#{operation[:argument]}" end.join(',')})"}->-#{node[:output]}" end.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
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/libis/format/converter/chain.rb', line 107 def valid_chain_nodes(converter) source_format = @converter_chain.last[:output] rescue @source_format nodes = [] if converter.input_types.include? source_format converter.output_types(source_format).each do |format| node = {converter: converter, input: source_format, output: format} next if node_exists?(node) nodes << node end end nodes end |