Module: ActiveFedora::RdfList

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

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?, #method_missing, #new_record=, #new_record?, #query, rdf_registry, #reset_child_cache!, #reset_rdf_subject!, #set_value, #target_class

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveFedora::RdfNode

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



49
50
51
# File 'lib/active_fedora/rdf_list.rb', line 49

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

#[]=(idx, value) ⇒ Object



53
54
55
# File 'lib/active_fedora/rdf_list.rb', line 53

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



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

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.is_a?(String)
    attributes_collection = [attributes_collection]
  end

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

#firstObject



45
46
47
# File 'lib/active_fedora/rdf_list.rb', line 45

def first
  self[0] 
end

#headObject



90
91
92
# File 'lib/active_fedora/rdf_list.rb', line 90

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

#initialize(graph, subject) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/active_fedora/rdf_list.rb', line 8

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



67
68
69
# File 'lib/active_fedora/rdf_list.rb', line 67

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

#rdf_subjectObject



41
42
43
# File 'lib/active_fedora/rdf_list.rb', line 41

def rdf_subject
  subject
end

#sizeObject



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

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

#tailObject



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

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



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

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



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

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



81
82
83
84
85
86
87
88
# File 'lib/active_fedora/rdf_list.rb', line 81

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