Module: BEL::Translate::ClassMethods

Included in:
BEL
Defined in:
lib/bel/translate.rb

Overview

Defines the translate API that is provided under the BEL module as a mixin.

Instance Method Summary collapse

Instance Method Details

#nanopub(input, input_format, options = {}) ⇒ #each

Return a stream of Nanopub::Nanopub objects for the input.

Parameters:

  • input (IO)

    the IO to read from

  • input_format (Symbol)

    the symbol that can be used to identify the translator plugin that can read the input

  • options (Hash{Symbol => Object}) (defaults to: {})

Returns:

  • (#each)

    an object that responds to each and provides Nanopub::Nanopub objects



22
23
24
25
26
27
28
29
# File 'lib/bel/translate.rb', line 22

def nanopub(input, input_format, options = {})
  prepared_input = process_input(input)

  in_translator  = self.translator(input_format) or
    raise TranslateError.new(input_format)

  in_translator.read(prepared_input, options)
end

#process_input(input) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/bel/translate.rb', line 86

def process_input(input)
  if input.respond_to? :read
    input
  elsif File.exist?(input)
    File.open(input, :ext_enc => Encoding::UTF_8)
  elsif input.respond_to? :to_s
    input.to_s
  end
end

#translate(input, input_format, output_format, writer = StringIO.new, options = {}) ⇒ IO

Translate from one file format to another using Nanopub::Nanopub as a shared model. The translation is written to the IO writer directly.

Parameters:

  • input (IO)

    the IO to read from

  • input_format (#to_sym)

    the identifier for the translator plugin that will read the input

  • output_format (#to_sym)

    the identifier for the translator plugin that will write to writer

  • options (Hash{Symbol => Object}) (defaults to: {})

Returns:

  • (IO)

    the IO writer that the translation was written to



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bel/translate.rb', line 43

def translate(input, input_format, output_format, writer = StringIO.new, options = {})
  prepared_input = process_input(input)

  in_translator  = self.translator(input_format, options) or
    raise TranslateError.new(input_format)

  out_translator = self.translator(output_format, options) or
    raise TranslateError.new(output_format)

  nanopub = in_translator.read(prepared_input, options)
  out_translator.write(nanopub, writer, options)
  writer
end

#translator(input_format, options = {}) ⇒ ::BEL::Translator

Return the BEL::Translator plugin given an identifier.

Parameters:

  • input_format (#to_sym)

    the identifier for the translator plugin

  • options (Hash{Symbol => Object}) (defaults to: {})

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bel/translate.rb', line 65

def translator(input_format, options = {})
  return nil unless input_format

  id      = input_format.to_sym
  plugins = BEL::Translator.plugins

  plugin = plugins[id]
  if plugin
    plugin.create_translator(options)
  else
    match = BEL::Translator.plugins.values.find { |t|
      match  = false
      match |= (id == t.name.to_sym)
      match |= (t.media_types.include?(id))
      match |= (t.file_extensions.include?(id))
      match
    }
    match.create_translator(options) if match
  end
end