Skip to content

Commit

Permalink
Implemented RDF::URI#root?, #root, #has_parent? and #parent.
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Feb 1, 2010
1 parent 78bc806 commit d5b0e29
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 13 deletions.
81 changes: 70 additions & 11 deletions lib/rdf/model/uri.rb
Expand Up @@ -63,10 +63,59 @@ def anonymous?
false
end

##
# Returns `true` if this URI's path component is equal to `/`.
#
# @return [Boolean]
def root?
self.path == '/' || self.path.empty?
end

##
# Returns a copy of this URI with the path component set to `/`.
#
# @return [URI]
def root
if root?
self
else
uri = self.dup
uri.path = '/'
uri
end
end

##
# Returns `true` if this URI's path component isn't equal to `/`.
#
# @return [Boolean]
def has_parent?
!root?
end

##
# Returns a copy of this URI with the path component ascended to the
# parent directory, if any.
#
# @return [URI]
def parent
case
when root? then nil
else
require 'pathname' unless defined?(Pathname)
if path = Pathname.new(self.path).parent
uri = self.dup
uri.path = path.to_s
uri.path << '/' unless uri.root?
uri
end
end
end

##
# Returns a duplicate copy of `self`.
#
# @return [RDF::URI]
# @return [URI]
def dup
self.class.new(@uri.dup)
end
Expand Down Expand Up @@ -118,18 +167,28 @@ def hash
@uri.hash
end

protected
##
# Returns `true` if this URI instance supports the `symbol` method.
#
# @param [Symbol, String, #to_s] symbol
# @return [Boolean]
def respond_to?(symbol)
@uri.respond_to?(symbol) || super
end

def respond_to?(symbol) #:nodoc:
@uri.respond_to?(symbol) || super
##
# @param [Symbol, String, #to_s] symbol
# @param [Array<Object>] args
# @yield
# @private
def method_missing(symbol, *args, &block)
if @uri.respond_to?(symbol)
@uri.send(symbol, *args, &block)
else
super
end
end

def method_missing(symbol, *args, &block) #:nodoc:
if @uri.respond_to?(symbol)
@uri.send(symbol, *args, &block)
else
super
end
end
protected :method_missing
end
end
20 changes: 18 additions & 2 deletions spec/model_uri_spec.rb
Expand Up @@ -5,9 +5,25 @@
lambda { RDF::URI.new("http://rdf.rubyforge.org/") }.should_not raise_error
end

it "should return the root URI" do
uri = RDF::URI.new("http://rdf.rubyforge.org/RDF/URI.html")
uri.should respond_to(:root)
uri.root.should be_a_uri
uri.root.should == RDF::URI.new("http://rdf.rubyforge.org/")
end

it "should find the parent URI" do
uri = RDF::URI.new("http://rdf.rubyforge.org/RDF/URI.html")
uri.should respond_to(:parent)
uri.parent.should be_a_uri
uri.parent.should == RDF::URI.new("http://rdf.rubyforge.org/RDF/")
uri.parent.parent.should == RDF::URI.new("http://rdf.rubyforge.org/")
uri.parent.parent.parent.should be_nil
end

it "should return a consistent hash code" do
hash1 = RDF::URI.parse("http://rdf.rubyforge.org").hash
hash2 = RDF::URI.parse("http://rdf.rubyforge.org").hash
hash1 = RDF::URI.new("http://rdf.rubyforge.org/").hash
hash2 = RDF::URI.new("http://rdf.rubyforge.org/").hash
hash1.should == hash2
end

Expand Down

0 comments on commit d5b0e29

Please sign in to comment.