Skip to content

Commit

Permalink
Finish 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Dec 30, 2017
2 parents 5861fba + cb2ec01 commit 5b1ad24
Show file tree
Hide file tree
Showing 38 changed files with 1,393 additions and 708 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
language: ruby
bundler_args: --without debug
script: "bundle exec rspec spec"
before_install: "gem update --system"
env:
- CI=true
rvm:
- 2.2
- 2.3
- 2.4
- jruby-9
- 2.5
- jruby
- rbx-3
cache: bundler
sudo: false
matrix:
allow_failures:
- rvm: jruby-9
- rvm: jruby
- rvm: rbx-3
dist: trusty
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ from BNode identity (i.e., they each entail the other)

## Dependencies

* [Ruby](http://ruby-lang.org/) (>= 2.0)
* [Ruby](http://ruby-lang.org/) (>= 2.2)
* [LinkHeader][] (>= 0.0.8)
* Soft dependency on [RestClient][] (>= 1.7)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.12
3.0.0
1 change: 0 additions & 1 deletion lib/df.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/rdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module RDF
# RDF vocabularies
autoload :Vocabulary, 'rdf/vocabulary'
autoload :StrictVocabulary, 'rdf/vocabulary'
VOCABS = Dir.glob(File.join(File.dirname(__FILE__), 'rdf', 'vocab', '*.rb')).map { |f| File.basename(f)[0...-(File.extname(f).size)].to_sym } rescue []
VOCABS = Dir.glob(File.expand_path("../rdf/vocab/*.rb", __FILE__)).map { |f| File.basename(f)[0...-(File.extname(f).size)].to_sym } rescue []

# Use const_missing instead of autoload to load most vocabularies so we can provide deprecation messages
def self.const_missing(constant)
Expand Down
163 changes: 97 additions & 66 deletions lib/rdf/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,83 @@ class Format
##
# Enumerates known RDF serialization format classes.
#
# Given options from {Format.for}, it returns just those formats that match the specified criteria.
#
# @example finding all formats that have a writer supporting text/html
# RDF::Format.each(content_type: 'text/html', has_writer: true).to_a
# #=> RDF::RDFa::Format
#
# @param [String, #to_s] file_name (nil)
# @param [Symbol, #to_sym] file_extension (nil)
# @param [String, #to_s] content_type (nil)
# Content type may include wildcard characters, which will select among matching formats.
# Note that content_type will be taken from a URL opened using {RDF::Util::File.open_file}.
# @param [Boolean] has_reader (false)
# Only return a format having a reader.
# @param [Boolean] has_writer (false)
# Only return a format having a writer.
# @param [String, Proc] sample (nil)
# A sample of input used for performing format detection. If we find no formats, or we find more than one, and we have a sample, we can perform format detection to find a specific format to use, in which case we pick the last one we find
# @yield [klass]
# @yieldparam [Class]
# @return [Enumerator]
def self.each(&block)
@@subclasses.each(&block)
def self.each(file_name: nil,
file_extension: nil,
content_type: nil,
has_reader: false,
has_writer: false,
sample: nil,
**options,
&block)
formats = case
# Find a format based on the MIME content type:
when content_type
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
mime_type = content_type.to_s.split(';').first # remove any media type parameters

# Ignore text/plain, a historical encoding for N-Triples, which is
# problematic in format detection, as many web servers will serve
# content by default text/plain.
if (mime_type == 'text/plain' && sample) || mime_type == '*/*'
# All content types
@@subclasses
elsif mime_type.end_with?('/*')
# All content types that have the first part of the mime-type as a prefix
prefix = mime_type[0..-3]
content_types.map do |ct, fmts|
ct.start_with?(prefix) ? fmts : []
end.flatten.uniq
else
content_types[mime_type]
end
# Find a format based on the file name:
when file_name
ext = File.extname(RDF::URI(file_name).path.to_s)[1..-1].to_s
file_extensions[ext.to_sym]
# Find a format based on the file extension:
when file_extension
file_extensions[file_extension.to_sym]
else
@@subclasses
end || (sample ? @@subclasses : []) # If we can sample, check all classes

# Subset by available reader or writer
formats = formats.select do |f|
has_reader ? f.reader : (has_writer ? f.writer : true)
end

# If we have multiple formats and a sample, use that for format detection
if formats.length != 1 && sample
sample = case sample
when Proc then sample.call.to_s
else sample.dup.to_s
end.force_encoding(Encoding::ASCII_8BIT)
# Given a sample, perform format detection across the appropriate formats, choosing the last that matches
# Return last format that has a positive detection
formats = formats.select {|f| f.detect(sample)}
end
formats.each(&block)
end

##
Expand All @@ -77,6 +149,7 @@ def self.each(&block)
# @option options [String, #to_s] :file_name (nil)
# @option options [Symbol, #to_sym] :file_extension (nil)
# @option options [String, #to_s] :content_type (nil)
# Content type may include wildcard characters, which will select among matching formats.
# Note that content_type will be taken from a URL opened using {RDF::Util::File.open_file}.
# @option options [Boolean] :has_reader (false)
# Only return a format having a reader.
Expand All @@ -88,73 +161,31 @@ def self.each(&block)
# @yieldreturn [String] another way to provide a sample, allows lazy for retrieving the sample.
#
# @return [Class]
def self.for(options = {})
format = case options
when String, RDF::URI
# Find a format based on the file name
fn, options = options, {}
self.for(file_name: fn) { yield if block_given? }

when Hash
case
# Find a format based on the MIME content type:
when mime_type = options[:content_type]
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
mime_type = mime_type.to_s
mime_type = mime_type.split(';').first # remove any media type parameters

# Ignore text/plain, a historical encoding for N-Triples, which is
# problematic in format detection, as many web servers will serve
# content by default text/plain.
content_types[mime_type] unless mime_type == 'text/plain' && (options[:sample] || block_given?)
# Find a format based on the file name:
when file_name = options[:file_name]
self.for(file_extension: File.extname(RDF::URI(file_name).path.to_s)[1..-1]) { yield if block_given? }
# Find a format based on the file extension:
when file_ext = options[:file_extension]
file_extensions[file_ext.to_sym]
end

when Symbol
# Try to find a match based on the full class name
# We want this to work even if autoloading fails
fmt, options = options, {}
classes = @@subclasses.select { |klass| klass.symbols.include?(fmt) }
if classes.empty?
classes = case fmt
when :ntriples then [RDF::NTriples::Format]
when :nquads then [RDF::NQuads::Format]
else []
end
def self.for(*args, **options, &block)
options = {sample: block}.merge(options) if block_given?
formats = case args.first
when String, RDF::URI
# Find a format based on the file name
self.each(file_name: args.first, **options).to_a
when Symbol
# Try to find a match based on the full class name
# We want this to work even if autoloading fails
fmt = args.first
classes = self.each(options).select {|f| f.symbols.include?(fmt)}
if classes.empty?
classes = case fmt
when :ntriples then [RDF::NTriples::Format]
when :nquads then [RDF::NQuads::Format]
else []
end
classes
end

if format.is_a?(Array)
format = format.select {|f| f.reader} if options[:has_reader]
format = format.select {|f| f.writer} if options[:has_writer]

return format.last if format.uniq.length == 1
elsif !format.nil?
return format
end

# If we have a sample, use that for format detection
if sample = (options[:sample] if options.is_a?(Hash)) || (yield if block_given?)
sample = sample.dup.to_s
sample.force_encoding(Encoding::ASCII_8BIT) if sample.respond_to?(:force_encoding)
# Given a sample, perform format detection across the appropriate formats, choosing the last that matches
format ||= @@subclasses

# Return last format that has a positive detection
format.reverse.detect {|f| f.detect(sample)} || format.last
elsif format.is_a?(Array)
# Otherwise, just return the last matching format
format.last
end
classes
else
nil
self.each(options).to_a
end

# Return the last detected format
formats.last
end

##
Expand Down
17 changes: 0 additions & 17 deletions lib/rdf/mixin/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -747,30 +747,13 @@ def dump(*args, **options)
protected

##
# @overload #to_hash
# Returns all RDF object terms indexed by their subject and predicate
# terms.
#
# The return value is a `Hash` instance that has the structure:
# `{subject => {predicate => [*objects]}}`.
#
# @return [Hash]
# @deprecated Use {#to_h} instead.
# @overload #to_writer
# Implements #to_writer for each available instance of {RDF::Writer},
# based on the writer symbol.
#
# @return [String]
# @see {RDF::Writer.sym}
def method_missing(meth, *args)
case meth
when :to_hash
warn "[DEPRECATION] RDF::Enumerable#to_hash is deprecated, use RDF::Enumerable#to_h instead.\n" +
"This is due to the introduction of keyword arugments that attempt to turn the last argument into a hash using #to_hash.\n" +
"This can be avoided by explicitly passing an options hash as the last argument.\n" +
"Called from #{Gem.location_of_caller.join(':')}"
return self.to_h
end
writer = RDF::Writer.for(meth.to_s[3..-1].to_sym) if meth.to_s[0,3] == "to_"
if writer
writer.buffer(standard_prefixes: true) {|w| w << self}
Expand Down
34 changes: 0 additions & 34 deletions lib/rdf/mixin/enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,6 @@ class Enumerator < ::Enumerator
def to_a
return super.to_a.extend(RDF::Queryable, RDF::Enumerable)
end

protected

##
# @overload #to_ary
# @see #to_a
# @deprecated use {#to_a} instead
def method_missing(name, *args)
if name == :to_ary
warn "[DEPRECATION] #{self.class}#to_ary is deprecated, use " \
"#{self.class}#to_a instead. Called from " \
"#{Gem.location_of_caller.join(':')}"
to_a
else
super
end
end
end
end

Expand All @@ -52,23 +35,6 @@ class Enumerator < ::Enumerator
def to_a
return super.to_a.extend(RDF::Queryable, RDF::Enumerable)
end

protected

##
# @overload #to_ary
# @see #to_a
# @deprecated use {#to_a} instead
def method_missing(name, *args)
if name == :to_ary
warn "[DEPRECATION] #{self.class}#to_ary is deprecated, use " \
"#{self.class}#to_a instead. Called from " \
"#{Gem.location_of_caller.join(':')}"
self.to_a
else
super
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rdf/model/literal/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(value, datatype: nil, lexical: nil, **options)
when value.is_a?(::Date) then value
when value.respond_to?(:to_date) then value.to_date
else ::Date.parse(value.to_s)
end rescue nil
end rescue ::Date.new
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/rdf/model/literal/datetime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(value, datatype: nil, lexical: nil, **options)
when value.is_a?(::DateTime) then value
when value.respond_to?(:to_datetime) then value.to_datetime
else ::DateTime.parse(value.to_s)
end rescue nil
end rescue ::DateTime.new
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/rdf/model/literal/decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(value, datatype: nil, lexical: nil, **options)
else
value = value.to_s
value += "0" if value.end_with?(".") # Normalization required in Ruby 2.4
BigDecimal(value) rescue nil
BigDecimal(value) rescue BigDecimal(0)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rdf/model/literal/double.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize(value, datatype: nil, lexical: nil, **options)
end
when value.is_a?(::Float) then value
when value.respond_to?(:to_f) then value.to_f
else Float(value.to_s) rescue nil # FIXME
else 0.0 # FIXME
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rdf/model/literal/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(value, datatype: nil, lexical: nil, **options)
@object = case
when value.is_a?(::Integer) then value
when value.respond_to?(:to_i) then value.to_i
else Integer(value.to_s) rescue nil
else 0
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rdf/model/literal/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(value, datatype: nil, lexical: nil, **options)
when value.is_a?(::DateTime) then value
when value.respond_to?(:to_datetime) then value.to_datetime rescue ::DateTime.parse(value.to_s)
else ::DateTime.parse(value.to_s)
end rescue nil
end rescue ::DateTime.new
end

##
Expand Down
17 changes: 3 additions & 14 deletions lib/rdf/model/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,11 @@ def self.cache
# * `:default` Produces 36 characters, including hyphens separating the UUID value parts
# * `:compact` Produces a 32 digits (hexadecimal) value with no hyphens
# * `:urn` Adds the prefix urn:uuid: to the default format
# @param [Regexp] grammar (nil)
# a grammar specification that the generated UUID must match
# The UUID is generated such that its initial part is guaranteed
# to match the given `grammar`, e.g. `/^[A-Za-z][A-Za-z0-9]*/`.
# Some RDF storage systems (e.g. AllegroGraph) require this.
#
# Requires that the `uuid` gem be loadable to use `format`
# @return [RDF::Node]
def self.uuid(format: :default, grammar: nil)
case
when grammar
warn "[DEPRECATION] The grammar parameter to RDF::Node#uri is deprecated.\n" +
"Called from #{Gem.location_of_caller.join(':')}"
uuid = RDF::Util::UUID.generate(format: format) until uuid =~ grammar
else
uuid = RDF::Util::UUID.generate(format: format)
end
def self.uuid(format: :default)
uuid = RDF::Util::UUID.generate(format: format)
self.new(uuid)
end

Expand Down
Loading

0 comments on commit 5b1ad24

Please sign in to comment.