Class: Grom::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader) ⇒ Builder

Returns a new instance of Builder.



5
6
7
8
9
# File 'lib/grom/builder.rb', line 5

def initialize(reader)
  @reader = reader

  build_objects
end

Instance Attribute Details

#objectsObject (readonly)

Returns the value of attribute objects.



3
4
5
# File 'lib/grom/builder.rb', line 3

def objects
  @objects
end

Instance Method Details

#build_objectsObject



11
12
13
14
15
16
# File 'lib/grom/builder.rb', line 11

def build_objects
  build_objects_by_subject
  link_objects

  @objects
end

#build_objects_by_subjectObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/grom/builder.rb', line 22

def build_objects_by_subject
  initialize_objects_hashes
  @reader.statements_by_subject.each do |subject, statements|
    object = Grom::Node.new(statements)
    @objects_by_subject[subject] = object
    @objects << object
  end

  self
end

#initialize_objects_hashesObject



18
19
20
# File 'lib/grom/builder.rb', line 18

def initialize_objects_hashes
  @objects, @objects_by_subject = [], {}
end


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/grom/builder.rb', line 33

def link_objects
  @reader.edges_by_subject.each do |subject, predicates|
    predicates.each do |predicate, object_uris|
      current_node = @objects_by_subject[subject]

      object_uris.each do |object_uri|

        predicate_name_symbol = "@#{predicate}".to_sym
        object_array = current_node.instance_variable_get(predicate_name_symbol)
        object_array = [] if object_array.is_a?(String)
        object_array << @objects_by_subject[object_uri]
        
        current_node.instance_variable_set(predicate_name_symbol, object_array)

        # TODO: Not sure about the above conditional
      end
    end
  end

  self
end