Skip to content

Commit a35d255

Browse files
committed
Finish 3.0.3
2 parents ff729b0 + 03b6e2e commit a35d255

File tree

14 files changed

+73
-27
lines changed

14 files changed

+73
-27
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
language: ruby
22
bundler_args: --without debug
33
script: "bundle exec rspec spec"
4-
before_install: "gem update --system"
4+
before_install:
5+
- "gem update --system"
6+
- "gem install bundler"
57
env:
68
- CI=true
79
rvm:

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.2
1+
3.0.3

lib/rdf/cli.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class Option
9292
# @return [String]
9393
attr_reader :description
9494

95+
# Default value for this option
96+
# @return [Object]
97+
attr_reader :default
98+
9599
# Potential values (for select or radio) or Ruby datatype
96100
# @return [Class, Array<String>]
97101
attr_reader :datatype
@@ -110,6 +114,7 @@ class Option
110114
# @param [Symbol] symbol
111115
# @param [Array<String>] on
112116
# @param [String] datatype
117+
# @param [Object] default
113118
# @param [String] control
114119
# @param [String] description
115120
# @param [[:optional, :disabled, :removed, :required]] use
@@ -118,10 +123,10 @@ class Option
118123
# @yieldparam [OptionParser] options (nil) optional OptionParser
119124
# @yieldreturn [Object] a possibly modified input value
120125
def initialize(symbol: nil, on: nil, datatype: nil, control: nil,
121-
description: nil, use: :optional, **options, &block)
126+
description: nil, use: :optional, default: nil, **options, &block)
122127
raise ArgumentError, "symbol is a required argument" unless symbol
123128
raise ArgumentError, "on is a required argument" unless on
124-
@symbol, @on, @datatype, @control, @description, @use, @callback = symbol.to_sym, Array(on), datatype, control, description, use, block
129+
@symbol, @on, @datatype, @control, @description, @use, @default, @callback = symbol.to_sym, Array(on), datatype, control, description, use, default, block
125130
end
126131

127132
def call(arg, options = {})
@@ -142,6 +147,7 @@ def to_hash
142147
{
143148
symbol: symbol,
144149
datatype: (datatype.is_a?(Class) ? datatype.name : datatype),
150+
default: default,
145151
control: control,
146152
description: description,
147153
use: use

lib/rdf/model/literal.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ def initialize(value, language: nil, datatype: nil, lexical: nil, validate: fals
170170
@language = language.to_s.downcase.to_sym if language
171171
@datatype = RDF::URI(datatype).freeze if datatype
172172
@datatype ||= self.class.const_get(:DATATYPE) if self.class.const_defined?(:DATATYPE)
173-
@datatype ||= @language ? RDF.langString : RDF::XSD.string
174-
raise ArgumentError, "datatype of rdf:langString requires a language" if !@language && @datatype == RDF::langString
173+
@datatype ||= @language ? RDF.langString : RDF::URI("http://www.w3.org/2001/XMLSchema#string")
175174
end
176175

177176
##
@@ -226,8 +225,8 @@ def compatible?(other)
226225
# * The arguments are plain literals with identical language tags
227226
# * The first argument is a plain literal with language tag and the second argument is a simple literal or literal typed as xsd:string
228227
has_language? ?
229-
(language == other.language || other.datatype == RDF::XSD.string) :
230-
other.datatype == RDF::XSD.string
228+
(language == other.language || other.datatype == RDF::URI("http://www.w3.org/2001/XMLSchema#string")) :
229+
other.datatype == RDF::URI("http://www.w3.org/2001/XMLSchema#string")
231230
end
232231

233232
##
@@ -317,7 +316,7 @@ def ==(other)
317316
# @return [Boolean] `true` or `false`
318317
# @see http://www.w3.org/TR/rdf-concepts/#dfn-plain-literal
319318
def plain?
320-
[RDF.langString, RDF::XSD.string].include?(datatype)
319+
[RDF.langString, RDF::URI("http://www.w3.org/2001/XMLSchema#string")].include?(datatype)
321320
end
322321

323322
##
@@ -327,7 +326,7 @@ def plain?
327326
# @return [Boolean] `true` or `false`
328327
# @see http://www.w3.org/TR/sparql11-query/#simple_literal
329328
def simple?
330-
datatype == RDF::XSD.string
329+
datatype == RDF::URI("http://www.w3.org/2001/XMLSchema#string")
331330
end
332331

333332
##
@@ -361,6 +360,8 @@ def has_datatype?
361360
# @return [Boolean] `true` or `false`
362361
# @since 0.2.1
363362
def valid?
363+
return false if language? && language.to_s !~ /^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/
364+
return false if datatype? && datatype.invalid?
364365
grammar = self.class.const_get(:GRAMMAR) rescue nil
365366
grammar.nil? || !!(value =~ grammar)
366367
end
@@ -487,15 +488,15 @@ def inspect
487488
def method_missing(name, *args)
488489
case name
489490
when :to_str
490-
return to_s if @datatype == RDF.langString || @datatype == RDF::XSD.string
491+
return to_s if @datatype == RDF.langString || @datatype == RDF::URI("http://www.w3.org/2001/XMLSchema#string")
491492
end
492493
super
493494
end
494495

495496
def respond_to_missing?(name, include_private = false)
496497
case name
497498
when :to_str
498-
return true if @datatype == RDF.langString || @datatype == RDF::XSD.string
499+
return true if @datatype == RDF.langString || @datatype == RDF::URI("http://www.w3.org/2001/XMLSchema#string")
499500
end
500501
super
501502
end

lib/rdf/model/literal/boolean.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module RDF; class Literal
55
# @see http://www.w3.org/TR/xmlschema11-2/#boolean
66
# @since 0.2.1
77
class Boolean < Literal
8-
DATATYPE = RDF::XSD.boolean
8+
DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#boolean")
99
GRAMMAR = /^(true|false|1|0)$/.freeze
1010
TRUES = %w(true 1).freeze
1111
FALSES = %w(false 0).freeze

lib/rdf/model/literal/date.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module RDF; class Literal
55
# @see http://www.w3.org/TR/xmlschema11-2/#date
66
# @since 0.2.1
77
class Date < Literal
8-
DATATYPE = RDF::XSD.date
8+
DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#date")
99
GRAMMAR = %r(\A(-?\d{4}-\d{2}-\d{2})((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z).freeze
1010
FORMAT = '%Y-%m-%d'.freeze
1111

lib/rdf/model/literal/datetime.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module RDF; class Literal
55
# @see http://www.w3.org/TR/xmlschema11-2/#dateTime#boolean
66
# @since 0.2.1
77
class DateTime < Literal
8-
DATATYPE = RDF::XSD.dateTime
8+
DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#dateTime")
99
GRAMMAR = %r(\A(-?\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?)((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z).freeze
1010
FORMAT = '%Y-%m-%dT%H:%M:%S%:z'.freeze
1111

@@ -57,14 +57,14 @@ def tz
5757
# @return [RDF::Literal]
5858
def timezone
5959
if tz == 'Z'
60-
RDF::Literal("PT0S", datatype: RDF::XSD.dayTimeDuration)
60+
RDF::Literal("PT0S", datatype: RDF::URI("http://www.w3.org/2001/XMLSchema#dayTimeDuration"))
6161
elsif md = tz.to_s.match(/^([+-])?(\d+):(\d+)?$/)
6262
plus_minus, hour, min = md[1,3]
6363
plus_minus = nil unless plus_minus == "-"
6464
hour = hour.to_i
6565
min = min.to_i
6666
res = "#{plus_minus}PT#{hour}H#{"#{min}M" if min > 0}"
67-
RDF::Literal(res, datatype: RDF::XSD.dayTimeDuration)
67+
RDF::Literal(res, datatype: RDF::URI("http://www.w3.org/2001/XMLSchema#dayTimeDuration"))
6868
end
6969
end
7070

lib/rdf/model/literal/decimal.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module RDF; class Literal
1111
# @see http://www.w3.org/TR/xmlschema11-2/#decimal
1212
# @since 0.2.1
1313
class Decimal < Numeric
14-
DATATYPE = RDF::XSD.decimal
14+
DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#decimal")
1515
GRAMMAR = /^[\+\-]?\d+(\.\d*)?$/.freeze
1616

1717
##

lib/rdf/model/literal/double.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module RDF; class Literal
1111
# @see http://www.w3.org/TR/xmlschema11-2/#double
1212
# @since 0.2.1
1313
class Double < Numeric
14-
DATATYPE = RDF::XSD.double
14+
DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#double")
1515
GRAMMAR = /^(?:NaN|\-?INF|[+\-]?(?:\d+(:?\.\d*)?|\.\d+)(?:[eE][\+\-]?\d+)?)$/.freeze
1616

1717
##

lib/rdf/model/literal/integer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module RDF; class Literal
1212
# @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#integer
1313
# @since 0.2.1
1414
class Integer < Decimal
15-
DATATYPE = RDF::XSD.integer
15+
DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#integer")
1616
GRAMMAR = /^[\+\-]?\d+$/.freeze
1717

1818
##

lib/rdf/model/literal/time.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module RDF; class Literal
1010
# @see http://www.w3.org/TR/xmlschema11-2/#time
1111
# @since 0.2.1
1212
class Time < Literal
13-
DATATYPE = RDF::XSD.time
13+
DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#time")
1414
GRAMMAR = %r(\A(\d{2}:\d{2}:\d{2}(?:\.\d+)?)((?:[\+\-]\d{2}:\d{2})|UTC|GMT|Z)?\Z).freeze
1515
FORMAT = '%H:%M:%S%:z'.freeze
1616

lib/rdf/model/literal/token.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module RDF; class Literal
55
# @see http://www.w3.org/TR/xmlschema11-2/#token
66
# @since 0.2.3
77
class Token < Literal
8-
DATATYPE = RDF::XSD.token
8+
DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#token")
99
GRAMMAR = /\A[^\x0D\x0A\x09]+\z/i.freeze # FIXME
1010

1111
##

rdf.gemspec

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ Gem::Specification.new do |gem|
1818
gem.files = %w(AUTHORS CREDITS README.md UNLICENSE VERSION bin/rdf etc/doap.nt) + Dir.glob('lib/**/*.rb')
1919
gem.bindir = %q(bin)
2020
gem.executables = %w(rdf)
21-
gem.default_executable = gem.executables.first
2221
gem.require_paths = %w(lib)
23-
gem.extensions = %w()
24-
gem.test_files = %w()
25-
gem.has_rdoc = false
2622

2723
gem.required_ruby_version = '>= 2.2.2'
2824
gem.requirements = []

spec/model_literal_spec.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def self.literal(selector)
1212
when :plain then ['Hello'.freeze]
1313
when :empty_lang then [''.freeze, {language: :en}]
1414
when :plain_lang then ['Hello'.freeze, {language: :en}]
15+
# langString language: must not contain spaces
16+
when :wrong_lang then ['WrongLang'.freeze, {language: "en f"}]
17+
# langString language: must be non-empty valid language
18+
when :unset_lang then ['NoLanguage'.freeze, {datatype: RDF::langString}]
1519
when :string then ['String.freeze', {datatype: RDF::XSD.string}]
1620
when :false then [false]
1721
when :true then [true]
@@ -33,6 +37,7 @@ def self.literals(*selector)
3337
when :all_simple then [:empty, :plain, :string].map {|s| literal(s)}
3438
when :all_plain_lang then [:empty_lang, :plain_lang].map {|s| literal(s)}
3539
when :all_native then [:false, :true, :int, :long, :double, :time, :date, :datetime].map {|s| literal(s)}
40+
when :all_invalid_lang then [:wrong_lang, :unset_lang].map {|s| literal(s)}
3641
when :all_plain then literals(:all_simple, :all_plain_lang)
3742
else literals(:all_plain, :all_native)
3843
end
@@ -340,6 +345,43 @@ def self.literals(*selector)
340345
end
341346
end
342347

348+
describe "language-tagged string" do
349+
literals(:all_plain_lang).each do |args|
350+
it "validates #{args.inspect}" do
351+
expect(RDF::Literal.new(*args)).to be_valid
352+
end
353+
end
354+
355+
literals(:all_invalid_lang).each do |args|
356+
it "invalidates #{args.inspect}" do
357+
expect(RDF::Literal.new(*args)).not_to be_valid
358+
end
359+
end
360+
361+
# test to make sure extra validation is not needed
362+
context "when language? && !@language" do
363+
langString = RDF::Literal.new("hello", datatype: RDF::langString)
364+
it "should be invalid" do
365+
expect(langString.language?).to be true
366+
expect(!langString.instance_variable_get("@language")).to be true
367+
expect(langString).not_to be_valid
368+
end
369+
end
370+
end
371+
372+
describe "datatyped literal" do
373+
(literals(:all) - literals(:all_simple, :all_plain_lang) +
374+
[["foo", datatype: RDF::URI("http://example/bar")]]).each do |args|
375+
it "validates #{args.inspect}" do
376+
expect(RDF::Literal.new(*args)).to be_valid
377+
end
378+
end
379+
380+
it "invalidates ['foo', datatype: 'bar']" do
381+
expect(RDF::Literal.new("foo", datatype: "bar")).not_to be_valid
382+
end
383+
end
384+
343385
describe RDF::Literal::Boolean do
344386
it_behaves_like 'RDF::Literal with datatype and grammar', "true", RDF::XSD.boolean
345387
it_behaves_like 'RDF::Literal equality', "true", true
@@ -1256,7 +1298,6 @@ def self.literals(*selector)
12561298
{
12571299
"language with xsd:string" => {value: "foo", language: "en", datatype: RDF::XSD.string},
12581300
"language with xsd:date" => {value: "foo", language: "en", datatype: RDF::XSD.date},
1259-
"no language with rdf:langString" => {value: "foo", datatype: RDF::langString},
12601301
}.each do |name, opts|
12611302
it "raises error for #{name}" do
12621303
expect {RDF::Literal.new(opts.delete(:value), opts)}.to raise_error(ArgumentError)

0 commit comments

Comments
 (0)