lib/rdf/model/statement.rb in rdf-0.1.10 vs lib/rdf/model/statement.rb in rdf-0.2.0
- old
+ new
@@ -16,64 +16,75 @@
# :subject => RDF::URI.new("http://rubygems.org/gems/rdf"),
# :predicate => RDF::DC.creator,
# :object => RDF::URI.new("http://ar.to/#self"),
# })
#
- class Statement < Value
+ class Statement
+ include RDF::Value
+
# @return [Object]
attr_accessor :id
- # @return [Resource]
+ # @return [RDF::Resource]
attr_accessor :context
- # @return [Resource]
+ # @return [RDF::Resource]
attr_accessor :subject
- # @return [URI]
+ # @return [RDF::URI]
attr_accessor :predicate
- # @return [Value]
+ # @return [RDF::Value]
attr_accessor :object
##
# @overload initialize(options = {})
# @param [Hash{Symbol => Object}] options
- # @option options [Resource] :subject (nil)
- # @option options [URI] :predicate (nil)
- # @option options [Value] :object (nil)
- # @option options [Resource] :context (nil)
+ # @option options [RDF::Resource] :subject (nil)
+ # @option options [RDF::URI] :predicate (nil)
+ # @option options [RDF::Value] :object (nil)
+ # @option options [RDF::Resource] :context (nil)
#
# @overload initialize(subject, predicate, object, options = {})
- # @param [Resource] subject
- # @param [URI] predicate
- # @param [Value] object
+ # @param [RDF::Resource] subject
+ # @param [RDF::URI] predicate
+ # @param [RDF::Value] object
# @param [Hash{Symbol => Object}] options
- # @option options [Resource] :context (nil)
+ # @option options [RDF::Resource] :context (nil)
def initialize(subject = nil, predicate = nil, object = nil, options = {})
- options = options.dup unless options.empty?
case subject
when Hash
- options = subject.dup
- subject = options.delete(:subject)
- predicate = options.delete(:predicate)
- object = options.delete(:object)
- initialize(subject, predicate, object, options)
+ @options = subject.dup
+ @subject = @options.delete(:subject)
+ @predicate = @options.delete(:predicate)
+ @object = @options.delete(:object)
else
- @id = options.delete(:id) if options.has_key?(:id)
- @context = options.delete(:context) || options.delete(:graph)
- @options = options
+ @options = !options.empty? ? options.dup : {}
@subject = subject
@predicate = predicate
- @object = case object
- when nil then nil
- when RDF::Value then object
- else RDF::Literal.new(object)
- end
+ @object = object
end
+ @id = @options.delete(:id) if @options.has_key?(:id)
+ @context = @options.delete(:context) || @options.delete(:graph)
+ initialize!
end
##
+ # @private
+ def initialize!
+ @context = Node.intern(@context) if @context.is_a?(Symbol)
+ @subject = Node.intern(@subject) if @subject.is_a?(Symbol)
+ @predicate = Node.intern(@predicate) if @predicate.is_a?(Symbol)
+ @object = case @object
+ when nil then nil
+ when Symbol then Node.intern(@object)
+ when Value then @object
+ else Literal.new(@object)
+ end
+ end
+
+ ##
# @return [Boolean]
def invalid?
!valid?
end
@@ -165,36 +176,43 @@
return true
end
##
# @param [Integer] index
- # @return [Value]
+ # @return [RDF::Value]
def [](index)
- to_a[index]
+ case index
+ when 0 then self.subject
+ when 1 then self.predicate
+ when 2 then self.object
+ when 3 then self.context
+ else nil
+ end
end
##
- # @param [Integer] index
- # @param [Value] value
- # @return [Value]
+ # @param [Integer] index
+ # @param [RDF::Value] value
+ # @return [RDF::Value]
def []=(index, value)
case index
- when 0 then subject = value
- when 1 then predicate = value
- when 2 then object = value
- when 3 then context = value
+ when 0 then self.subject = value
+ when 1 then self.predicate = value
+ when 2 then self.object = value
+ when 3 then self.context = value
+ else nil
end
end
##
- # @return [Array(Value)]
+ # @return [Array(RDF::Value)]
def to_quad
[subject, predicate, object, context]
end
##
- # @return [Array(Value)]
+ # @return [Array(RDF::Value)]
def to_triple
[subject, predicate, object]
end
alias_method :to_a, :to_triple
@@ -204,12 +222,12 @@
# Returns the terms of this statement as a `Hash`.
#
# @param [Symbol] subject_key
# @param [Symbol] predicate_key
# @param [Symbol] object_key
- # @return [Hash{Symbol => Value}]
- def to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object)
- {subject_key => subject, predicate_key => predicate, object_key => object}
+ # @return [Hash{Symbol => RDF::Value}]
+ def to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object, context_key = :context)
+ {subject_key => subject, predicate_key => predicate, object_key => object, context_key => context}
end
##
# Returns a string representation of this statement.
#