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
-
#nanopub(input, input_format, options = {}) ⇒ #each
Return a stream of Nanopub::Nanopub objects for the input.
- #process_input(input) ⇒ Object
-
#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.
-
#translator(input_format, options = {}) ⇒ ::BEL::Translator
Return the BEL::Translator plugin given an identifier.
Instance Method Details
#nanopub(input, input_format, options = {}) ⇒ #each
Return a stream of Nanopub::Nanopub objects for the input.
22 23 24 25 26 27 28 29 |
# File 'lib/bel/translate.rb', line 22 def nanopub(input, input_format, = {}) prepared_input = process_input(input) in_translator = self.translator(input_format) or raise TranslateError.new(input_format) in_translator.read(prepared_input, ) 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.
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, = {}) prepared_input = process_input(input) in_translator = self.translator(input_format, ) or raise TranslateError.new(input_format) out_translator = self.translator(output_format, ) or raise TranslateError.new(output_format) nanopub = in_translator.read(prepared_input, ) out_translator.write(nanopub, writer, ) writer end |
#translator(input_format, options = {}) ⇒ ::BEL::Translator
Return the BEL::Translator plugin given an identifier.
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, = {}) return nil unless input_format id = input_format.to_sym plugins = BEL::Translator.plugins plugin = plugins[id] if plugin plugin.create_translator() 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() if match end end |