Class: Grom::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/grom/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Reader

Returns a new instance of Reader.



7
8
9
10
11
12
13
# File 'lib/grom/reader.rb', line 7

def initialize(data)
  @data = data

  read_data

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/grom/reader.rb', line 5

def data
  @data
end

#edges_by_subjectObject (readonly)

Returns the value of attribute edges_by_subject.



5
6
7
# File 'lib/grom/reader.rb', line 5

def edges_by_subject
  @edges_by_subject
end

#objectsObject (readonly)

Returns the value of attribute objects.



5
6
7
# File 'lib/grom/reader.rb', line 5

def objects
  @objects
end

#statements_by_subjectObject (readonly)

Returns the value of attribute statements_by_subject.



5
6
7
# File 'lib/grom/reader.rb', line 5

def statements_by_subject
  @statements_by_subject
end

#subjects_by_typeObject (readonly)

Returns the value of attribute subjects_by_type.



5
6
7
# File 'lib/grom/reader.rb', line 5

def subjects_by_type
  @subjects_by_type
end

Instance Method Details

#read_dataObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/grom/reader.rb', line 15

def read_data
  # Reset all our hashes just in case
  @statements_by_subject  = {}

  # TODO: Find a better name for this hash!
  @edges_by_subject = {}

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

      # TODO: Use Ruby key value syntax in below method.
      Grom::Helper.lazy_array_insert(@statements_by_subject, subject, statement)

      predicate = statement.predicate.to_s

      if (statement.object =~ URI.regexp) == 0 && 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