Sha256: 87f11580a18b5ac987a6adbd3ab4cb05aee5eb95f71cbb07be1b9f7f7955c4f9

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

module Grom
  # Reads n-triple data and passes it to a Grom::Builder instance to create objects
  #
  # @since 0.1.0
  # @attr_reader [String] data n-triple data.
  # @attr_reader [Hash] statements_by_subject statements grouped by subject.
  # @attr_reader [Hash] edges_by_subject subjects connected to objects which are uris via their predicates.
  # @attr_reader [Array] objects Grom::Node objects generated from n-triple data.
  class Reader
    attr_reader :data, :statements_by_subject, :edges_by_subject, :objects

    # @param [String] data n-triple data.
    def initialize(data)
      @data = data

      read_data

      @objects = Grom::Builder.new(self).objects
    end

    # Reads the n-triple data and separates the statements by subject.
    #
    # @return [Grom::Reader] an instance of self.
    def read_data
      @statements_by_subject = {}

      @edges_by_subject = {}

      RDF::NTriples::Reader.new(@data) do |reader|
        reader.each_statement do |statement|
          subject = statement.subject.to_s

          Grom::Helper.lazy_array_insert(@statements_by_subject, subject, statement)

          predicate = statement.predicate.to_s

          if statement.object.uri? && predicate != RDF.type.to_s
            predicate = Grom::Helper.get_id(predicate)
            @edges_by_subject[subject] ||= {}
            @edges_by_subject[subject][predicate] ||= []
            @edges_by_subject[subject][predicate] << statement.object.to_s
          end
        end
      end

      self
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
grom-0.5.1 lib/grom/reader.rb
grom-0.5.0 lib/grom/reader.rb
grom-0.4.0 lib/grom/reader.rb
grom-0.3.10.pre lib/grom/reader.rb
grom-0.3.9 lib/grom/reader.rb
grom-0.3.8 lib/grom/reader.rb
grom-0.3.7 lib/grom/reader.rb
grom-0.3.6 lib/grom/reader.rb