Class: ActiveFedora::Rdf::List

Inherits:
RDF::List
  • Object
show all
Extended by:
Configurable, Properties
Includes:
NestedAttributes
Defined in:
lib/active_fedora/rdf/list.rb

Overview

An implementation of RDF::List intregrated with ActiveFedora::Rdf.

A thoughtful reflection period is encouraged before using the rdf:List concept in your data. The community may pursue other options for ordered sets.

Defined Under Namespace

Classes: ListResource

Instance Attribute Summary

Attributes included from Properties

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

base_uri, configure, rdf_label, rdf_type, repository, transform_type, type

Methods included from Properties

config_for_term_or_uri, fields, map_predicates, property

Constructor Details

#initialize(*args) ⇒ List

Returns a new instance of List.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_fedora/rdf/list.rb', line 27

def initialize(*args)
  super
  parent = graph.parent if graph.respond_to? :parent
  @graph = ListResource.new(subject) << graph unless graph.kind_of? Resource
  graph << parent if parent
  graph.list = self
  graph.singleton_class.properties = self.class.properties
  graph.singleton_class.properties.keys.each do |property|
    graph.singleton_class.send(:register_property, property)
  end
  graph.insert RDF::Statement.new(subject, RDF.type, RDF.List)
  graph.reload
end

Class Method Details

.from_uri(uri, vals) ⇒ Object



17
18
19
20
# File 'lib/active_fedora/rdf/list.rb', line 17

def from_uri(uri, vals)
  list = ListResource.from_uri(uri, vals)
  self.new(list.rdf_subject, list)
end

Instance Method Details

#<<(value) ⇒ Object

Monkey patch to allow lists to have subject URIs. Overrides RDF::List to prevent URI subjects from being replaced with nodes.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/active_fedora/rdf/list.rb', line 137

def <<(value)
  value = case value
    when nil         then RDF.nil
    when RDF::Value  then value
    when Array       then RDF::List.new(nil, graph, value)
    else value
  end

  if empty?
    resource.set_value(RDF.first, value)
    resource.insert([subject, RDF.rest, RDF.nil])
    resource << value if value.kind_of? Resource
    return self
  end
  super
  resource << value if value.kind_of? Resource
end

#[]=(idx, value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_fedora/rdf/list.rb', line 41

def []=(idx, value)
  raise IndexError "index #{idx} too small for array: minimum 0" if idx < 0

  if idx >= length
    (idx - length).times do
      self << RDF::OWL.Nothing
    end
    return self << value
  end
  each_subject.with_index do |v, i|
    next unless i == idx
    resource.set_value(v, RDF.first, value)
  end
end

#each(&block) ⇒ Object

Override to return AF::Rdf::Resources as values, where appropriate.



59
60
61
62
63
64
65
# File 'lib/active_fedora/rdf/list.rb', line 59

def each(&block)
  return super unless block_given?

  super do |value|
    block.call(node_from_value(value))
  end
end

#firstObject

Do these like #each.



69
70
71
# File 'lib/active_fedora/rdf/list.rb', line 69

def first
  node_from_value(super)
end

#node_from_value(value) ⇒ Object

Find an AF::Rdf::Resource from the value returned by RDF::List



79
80
81
82
83
84
85
86
87
# File 'lib/active_fedora/rdf/list.rb', line 79

def node_from_value(value)
  if value.kind_of? RDF::Resource
    type_uri = resource.query([value, RDF.type, nil]).to_a.first.try(:object)
    klass = ActiveFedora::Rdf::Resource.type_registry[type_uri]
    klass ||= Resource
    return klass.from_uri(value,resource)
  end
  value
end

#resourceObject



23
24
25
# File 'lib/active_fedora/rdf/list.rb', line 23

def resource
  graph
end

#shiftObject



73
74
75
# File 'lib/active_fedora/rdf/list.rb', line 73

def shift
  node_from_value(super)
end