Module: ActiveFedora::RdfList

Defined in:
lib/active_fedora/rdf_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



3
4
5
# File 'lib/active_fedora/rdf_list.rb', line 3

def graph
  @graph
end

#subjectObject (readonly)

Returns the value of attribute subject.



3
4
5
# File 'lib/active_fedora/rdf_list.rb', line 3

def subject
  @subject
end

Instance Method Details

#[](idx) ⇒ Object



16
17
18
# File 'lib/active_fedora/rdf_list.rb', line 16

def [](idx)
  idx == 0 ?  head.value : tail[idx-1]
end

#[]=(idx, value) ⇒ Object



20
21
22
# File 'lib/active_fedora/rdf_list.rb', line 20

def []=(idx, value)
  idx == 0 ?  head.value=value : tail_or_create(idx-1).value=value
end

#firstObject



12
13
14
# File 'lib/active_fedora/rdf_list.rb', line 12

def first
  self[0] 
end

#headObject



57
58
59
# File 'lib/active_fedora/rdf_list.rb', line 57

def head
  @head ||= self.class.new(graph, subject)
end

#initialize(graph, subject) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/active_fedora/rdf_list.rb', line 4

def initialize(graph, subject)
  @graph = graph
  @subject = subject
  first = graph.query([subject, RDF.first, nil]).first
  graph.insert([subject, RDF.first, RDF.nil]) unless first
  graph.insert([subject, RDF.rest, RDF.nil])
end

#inspectObject



34
35
36
# File 'lib/active_fedora/rdf_list.rb', line 34

def inspect
  "[ #{value ? value.inspect : 'nil'}, #{tail.inspect}]"
end

#sizeObject



24
25
26
27
28
29
30
31
32
# File 'lib/active_fedora/rdf_list.rb', line 24

def size
  if tail
    tail.size + 1
  elsif graph.query([subject, RDF.first, RDF.nil]).first
    0
  else
    1
  end
end

#tailObject



61
62
63
64
65
# File 'lib/active_fedora/rdf_list.rb', line 61

def tail
  rest = graph.query([subject, RDF.rest, nil]).first
  return if rest.object == RDF.nil
  self.class.new(graph, rest.object)
end

#tail_or_create(idx) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_fedora/rdf_list.rb', line 67

def tail_or_create(idx)
  rest = graph.query([subject, RDF.rest, nil]).first
  obj = if rest.object != RDF.nil 
    self.class.new(graph, rest.object)
  else
    #add a tail
    graph.delete([subject, RDF.rest, RDF.nil])
    tail = RDF::Node.new
    graph.insert([subject, RDF.rest, tail])
    self.class.new(graph, tail)
  end

  idx == 0 ? obj : obj.tail_or_create(idx-1)
end

#valueObject



38
39
40
41
42
43
44
45
46
# File 'lib/active_fedora/rdf_list.rb', line 38

def value
  v = graph.query([subject, RDF.first, nil]).first
  return v.object if v.object.uri?
  if v.object.resource?
    type = graph.query([v.object, RDF.type, nil]).first
    return ActiveFedora::RdfNode.rdf_registry[type.object].new(graph, v.object)
  end
  v
end

#value=(obj) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/active_fedora/rdf_list.rb', line 48

def value=(obj)
  graph.delete([subject, RDF.first, RDF.nil])
  if obj.respond_to? :rdf_subject
    graph.insert([subject, RDF.first, obj.rdf_subject]) # an ActiveFedora::RdfObject
  else
    graph.insert([subject, RDF.first, obj])
  end
end