class ActiveTriples::List

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.

Public Class Methods

from_uri(uri, vals) click to toggle source
# 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
new(subject: nil, graph: nil, values: nil, &block) click to toggle source
Calls superclass method
# 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

Public Instance Methods

<<(value) click to toggle source

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

@NOTE Lists built this way will return false for valid?

Calls superclass method
# 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) click to toggle source
# 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
clear() click to toggle source
Calls superclass method
# 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() { |node_from_value(value)| ... } click to toggle source

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

Calls superclass method
# File lib/active_triples/list.rb, line 65
def each(&block)
  return super unless block_given?
  super { |value| yield node_from_value(value) }
end
first() click to toggle source

Do these like each.

Calls superclass method
# File lib/active_triples/list.rb, line 72
def first
  node_from_value(super)
end
node_from_value(value) click to toggle source

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

# 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
resource() click to toggle source
# File lib/active_triples/list.rb, line 25
def resource
  graph
end