Skip to content

Commit

Permalink
More refactoring and stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdcawley committed Aug 15, 2008
1 parent e23785e commit a98f9e2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 33 deletions.
10 changes: 2 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'rubygems'
require 'rake'
require 'spec/rake/spectask'
require 'yard'

task :default => [:spec]

Expand Down Expand Up @@ -29,11 +28,6 @@ task :spec do
sh "spec --colour spec"
end

YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb'] # optional
# t.options = ['--any', '--extra', '--opts'] # optional
end

desc "Turns spec results into HTML and publish to web (Tom only!)"
task :spec_html do
sh "spec --format html:rena_new_spec.html spec"
Expand All @@ -56,5 +50,5 @@ end

desc "Runs specs on JRuby"
task :jspec do
sh "jruby -S `whereis spec` --colour spec"
end
sh "jruby -S spec --colour --pattern test/spec/*.spec.rb"
end
2 changes: 2 additions & 0 deletions lib/rena/bnode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def eql? (other)
other.is_a?(self.class) && other.identifier == self.identifier
end

alias_method :==, :eql?

##
# Exports the BNode in N-Triples form.
#
Expand Down
10 changes: 3 additions & 7 deletions lib/rena/literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ module Rena
class Literal
attr_accessor :contents, :lang
def initialize(contents, lang = nil)
@contents = contents
@contents = contents.to_s
if lang != nil && lang != false
@lang = lang.downcase
end
end

def == (obj)
if obj.class == Literal && obj.contents == @contents && (obj.lang == @lang || (obj.lang == nil && @lang == nil))
true
else
false
end
obj.is_a?(self.class) && obj.contents == @contents && obj.lang == @lang
end

def to_n3
Expand Down Expand Up @@ -88,4 +84,4 @@ def infer!
end
end
end
end
end
44 changes: 27 additions & 17 deletions lib/rena/triple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ class Triple
class InvalidPredicate < StandardError
end

class InvalidSubject < StandardError
end

class InvalidObject < StandardError
end

attr_accessor :subject, :object, :predicate

##
Expand All @@ -22,9 +28,9 @@ class InvalidPredicate < StandardError
# @raise [Error] Checks parameter types and raises if they are incorrect.
# @author Tom Morris
def initialize (subject, predicate, object)
self.check_subject(subject)
@predicate = coerce_predicate(predicate)
self.check_object(object)
@subject = self.class.coerce_subject(subject)
@predicate = self.class.coerce_predicate(predicate)
@object = coerce_object(object)
end

def to_ntriples
Expand All @@ -33,22 +39,23 @@ def to_ntriples

protected

def check_subject(subject)
if subject.class == BNode || subject.class == URIRef
@subject = subject
elsif subject.class == String
def self.coerce_subject(subject)
case subject
when URIRef, BNode
subject
when String
if subject =~ /\S+\/\/\S+/ # does it smell like a URI?
@subject = URIRef.new(subject)
URIRef.new(subject)
else
@subject = BNode.new(subject)
BNode.new(subject)
end
else
raise "Subject is not of a known class"
raise InvalidSubject, "Subject is not of a known class"
end
end

protected
def coerce_predicate(uri_or_string)
def self.coerce_predicate(uri_or_string)
case uri_or_string
when URIRef
uri_or_string
Expand All @@ -57,16 +64,19 @@ def coerce_predicate(uri_or_string)
else
raise InvalidPredicate, "Predicate should be a URI"
end
rescue UriRelativeException => e
raise InvalidPredicate, "Couldn't make a URIRef: #{e.message}"
end

protected
def check_object(object)
if [String, Integer, Fixnum, Float].include? object.class
@object = Literal.new(object.to_s)
elsif [URIRef, BNode, Literal, TypedLiteral].include? object.class
@object = object
def coerce_object(object)
case object
when String, Integer, Fixnum, Float
Literal.new object
when URIRef, BNode, Literal, TypedLiteral
object
else
raise "Object expects valid class"
raise InvalidObject, "#{object.inspect} is not a valid object"
end
end
end
Expand Down
49 changes: 48 additions & 1 deletion spec/triple_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,52 @@
it "should emit an NTriple" do
f = Triple.new(URIRef.new("http://tommorris.org/foaf#me"), URIRef.new("http://xmlns.com/foaf/0.1/name"), Literal.new("Tom Morris"))
f.to_ntriples.should == "<http://tommorris.org/foaf#me> <http://xmlns.com/foaf/0.1/name> \"Tom Morris\" ."
end
end

describe "#coerce_subject" do
it "Should accept a URIRef" do
ref = URIRef.new('http://localhost/')
Triple.coerce_subject(ref).should == ref
end

it "Should accept a BNode" do
node = BNode.new('a')
Triple.coerce_subject(node).should == node
end

it "should accept a uri string and make URIRef" do
Triple.coerce_subject('http://localhost/').should == URIRef.new('http://localhost/')
end

it "should turn an other string into a BNode" do
Triple.coerce_subject('foo').should == BNode.new('foo')
end

it "should raise an InvalidSubject exception with any other class argument" do
lambda do
Triple.coerce_subject(Object.new)
end.should raise_error(Rena::Triple::InvalidSubject)
end
end

describe "#coerce_predicate" do
it "should make a string into a URI ref" do
Triple.coerce_predicate("http://localhost/").should == URIRef.new('http://localhost')
end

it "should leave a URIRef alone" do
ref = URIRef.new('http://localhost/')
Triple.coerce_predicate(ref).should == ref
end

it "should barf on an illegal uri string" do
lambda do
Triple.coerce_predicate("I'm just a soul whose intention is good")
end.should raise_error(Rena::Triple::InvalidPredicate)
end
end

describe "#coerce_object" do
it "should be tested"
end
end

0 comments on commit a98f9e2

Please sign in to comment.