Class: BEL::Translator::Plugins::Bnj::BnjTranslator

Inherits:
Object
  • Object
show all
Includes:
BEL::Translator
Defined in:
lib/bel/translator/plugins/bnj/translator.rb

Instance Method Summary collapse

Instance Method Details

#read(data, options = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/bel/translator/plugins/bnj/translator.rb', line 12

def read(data, options = {})
  ::BEL::JSON.read(data, options).lazy.select { |obj|
    obj.include?(Bnj::NANOPUB_ROOT)
  }.map { |json_obj|
    unwrap(json_obj)
  }
end

#write(objects, writer = StringIO.new, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bel/translator/plugins/bnj/translator.rb', line 20

def write(objects, writer = StringIO.new, options = {})
  json_strings = Enumerator.new { |yielder|
    yielder << '['
    begin
      nanopub_enum = objects.each

      # write first nanopub object
      nanopub = nanopub_enum.next
      yielder << ::BEL::JSON.write(wrap(nanopub), nil)

      # each successive nanopub starts with a comma
      while true
        nanopub = nanopub_enum.next
        yielder << ','
        yielder << ::BEL::JSON.write(wrap(nanopub), nil)
      end
    rescue StopIteration
      # end of nanopub hashes
    end
    yielder << ']'
  }

  if block_given?
    json_strings.each do |string|
      yield string
    end
  else
    if writer
      json_strings.each do |string|
        writer << string
        writer.flush
      end
      writer
    else
      json_strings
    end
  end
end