Class: BEL::BELRDF::Reader::UnbufferedNanopubYielder

Inherits:
Object
  • Object
show all
Includes:
NanopubYielder
Defined in:
lib/bel/translator/plugins/rdf2/reader.rb

Constant Summary

Constants included from Quoting

Quoting::KeywordMatcher, Quoting::Keywords, Quoting::LenientQuotedMatcher, Quoting::NonWordMatcher, Quoting::QuoteNotEscapedMatcher, Quoting::StrictQuotedMatcher

Instance Method Summary collapse

Methods included from NanopubYielder

#describe, #make_nanopub, #nanopub_yielder, #resources_of_type

Methods included from Quoting

#always_quote, #ensure_quotes, #identifier_value?, #quote, #quote_if_needed, #quoted?, #quotes_required?, #remove_quotes, #string_value?, #unquote, #unquoted?

Methods included from Nanopub

union_annotation_references, union_by_keyword, union_namespace_references

Constructor Details

#initialize(data, format) ⇒ UnbufferedNanopubYielder

Returns a new instance of UnbufferedNanopubYielder.



123
124
125
126
# File 'lib/bel/translator/plugins/rdf2/reader.rb', line 123

def initialize(data, format)
  @data   = data
  @format = format
end

Instance Method Details

#eachObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/bel/translator/plugins/rdf2/reader.rb', line 128

def each
  if block_given?
    graph   = RDF::Graph.new
    nanopub = nil
    RDF::Reader.for(@format).new(@data) do |reader|
      reader.each_statement do |statement|
        case
        when statement.object == BELV.Nanopub &&
             statement.predicate == RDF.type
          nanopub = statement.subject
        when nanopub &&
             statement.predicate != BELV.hasNanopub &&
             statement.subject != nanopub

          # yield current graph as nanopub
          yield make_nanopub(
            describe(nanopub, graph),
            graph
          )

          # reset parse state
          graph.clear
          nanopub = nil

          # insert this RDF statement
          graph << statement
        else
          graph << statement
        end
      end
    end

    # yield last graph as nanopub
    yield make_nanopub(
      describe(nanopub, graph),
      graph
    )
  else
    to_enum(:each)
  end
end