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, inherited, rdf_label, repository, transform_type, type

Methods included from Reflection

add_reflection, #reflections

Constructor Details

#initialize(subject: nil, graph: nil, values: nil, &block) ⇒ List

Returns a new instance of List.



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

def initialize(subject: nil, graph: nil, values: nil, &block)
  super
  @graph = ListResource.new(subject) unless
    graph.kind_of? RDFSource
  @graph << parent if parent
  @graph.list = self
  @graph.reload
end

Class Method Details

.from_uri(uri, vals) ⇒ Object



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

def from_uri(uri, vals)
  list = ListResource.from_uri(uri, vals)
  self.new(subject: list.rdf_subject, graph: 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.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/active_triples/list.rb', line 151

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.list = self
    @graph.type = RDF.List
  end

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

#[]=(idx, value) ⇒ Object



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

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



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

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

#each(&block) ⇒ Object

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



65
66
67
68
# File 'lib/active_triples/list.rb', line 65

def each(&block)
  return super unless block_given?
  super { |value| yield node_from_value(value) }
end

#firstObject

Do these like #each.



72
73
74
# File 'lib/active_triples/list.rb', line 72

def first
  node_from_value(super)
end

#node_from_value(value) ⇒ Object

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



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

def node_from_value(value)
  return value unless value.is_a? RDF::Resource
  return value if value.is_a? RDFSource

  type_uri = resource.query([value, RDF.type, nil]).to_a.first.try(:object)
  klass = RDFSource.type_registry[type_uri]
  klass ||= Resource

  klass.from_uri(value, resource)
end

#resourceObject



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

def resource
  graph
end