Class: ActiveTriples::List

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

Overview

An implementation of RDF::List intregrated with ActiveTriples.

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

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

Methods included from Reflection

add_reflection

Constructor Details

#initialize(*args) ⇒ List



28
29
30
31
32
33
34
35
# File 'lib/active_triples/list.rb', line 28

def initialize(*args)
  super
  parent = graph.parent if graph.respond_to? :parent
  @graph = ListResource.new(subject) << graph unless graph.kind_of? RDFSource
  graph << parent if parent
  graph.list = self
  graph.reload
end

Class Method Details

.from_uri(uri, vals) ⇒ Object



18
19
20
21
# File 'lib/active_triples/list.rb', line 18

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.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/active_triples/list.rb', line 158

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 subject == RDF.nil
    @subject = RDF::Node.new
    @graph = ListResource.new(subject)
    @graph.type = RDF.List
  end

  if empty?
    @graph.type = RDF.List
    resource.set_value(RDF.first, value)
    resource.insert([subject, RDF.rest, RDF.nil])
    resource << value if value.kind_of? RDFSource
    return self
  end
  super
  resource << value if value.kind_of? RDFSource
end

#[]=(idx, value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_triples/list.rb', line 49

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

#clearObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_triples/list.rb', line 37

def clear
  graph.send :erase_old_resource
  parent = graph.parent
  old_subject = subject
  super
  @subject = old_subject
  @graph = ListResource.new(subject)
  graph << parent if parent
  graph.parent = parent
  graph.list = self
end

#each(&block) ⇒ Object

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



67
68
69
70
71
72
73
# File 'lib/active_triples/list.rb', line 67

def each(&block)
  return super unless block_given?

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

#firstObject

Do these like #each.



77
78
79
# File 'lib/active_triples/list.rb', line 77

def first
  node_from_value(super)
end

#node_from_value(value) ⇒ Object

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



87
88
89
90
91
92
93
94
95
# File 'lib/active_triples/list.rb', line 87

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 = ActiveTriples::Resource.type_registry[type_uri]
    klass ||= Resource
    return klass.from_uri(value,resource)
  end
  value
end

#resourceObject



24
25
26
# File 'lib/active_triples/list.rb', line 24

def resource
  graph
end

#shiftObject



81
82
83
# File 'lib/active_triples/list.rb', line 81

def shift
  node_from_value(super)
end