Sha256: af58e70750cc2cc7097489c01da7d9a95bc21babef0b5375632e68fec529d2aa

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module ActiveFedora
  module RdfNode
    class TermProxy

      attr_reader :graph, :subject, :predicate, :options

      delegate :class, :to_s, :==, :kind_of?, :each, :map, :empty?, :as_json, 
               :is_a?, :to_ary, :inspect, :first, :last, :include?, :count, 
               :to => :values

      # @param graph RDF::Graph
      # @param subject RDF::URI
      # @param options Hash
      def initialize(graph, subject, predicate, options)
        @graph = graph
        @subject = subject
        @predicate = predicate
        @options = options
      end

      def build
        new_subject = RDF::Node.new
        graph.graph.insert([subject, predicate, new_subject])
        graph.target_class(predicate).new(graph.graph, new_subject)
      end

      def <<(*values)
        values.each { |value| graph.append(subject, predicate, value) }
        values
      end

      def delete(*values)
        values.each do |value| 
          graph.delete_predicate(subject, predicate, value)
        end

        values
      end

      def values
        values = []

        graph.query(subject, predicate).each do |solution|
          v = solution.value
          v = v.to_s if v.is_a? RDF::Literal
          if options[:type] == :date
            v = Date.parse(v)
          end
          values << v
        end
        if options[:class_name]
          values = values.map{ |found_subject| graph.target_class(predicate).new(graph.graph, found_subject)}
        end
        
        values
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active-fedora-6.0.0.pre9 lib/active_fedora/rdf_node/term_proxy.rb
active-fedora-5.6.0 lib/active_fedora/rdf_node/term_proxy.rb
active-fedora-6.0.0.pre8 lib/active_fedora/rdf_node/term_proxy.rb