class Libis::Format::Converter::Repository

Attributes

converters[R]
converters_glob[RW]

Public Class Methods

get_converter_chain(src_type, tgt_type, operations = {}) click to toggle source
# File lib/libis/format/converter/repository.rb, line 45
def Repository.get_converter_chain(src_type, tgt_type, operations = {})
  instance.get_converter_chain src_type, tgt_type, operations
end
get_converters() click to toggle source
# File lib/libis/format/converter/repository.rb, line 31
def Repository.get_converters
  instance.get_converters
end
new() click to toggle source
# File lib/libis/format/converter/repository.rb, line 22
def initialize
  @converters = Set.new
  @converters_glob = File.join(File.dirname(__FILE__), '*_converter.rb')
end
register(converter_class) click to toggle source
# File lib/libis/format/converter/repository.rb, line 27
def Repository.register(converter_class)
  instance.converters.add? converter_class
end

Public Instance Methods

get_converter_chain(src_type, tgt_type, operations = {}) click to toggle source
# File lib/libis/format/converter/repository.rb, line 49
def get_converter_chain(src_type, tgt_type, operations = {})
  msg = "conversion from #{src_type.to_s} to #{tgt_type.to_s}"
  chain_list = find_chains src_type, tgt_type, operations
  # if chain_list.length > 1
  #   warn "Found more than one conversion chain for #{msg}. Picking the first one."
  # end
  if chain_list.empty?
    error "No conversion chain found for #{msg}"
    return nil
  end
  # chain_list.each do |chain|
  #   debug "Matched chain: #{chain}"
  # end
  chain_list[0]
end
get_converters() click to toggle source
# File lib/libis/format/converter/repository.rb, line 35
def get_converters
  if converters.empty?
    Dir.glob(converters_glob).each do |filename|
      # noinspection RubyResolve
      require File.expand_path(filename)
    end
  end
  converters
end

Private Instance Methods

build_chains(chain) click to toggle source
# File lib/libis/format/converter/repository.rb, line 72
def build_chains(chain)

  found = []
  chains = [chain]

  # Avoid chains that are too long
  Libis::Format::Config[:converter_chain_max_level].times do
    new_chains = []
    get_converters.each do |converter|
      new_chains += chains.map { |c| c.append(converter) }.flatten
    end

    found = new_chains.select { |c| c.valid?}
    return found unless found.empty?

    chains = new_chains
  end

  found

end
find_chains(src_type, tgt_type, operations) click to toggle source
# File lib/libis/format/converter/repository.rb, line 67
def find_chains(src_type, tgt_type, operations)
  chain = Libis::Format::Converter::Chain.new(src_type, tgt_type, operations)
  build_chains(chain)
end