Skip to content

Commit

Permalink
.to_sym and #to_sym for Format, Reader and Writer, to retrieve a symb…
Browse files Browse the repository at this point in the history
…ol appropriate for use with .for().
  • Loading branch information
gkellogg committed Apr 21, 2011
1 parent ca95691 commit 5a6dbcd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/rdf/format.rb
Expand Up @@ -139,6 +139,16 @@ def self.file_extensions
@@file_extensions
end

##
# Returns a symbol appropriate to use with RDF::Format.for()
# @return [Symbol]
def self.to_sym
elements = self.to_s.split("::")
sym = elements.pop
sym = elements.pop if sym == 'Format'
sym.downcase.to_s.to_sym
end

##
# Retrieves or defines the reader class for this RDF serialization
# format.
Expand Down
17 changes: 17 additions & 0 deletions lib/rdf/reader.rb
Expand Up @@ -127,6 +127,23 @@ def self.open(filename, options = {}, &block)
end
end

##
# Returns a symbol appropriate to use with RDF::Reader.for()
# @return [Symbol]
def self.to_sym
elements = self.to_s.split("::")
sym = elements.pop
sym = elements.pop if sym == 'Reader'
sym.downcase.to_s.to_sym
end

##
# Returns a symbol appropriate to use with RDF::Reader.for()
# @return [Symbol]
def to_sym
self.class.to_sym
end

##
# Initializes the reader.
#
Expand Down
17 changes: 17 additions & 0 deletions lib/rdf/writer.rb
Expand Up @@ -158,6 +158,23 @@ def self.open(filename, options = {}, &block)
end
end

##
# Returns a symbol appropriate to use with RDF::Writer.for()
# @return [Symbol]
def self.to_sym
elements = self.to_s.split("::")
sym = elements.pop
sym = elements.pop if sym == 'Writer'
sym.downcase.to_s.to_sym
end

##
# Returns a symbol appropriate to use with RDF::Writer.for()
# @return [Symbol]
def to_sym
self.class.to_sym
end

##
# Initializes the writer.
#
Expand Down
14 changes: 13 additions & 1 deletion spec/nquads_spec.rb
Expand Up @@ -23,6 +23,10 @@
]
formats.each { |format| format.should == RDF::NQuads::Format }
end

it "should return :nquads for to_sym" do
RDF::NQuads::Format.to_sym.should == :nquads
end
end

describe RDF::NQuads::Reader do
Expand All @@ -34,7 +38,6 @@
# @see lib/rdf/spec/reader.rb in rdf-spec
it_should_behave_like RDF_Reader


it "should be discoverable" do
readers = [
RDF::Reader.for(:nquads),
Expand All @@ -45,6 +48,11 @@
]
readers.each { |reader| reader.should == RDF::NQuads::Reader }
end

it "should return :nquads for to_sym" do
@reader.class.to_sym.should == :nquads
@reader.to_sym.should == :nquads
end
end

describe RDF::NQuads::Writer do
Expand All @@ -60,4 +68,8 @@
]
writers.each { |writer| writer.should == RDF::NQuads::Writer }
end

it "should return :nquads for to_sym" do
RDF::NQuads::Writer.to_sym.should == :nquads
end
end
13 changes: 13 additions & 0 deletions spec/ntriples_spec.rb
Expand Up @@ -22,6 +22,10 @@
]
formats.each { |format| format.should == RDF::NTriples::Format }
end

it "should return :ntriples for to_sym" do
RDF::NTriples::Format.to_sym.should == :ntriples
end
end

describe RDF::NTriples::Reader do
Expand All @@ -42,6 +46,11 @@
]
readers.each { |reader| reader.should == RDF::NTriples::Reader }
end

it "should return :ntriples for to_sym" do
@reader.class.to_sym.should == :ntriples
@reader.to_sym.should == :ntriples
end
end

describe RDF::NTriples::Writer do
Expand All @@ -55,6 +64,10 @@
]
writers.each { |writer| writer.should == RDF::NTriples::Writer }
end

it "should return :ntriples for to_sym" do
RDF::NTriples::Writer.to_sym.should == :ntriples
end
end

describe RDF::NTriples do
Expand Down

0 comments on commit 5a6dbcd

Please sign in to comment.