lib/rdf/json/extensions.rb in rdf-json-0.1.1 vs lib/rdf/json/extensions.rb in rdf-json-0.2.0

- old
+ new

@@ -30,10 +30,19 @@ # @example Serializing enumerables into RDF/JSON format # [RDF::Statement.new(s, p, o)].extend(RDF::Enumerable).to_json # module Extensions ## + # @private + def self.install! + self.constants.each do |klass| + RDF.const_get(klass).send(:include, self.const_get(:Value)) # needed on Ruby 1.8.x + RDF.const_get(klass).send(:include, self.const_get(klass)) + end + end + + ## # RDF/JSON extensions for `RDF::Value`. module Value ## # Returns the serialized RDF/JSON representation of this value. # @@ -75,13 +84,13 @@ # Returns the RDF/JSON representation of this literal. # # @return [Hash] def to_rdf_json case - when datatype? # FIXME: use `has_datatype?` in RDF.rb 0.1.0 + when has_datatype? {:type => :literal, :value => value.to_s, :datatype => datatype.to_s} - when language? # FIXME: use `has_language?` in RDF.rb 0.1.0 + when has_language? {:type => :literal, :value => value.to_s, :lang => language.to_s} else {:type => :literal, :value => value.to_s} end end @@ -93,13 +102,11 @@ ## # Returns the RDF/JSON representation of this statement. # # @return [Hash] def to_rdf_json - # FIXME: improve the RDF::Statement constructor in RDF.rb 0.1.0 - s, p, o = subject.to_s, predicate.to_s, object.is_a?(RDF::Value) ? object : RDF::Literal.new(object) - {s => {p => [o.to_rdf_json]}} + {subject.to_s => {predicate.to_s => [object.to_rdf_json]}} end end ## # RDF/JSON extensions for `RDF::Enumerable`. @@ -141,9 +148,7 @@ module Repository include Enumerable end end # module Extensions - Extensions.constants.each do |klass| - RDF.const_get(klass).send(:include, Extensions.const_get(klass)) - end + Extensions.install! end # module RDF::JSON