Class: BEL::Translator::Plugins::BelScript::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/bel/translator/plugins/bel_script/writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Writer

Create a BEL::Translator::Plugins::BelScript::Writer object that serializes Nanopub::Nanopub to BEL Script.

Parameters:

  • data (Enumerator<BEL::Nanopub::Nanopub>)

    nanopubs iterated using each

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):



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
58
# File 'lib/bel/translator/plugins/bel_script/writer.rb', line 27

def initialize(data, options = {})
  @data                     = data
  @streaming                = options.fetch(:streaming, false)
  @write_header             = options.fetch(:write_header, true)
  @annotation_reference_map = options.fetch(:annotation_reference_map, nil)
  @namespace_reference_map  = options.fetch(:namespace_reference_map, nil)

  # augment self with BEL serialization stategy.
  serialization = options[:serialization]
  serialization_module =
    case serialization
    when Module
      serialization
    when String, Symbol
      serialization_refs = {
        :discrete => BelDiscreteSerialization,
        :topdown  => BelTopDownSerialization,
        :citation => BelCitationSerialization,
      }
      serialization_module = serialization_refs[serialization.to_sym]
      unless serialization_module
          raise %Q{No BEL serialization strategy for "#{serialization}"}
      end
      serialization_module
    else
      # Default to citation serialization.
      BelCitationSerialization
    end

  self_eigenclass = (class << self; self; end)
  self_eigenclass.send(:include, serialization_module)
end

Instance Method Details

#eachObject



60
61
62
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
# File 'lib/bel/translator/plugins/bel_script/writer.rb', line 60

def each
  if block_given?
    combiner =
      if @streaming
        BEL::Nanopub::StreamingNanopubCombiner.new(@data)
      elsif @annotation_reference_map && @namespace_reference_map
        BEL::Nanopub::MapReferencesCombiner.new(
          @data,
          BEL::Nanopub::HashMapReferences.new(
            @annotation_reference_map,
            @namespace_reference_map
          )
        )
      else
        BEL::Nanopub::BufferingNanopubCombiner.new(@data)
      end

    header_flag = true
    combiner.each { |nanopub|

      # serialize nanopub
      bel = to_bel(nanopub)

      if @write_header && header_flag
        yield document_header(nanopub..document_header)
        yield namespaces(combiner.namespace_references)
        yield annotations(combiner.annotation_references)

        yield <<-COMMENT.gsub(/^\s+/, '')
          ###############################################
          # Statements Section
        COMMENT
        header_flag = false
      end

      yield bel
    }

    yield epilogue
  else
    to_enum(:each)
  end
end