diff --git a/lib/spira/types.rb b/lib/spira/types.rb index bbda8cb..25a9928 100644 --- a/lib/spira/types.rb +++ b/lib/spira/types.rb @@ -19,6 +19,7 @@ module Types require 'spira/types/any' require 'spira/types/string' require 'spira/types/float' + require 'spira/types/uri' end diff --git a/lib/spira/types/uri.rb b/lib/spira/types/uri.rb new file mode 100644 index 0000000..2cd3a00 --- /dev/null +++ b/lib/spira/types/uri.rb @@ -0,0 +1,25 @@ +module Spira::Types + + ## + # This type takes RDF Resource objects and provides RDF::URI objects for the + # ruby representation. + # + # @see Spira::Type + # @see http://rdf.rubyforge.org/RDF/URI.html + class URI + + include Spira::Type + + def self.unserialize(value) + RDF::URI(value) + end + + def self.serialize(value) + RDF::URI(value) + end + + register_alias :uri + register_alias RDF::URI + + end +end diff --git a/spec/types/uri.spec b/spec/types/uri.spec new file mode 100644 index 0000000..cf74fb9 --- /dev/null +++ b/spec/types/uri.spec @@ -0,0 +1,40 @@ +require File.dirname(__FILE__) + "/../spec_helper.rb" + +describe Spira::Types::URI do + + before :each do + @uri = RDF::URI('http://example.org/example') + end + + context "when serializing" do + it "should serialize URIs to URIs" do + serialized = Spira::Types::URI.serialize(@uri) + serialized.should be_a RDF::URI + serialized.should == @uri + end + + it "should serialize non-URIs to URIs based on the URI constructor" do + serialized = Spira::Types::URI.serialize("test") + serialized.should be_a RDF::URI + serialized.should == RDF::URI('test') + end + + end + + context "when unserializing" do + it "should unserialize URIs to themselves" do + value = Spira::Types::URI.unserialize(@uri) + value.should be_a RDF::URI + value.should == @uri + end + + it "should unserialize non-URIs to URIs based on the URI constructor" do + value = Spira::Types::URI.unserialize("test") + value.should be_a RDF::URI + value.should == RDF::URI('test') + end + end + + +end +