Module: ActiveFedora::RdfList

Extended by:
ActiveSupport::Concern
Includes:
RdfNode
Defined in:
lib/active_fedora/rdf_list.rb

Constant Summary collapse

UNASSIGNABLE_KEYS =
%w(_destroy )

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RdfNode

#==, #append, #attributes=, #config_for_term_or_uri, #delete_predicate, #destroy, #find_predicate, #get_values, #mark_for_destruction, #marked_for_destruction?, #new_record=, #new_record?, #query, rdf_registry, #reset_child_cache!, #reset_rdf_subject!, #set_value, #target_class

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



6
7
8
# File 'lib/active_fedora/rdf_list.rb', line 6

def graph
  @graph
end

#subjectObject (readonly)

Returns the value of attribute subject.



6
7
8
# File 'lib/active_fedora/rdf_list.rb', line 6

def subject
  @subject
end

Instance Method Details

#[](idx) ⇒ Object



59
60
61
# File 'lib/active_fedora/rdf_list.rb', line 59

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

#[]=(idx, value) ⇒ Object



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

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

#assign_nested_attributes_for_collection_association(association_name, attributes_collection) ⇒ Object

Override assign_nested_attributes



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_fedora/rdf_list.rb', line 25

def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
  options = self.nested_attributes_options[association_name]

  # TODO
  #check_record_limit!(options[:limit], attributes_collection)

  if attributes_collection.is_a?(Hash)
    attributes_collection = attributes_collection.values
  end

  association = self.send(association_name)
  
  original_length_of_list = self.size
  attributes_collection.each_with_index do |attributes, index|
    attributes = attributes.with_indifferent_access
    minted_node = association.mint_node(attributes.except(*UNASSIGNABLE_KEYS))
    self[original_length_of_list+index] = minted_node
  end
end

#clear(first_element = true) ⇒ Object



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

def clear(first_element=true)
  # Remove the pointed at element
  v = graph.query([subject, RDF.first, nil]).first
  # TODO - Recursive delete
  graph.delete([v.object, nil, nil])
  
  # Remove the tail
  tail.clear(false) if tail
  # clear the cache
  @tail = nil
  graph.delete([subject, nil, nil])
  if first_element
    # Re-add first/rest predicates if its the first node
    graph.insert([subject, RDF.first, RDF.nil])
    graph.insert([subject, RDF.rest, RDF.nil])
  end
end

#each {|head.value| ... } ⇒ Object

Yields:



85
86
87
88
# File 'lib/active_fedora/rdf_list.rb', line 85

def each &block
  yield(head.value)
  tail.each(&block) if tail
end

#empty?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/active_fedora/rdf_list.rb', line 100

def empty?
  !tail && graph.query([subject, RDF.first, RDF.nil]).first
end

#firstObject



55
56
57
# File 'lib/active_fedora/rdf_list.rb', line 55

def first
  self[0] 
end

#headObject



139
140
141
# File 'lib/active_fedora/rdf_list.rb', line 139

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

#initialize(graph, subject) ⇒ Object

RdfList is a node of a linked list structure. The RDF.first predicate points to the contained object The RDF.rest predicate points to the next node in the list or

RDF.nil if this is the final node.


13
14
15
16
17
18
19
20
# File 'lib/active_fedora/rdf_list.rb', line 13

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

#insert_child(predicate, node) ⇒ Object

Override the method from RdfNode, enabling us to insert into the list.



47
48
49
# File 'lib/active_fedora/rdf_list.rb', line 47

def insert_child(predicate, node)
  self[size] = node
end

#inspectObject



114
115
116
# File 'lib/active_fedora/rdf_list.rb', line 114

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

#rdf_subjectObject



51
52
53
# File 'lib/active_fedora/rdf_list.rb', line 51

def rdf_subject
  subject
end

#sizeObject



104
105
106
107
108
109
110
111
112
# File 'lib/active_fedora/rdf_list.rb', line 104

def size
  if empty?
    0
  elsif tail
    tail.size + 1
  else
    1
  end
end

#tailObject



143
144
145
146
147
148
# File 'lib/active_fedora/rdf_list.rb', line 143

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

#tail_or_create(idx) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/active_fedora/rdf_list.rb', line 150

def tail_or_create(idx)
  unless tail
    #add a tail
    graph.delete([subject, RDF.rest, RDF.nil])
    tail_node = RDF::Node.new
    graph.insert([subject, RDF.rest, tail_node])
    @tail = self.class.new(graph, tail_node)
  end

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

#to_aryObject



90
91
92
93
94
95
96
97
98
# File 'lib/active_fedora/rdf_list.rb', line 90

def to_ary
  if empty?
    []
  elsif tail
    [head.value] + tail.to_ary
  else
    [head.value]
  end
end

#valueObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_fedora/rdf_list.rb', line 118

def value
  v = graph.query([subject, RDF.first, nil]).first
  if v.object.uri?
    return v.object == RDF.nil ? nil : v.object
  end
  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



130
131
132
133
134
135
136
137
# File 'lib/active_fedora/rdf_list.rb', line 130

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