Skip to content

Commit

Permalink
Finish 2.2.10
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Sep 27, 2017
2 parents 5484aad + 76ea1fb commit 9c9e551
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 24 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
2.2.9
2.2.10
14 changes: 8 additions & 6 deletions lib/rdf.rb
Expand Up @@ -93,19 +93,19 @@ def self.Node(*args, &block)
#
# @param (see RDF::URI#initialize)
# @return [RDF::URI]
def self.URI(*args, &block)
(uri = args.first).respond_to?(:to_uri) ? uri.to_uri : URI.new(*args, &block)
def self.URI(uri, *args, &block)
uri.respond_to?(:to_uri) ? uri.to_uri : URI.new(uri, *args, &block)
end

##
# Alias for `RDF::Literal.new`.
#
# @param (see RDF::Literal#initialize)
# @return [RDF::Literal]
def self.Literal(*args, &block)
case literal = args.first
def self.Literal(literal, *args, &block)
case literal
when RDF::Literal then literal
else Literal.new(*args, &block)
else Literal.new(literal, *args, &block)
end
end

Expand Down Expand Up @@ -220,12 +220,14 @@ def self.respond_to?(method, include_all = false)
super || RDF::RDFV.respond_to?(method, include_all)
end

RDF_N_REGEXP = %r{_\d+}.freeze

##
# Delegate other methods to RDF::RDFV
def self.method_missing(property, *args, &block)
if args.empty?
# Special-case rdf:_n for all integers
property.to_s =~ %r{_\d+} ? RDF::URI("#{to_uri}#{property}") : RDF::RDFV.send(property)
RDF_N_REGEXP.match(property) ? RDF::URI("#{to_uri}#{property}") : RDF::RDFV.send(property)
else
super
end
Expand Down
29 changes: 17 additions & 12 deletions lib/rdf/model/uri.rb
Expand Up @@ -81,7 +81,7 @@ class URI
IRI = Regexp.compile("^#{SCHEME}:(?:#{IHIER_PART})(?:\\?#{IQUERY})?(?:\\##{IFRAGMENT})?$").freeze

# Split an IRI into it's component parts
IRI_PARTS = /^(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(\?[^#]*)?(#.*)?$/
IRI_PARTS = /^(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(\?[^#]*)?(#.*)?$/.freeze

# Remove dot expressions regular expressions
RDS_2A = /^\.?\.\/(.*)$/.freeze
Expand Down Expand Up @@ -141,10 +141,9 @@ def self.cache
#
# @param (see #initialize)
# @return [RDF::URI] an immutable, frozen URI object
def self.intern(*args)
str = args.first
def self.intern(str, *args)
args << {} unless args.last.is_a?(Hash) # FIXME: needed until #to_hash is removed to avoid DEPRECATION warning.
(cache[(str = str.to_s).to_sym] ||= self.new(*args)).freeze
(cache[(str = str.to_s).to_sym] ||= self.new(str, *args)).freeze
end

##
Expand Down Expand Up @@ -224,6 +223,7 @@ def self.normalize_path(path)
# @param [Boolean] validate (false)
# @param [Boolean] canonicalize (false)
def initialize(*args, validate: false, canonicalize: false, **options)
@value = @object = @hash = nil
uri = args.first
if uri
@value = uri.to_s
Expand Down Expand Up @@ -344,7 +344,7 @@ def length
# @return [Boolean] `true` or `false`
# @since 0.3.9
def valid?
to_s.match(RDF::URI::IRI) || false
RDF::URI::IRI.match(to_s) || false
end

##
Expand Down Expand Up @@ -796,7 +796,8 @@ def inspect
# lexical representation of URI, either absolute or relative
# @return [String]
def value
@value ||= [
return @value if @value
@value = [
("#{scheme}:" if absolute?),
("//#{authority}" if authority),
path,
Expand All @@ -810,15 +811,15 @@ def value
#
# @return [Integer]
def hash
@hash ||= (value.hash * -1)
@hash || @hash = (value.hash * -1)
end

##
# Returns object representation of this URI, broken into components
#
# @return [Hash{Symbol => String}]
def object
@object ||= parse(@value)
@object || @object = parse(@value)
end
alias_method :to_h, :object

Expand All @@ -830,8 +831,8 @@ def object
def parse(value)
value = value.to_s.dup.force_encoding(Encoding::ASCII_8BIT)
parts = {}
if matchdata = value.to_s.match(IRI_PARTS)
scheme, authority, path, query, fragment = matchdata.to_a[1..-1]
if matchdata = IRI_PARTS.match(value)
scheme, authority, path, query, fragment = matchdata[1..-1]
userinfo, hostport = authority.to_s.split('@', 2)
hostport, userinfo = userinfo, nil unless hostport
user, password = userinfo.to_s.split(':', 2)
Expand Down Expand Up @@ -928,11 +929,13 @@ def normalized_password
::URI.encode(::URI.decode(password), /[^#{IUNRESERVED}|#{SUB_DELIMS}]/) if password
end

HOST_FROM_AUTHORITY_RE = /(?:[^@]+@)?([^:]+)(?::.*)?$/.freeze

##
# @return [String]
def host
object.fetch(:host) do
@object[:host] = ($1 if @object[:authority].to_s.match(/(?:[^@]+@)?([^:]+)(?::.*)?$/))
@object[:host] = ($1 if HOST_FROM_AUTHORITY_RE.match(@object[:authority]))
end
end

Expand All @@ -954,11 +957,13 @@ def normalized_host
normalize_segment(host, IHOST, true).chomp('.') if host
end

PORT_FROM_AUTHORITY_RE = /:(\d+)$/.freeze

##
# @return [String]
def port
object.fetch(:port) do
@object[:port] = ($1 if @object[:authority].to_s.match(/:(\d+)$/))
@object[:port] = ($1 if PORT_FROM_AUTHORITY_RE.match(@object[:authority]))
end
end

Expand Down
9 changes: 7 additions & 2 deletions lib/rdf/util/logger.rb
Expand Up @@ -192,6 +192,11 @@ def log_depth(**options, &block)
end

private
LOGGER_COMMON_LEVELS = {
fatal: 4, error: 3, warn: 2, info: 1, debug: 0
}.freeze
LOGGER_COMMON_LEVELS_REVERSE = LOGGER_COMMON_LEVELS.invert.freeze

##
# Common method for logging messages
#
Expand All @@ -212,8 +217,8 @@ def logger_common(*args, level:, **options)
logger = self.logger(options)
logger.log_statistics[level] = logger.log_statistics[level].to_i + 1
# Some older code uses integer level numbers
level = [:debug, :info, :warn, :error, :fatal][level] if level.is_a?(Integer)
return if logger.level > {fatal: 4, error: 3, warn: 2, info: 1, debug: 0}[level]
level = LOGGER_COMMON_LEVELS_REVERSE.fetch(level) if level.is_a?(Integer)
return if logger.level > LOGGER_COMMON_LEVELS.fetch(level)

depth = options.fetch(:depth, logger.log_depth)
args << yield if block_given?
Expand Down
6 changes: 3 additions & 3 deletions lib/rdf/vocabulary.rb
Expand Up @@ -273,7 +273,7 @@ def imported_from
#
# @return [RDF::URI]
def to_uri
RDF::URI.intern(to_s)
RDF::URI.intern(@@uris[self].to_s)
end

# For IRI compatibility
Expand Down Expand Up @@ -448,7 +448,7 @@ def props; @properties ||= {}; end
undef_method(*instance_methods.
map(&:to_s).
select {|m| m =~ /^\w+$/}.
reject {|m| %w(object_id dup instance_eval inspect to_s class).include?(m) || m[0,2] == '__'}.
reject {|m| %w(object_id dup instance_eval inspect to_s class send public_send).include?(m) || m[0,2] == '__'}.
map(&:to_sym))

##
Expand Down Expand Up @@ -623,7 +623,7 @@ def dup
# @since 0.3.9
def valid?
# Validate relative to RFC3987
to_s.match(RDF::URI::IRI) || false
RDF::URI::IRI.match(to_s) || false
end

##
Expand Down
18 changes: 18 additions & 0 deletions spec/vocabulary_spec.rb
Expand Up @@ -27,6 +27,16 @@
expect(subject["foo"]).to be_a(RDF::Vocabulary::Term)
end

it "allows #send" do
expect {subject.send(:foo)}.not_to raise_error
expect(subject.send(:foo)).to be_a(RDF::Vocabulary::Term)
end

it "allows #public_send" do
expect {subject.public_send(:foo)}.not_to raise_error
expect(subject.public_send(:foo)).to be_a(RDF::Vocabulary::Term)
end

it "does not add to @@uris" do
RDF::Vocabulary.new("http://example/")
expect(RDF::Vocabulary.class_variable_get(:"@@uris")).to be_a(Hash)
Expand Down Expand Up @@ -347,6 +357,14 @@
end
end

context 'without a uri' do
let!(:vocab) { @vocab ||= RDF::Vocabulary.from_graph(graph) }

it "gives a null relative uri" do
expect(vocab.to_uri).to eq RDF::URI.new(nil)
end
end

context "with existing Vocabulary" do
let!(:nt) {%{
<http://example/Klass> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
Expand Down

0 comments on commit 9c9e551

Please sign in to comment.