Permalink
Browse files

Port back more functionality from the 1.1 branch and describe changes…

… to come.
  • Loading branch information...
1 parent a18220e commit 5b42115267088303eeb17dc2618c4e0986489778 @gkellogg gkellogg committed Mar 20, 2013
Showing with 39 additions and 12 deletions.
  1. +39 −12 lib/rdf/model/graph.rb
View
@@ -31,16 +31,24 @@ class Graph
##
# Returns the options passed to this graph when it was constructed.
#
+ # @!attribute [r] options
# @return [Hash{Symbol => Object}]
attr_reader :options
##
+ # Name of this graph, if it is part of an {RDF::Repository}
+ # @!attribute [rw] context
# @return [RDF::Resource]
# @note In the next release, only projections from an
# {RDF::Enumerable} supporting contexts will have a context.
attr_accessor :context
+ alias_method :name, :context
+ alias_method :name=, :context=
+
##
+ # {RDF::Queryable} backing this graph.
+ # @!attribute [rw] data
# @return [RDF::Queryable]
attr_accessor :data
@@ -141,12 +149,12 @@ def unnamed?
end
##
- # Returns `false` to indicate that this graph is not durable
+ # A graph is durable if it's underlying data model is durable
#
# @return [Boolean]
# @see RDF::Durable#durable?
def durable?
- false
+ @data.durable?
end
##
@@ -158,10 +166,9 @@ def contexts(options = {})
end
##
- # Returns the URI representation of this graph.
+ # Returns the {RDF::Resource} representation of this graph.
#
- # @return [RDF::URI]
- # @note The next release, graphs will not be named, this will return nil
+ # @return [RDF::Resource]
def to_uri
context
end
@@ -171,7 +178,7 @@ def to_uri
#
# @return [String]
def to_s
- named? ? context.to_s : "<>"
+ named? ? context.to_s : "default"
end
##
@@ -180,7 +187,7 @@ def to_s
# @return [Boolean]
# @see RDF::Enumerable#empty?
def empty?
- @data.empty?
+ !@data.has_context?(context || false)
end
##
@@ -198,7 +205,7 @@ def anonymous?
# @return [Integer]
# @see RDF::Enumerable#count
def count
- @data.query(:context => context).count
+ @data.query(:context => context || false).count
end
##
@@ -209,7 +216,7 @@ def count
# @see RDF::Enumerable#has_statement?
def has_statement?(statement)
statement = statement.dup
- statement.context = context # TODO: going away
+ statement.context = context
@data.has_statement?(statement)
end
@@ -221,15 +228,35 @@ def has_statement?(statement)
# @return [Enumerator]
# @see RDF::Enumerable#each_statement
def each(&block)
- @data.query(:context => context).each(&block)
+ if @data.respond_to?(:query)
+ @data.query(:context => context || false, &block)
+ elsif @data.respond_to?(:each)
+ @data.each(&block)
+ else
+ @data.to_a.each(&block)
+ end
+ end
+
+ ##
+ # Graph equivalence based on the contents of each graph being _exactly_
+ # the same. To determine if the have the same _meaning_, consider
+ # [rdf-isomorphic](http://rubygems.org/gems/rdf-isomorphic).
+ #
+ # @param [RDF::Graph] other
+ # @return [Boolean]
+ # @see http://rubygems.org/gems/rdf-isomorphic
+ def ==(other)
+ other.is_a?(RDF::Graph) &&
+ context == other.context &&
+ statements.to_a == other.statements.to_a
end
##
# @private
# @see RDF::Queryable#query
def query_pattern(pattern, &block)
pattern = pattern.dup
- pattern.context = context
+ pattern.context = context || false
@data.query(pattern, &block)
end
@@ -255,7 +282,7 @@ def delete_statement(statement)
# @private
# @see RDF::Mutable#clear
def clear_statements
- @data.delete(:context => context)
+ @data.delete(:context => context || false)
end
protected :query_pattern

0 comments on commit 5b42115

Please sign in to comment.