Sha256: 4aa71d98a96407794e7a12b41aeec1baa0bd174d97c22d6e3cc8cd0a3495cc16

Contents?: true

Size: 1.99 KB

Versions: 18

Compression:

Stored size: 1.99 KB

Contents

module ActiveFedora
  module RdfList
    attr_reader :graph, :subject
    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

    def first
      self[0] 
    end

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

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

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

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

    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

    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

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

    def tail
      rest = graph.query([subject, RDF.rest, nil]).first
      return if rest.object == RDF.nil
      self.class.new(graph, rest.object)
    end

    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
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
active-fedora-6.4.0.rc1 lib/active_fedora/rdf_list.rb
active-fedora-6.3.0 lib/active_fedora/rdf_list.rb
active-fedora-6.2.0 lib/active_fedora/rdf_list.rb
active-fedora-6.1.1 lib/active_fedora/rdf_list.rb
active-fedora-6.1.0 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.rc7 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.rc6 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.rc5 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.rc4 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.rc3 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.rc2 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.rc1 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.pre10 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.pre9 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.pre8 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.pre7 lib/active_fedora/rdf_list.rb
active-fedora-6.0.0.pre6 lib/active_fedora/rdf_list.rb