rdf 2.1.1 → 2.2.0.pre.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,12 +11,11 @@ module RDF; class Literal
11
11
  FALSES = %w(false 0).freeze
12
12
 
13
13
  ##
14
- # @param [Boolean] value
15
- # @option options [String] :lexical (nil)
16
- def initialize(value, options = {})
17
- @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
18
- @string = options[:lexical] if options.has_key?(:lexical)
19
- @string ||= value if value.is_a?(String)
14
+ # @param [String, Boolean] value
15
+ # @param (see Literal#initialize)
16
+ def initialize(value, datatype: nil, lexical: nil, **options)
17
+ @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
18
+ @string = lexical || (value if value.is_a?(String))
20
19
  @object = case
21
20
  when true.equal?(value) then true
22
21
  when false.equal?(value) then false
@@ -10,12 +10,11 @@ module RDF; class Literal
10
10
  FORMAT = '%Y-%m-%d'.freeze
11
11
 
12
12
  ##
13
- # @param [Date] value
14
- # @option options [String] :lexical (nil)
15
- def initialize(value, options = {})
16
- @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
17
- @string = options[:lexical] if options.has_key?(:lexical)
18
- @string ||= value if value.is_a?(String)
13
+ # @param [String, Date, #to_date] value
14
+ # @param (see Literal#initialize)
15
+ def initialize(value, datatype: nil, lexical: nil, **options)
16
+ @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
17
+ @string = lexical || (value if value.is_a?(String))
19
18
  @object = case
20
19
  when value.is_a?(::Date) then value
21
20
  when value.respond_to?(:to_date) then value.to_date
@@ -12,10 +12,9 @@ module RDF; class Literal
12
12
  ##
13
13
  # @param [DateTime] value
14
14
  # @option options [String] :lexical (nil)
15
- def initialize(value, options = {})
16
- @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
17
- @string = options[:lexical] if options.has_key?(:lexical)
18
- @string ||= value if value.is_a?(String)
15
+ def initialize(value, datatype: nil, lexical: nil, **options)
16
+ @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
17
+ @string = lexical || (value if value.is_a?(String))
19
18
  @object = case
20
19
  when value.is_a?(::DateTime) then value
21
20
  when value.respond_to?(:to_datetime) then value.to_datetime
@@ -15,15 +15,19 @@ module RDF; class Literal
15
15
  GRAMMAR = /^[\+\-]?\d+(\.\d*)?$/.freeze
16
16
 
17
17
  ##
18
- # @param [BigDecimal] value
19
- # @option options [String] :lexical (nil)
20
- def initialize(value, options = {})
21
- @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
22
- @string = options[:lexical] if options.has_key?(:lexical)
23
- @string ||= value if value.is_a?(String)
18
+ # @param [String, BidDecimal, Numeric] value
19
+ # @param (see Literal#initialize)
20
+ def initialize(value, datatype: nil, lexical: nil, **options)
21
+ @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
22
+ @string = lexical || (value if value.is_a?(String))
24
23
  @object = case
25
- when value.is_a?(BigDecimal) then value
26
- else BigDecimal(value.to_s)
24
+ when value.is_a?(::BigDecimal) then value
25
+ when value.is_a?(::Float) then BigDecimal(value.to_s)
26
+ when value.is_a?(::Numeric) then BigDecimal(value)
27
+ else
28
+ value = value.to_s
29
+ value += "0" if value.end_with?(".") # Normalization required in Ruby 2.4
30
+ BigDecimal(value) rescue nil
27
31
  end
28
32
  end
29
33
 
@@ -15,12 +15,11 @@ module RDF; class Literal
15
15
  GRAMMAR = /^(?:NaN|(?:[\+\-]?(?:INF|(?:\d+(\.\d*)?(e[\+\-]?\d+)?))))$/i.freeze
16
16
 
17
17
  ##
18
- # @param [Float, #to_f] value
19
- # @option options [String] :lexical (nil)
20
- def initialize(value, options = {})
21
- @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
22
- @string = options[:lexical] if options.has_key?(:lexical)
23
- @string ||= value if value.is_a?(String)
18
+ # @param [String, Float, #to_f] value
19
+ # @param (see Literal#initialize)
20
+ def initialize(value, datatype: nil, lexical: nil, **options)
21
+ @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
22
+ @string = lexical || (value if value.is_a?(String))
24
23
  @object = case
25
24
  when value.is_a?(::String) then case value
26
25
  when '+INF' then 1/0.0
@@ -16,15 +16,14 @@ module RDF; class Literal
16
16
  GRAMMAR = /^[\+\-]?\d+$/.freeze
17
17
 
18
18
  ##
19
- # @param [Integer, #to_i] value
20
- # @option options [String] :lexical (nil)
21
- def initialize(value, options = {})
22
- @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
23
- @string = options[:lexical] if options.has_key?(:lexical)
24
- @string ||= value if value.is_a?(String)
19
+ # @param [String, Integer, #to_i] value
20
+ # @param (see Literal#initialize)
21
+ def initialize(value, datatype: nil, lexical: nil, **options)
22
+ @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
23
+ @string = lexical || (value if value.is_a?(String))
25
24
  @object = case
25
+ when value.is_a?(::Integer) then value
26
26
  when value.respond_to?(:to_i) then value.to_i
27
- when value.is_a?(::Integer) then value
28
27
  else Integer(value.to_s) rescue nil
29
28
  end
30
29
  end
@@ -15,12 +15,11 @@ module RDF; class Literal
15
15
  FORMAT = '%H:%M:%S%:z'.freeze
16
16
 
17
17
  ##
18
- # @param [Time] value
19
- # @option options [String] :lexical (nil)
20
- def initialize(value, options = {})
21
- @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
22
- @string = options[:lexical] if options.has_key?(:lexical)
23
- @string ||= value if value.is_a?(String)
18
+ # @param [String, DateTime, #to_datetime] value
19
+ # @param (see Literal#initialize)
20
+ def initialize(value, datatype: nil, lexical: nil, **options)
21
+ @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
22
+ @string = lexical || (value if value.is_a?(String))
24
23
  @object = case
25
24
  when value.is_a?(::DateTime) then value
26
25
  when value.respond_to?(:to_datetime) then value.to_datetime rescue ::DateTime.parse(value.to_s)
@@ -9,13 +9,12 @@ module RDF; class Literal
9
9
  GRAMMAR = /\A[^\x0D\x0A\x09]+\z/i.freeze # FIXME
10
10
 
11
11
  ##
12
- # @param [Symbol, #to_s] value
13
- # @option options [String] :lexical (nil)
14
- def initialize(value, options = {})
15
- @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
16
- @string = options[:lexical] if options.has_key?(:lexical)
17
- @string ||= value if value.is_a?(String)
18
- @object = value.is_a?(Symbol) ? value : value.to_s
12
+ # @param [String, Symbol, #to_sym] value
13
+ # @param (see Literal#initialize)
14
+ def initialize(value, datatype: nil, lexical: nil, **options)
15
+ @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
16
+ @string = lexical || (value if value.is_a?(String))
17
+ @object = value.is_a?(Symbol) ? value : value.to_sym
19
18
  end
20
19
 
21
20
  ##
@@ -114,7 +114,7 @@ module RDF
114
114
  ##
115
115
  # Returns a hash code for this blank node.
116
116
  #
117
- # @return [Fixnum]
117
+ # @return [Integer]
118
118
  def hash
119
119
  @id.hash
120
120
  end
@@ -31,11 +31,11 @@ module RDF
31
31
  ##
32
32
  # @private
33
33
  # @since 0.2.2
34
- def self.from(statement, options = {})
34
+ def self.from(statement, graph_name: nil, **options)
35
35
  case statement
36
36
  when Array, Query::Pattern
37
- graph_name = statement[3] == false ? nil : statement[3]
38
- self.new(statement[0], statement[1], statement[2], options.merge(graph_name: graph_name))
37
+ graph_name ||= statement[3] == false ? nil : statement[3]
38
+ self.new(statement[0], statement[1], statement[2], graph_name: graph_name, **options)
39
39
  when Statement then statement
40
40
  when Hash then self.new(options.merge(statement))
41
41
  else raise ArgumentError, "expected RDF::Statement, Hash, or Array, but got #{statement.inspect}"
@@ -58,7 +58,7 @@ module RDF
58
58
  attr_accessor :object
59
59
 
60
60
  ##
61
- # @overload initialize(options = {})
61
+ # @overload initialize(**options)
62
62
  # @param [Hash{Symbol => Object}] options
63
63
  # @option options [RDF::Term] :subject (nil)
64
64
  # A symbol is converted to an interned {Node}.
@@ -70,11 +70,11 @@ module RDF
70
70
  # @option options [Boolean] :inferred used as a marker to record that this statement was inferred based on semantic relationships (T-Box).
71
71
  # @return [RDF::Statement]
72
72
  #
73
- # @overload initialize(subject, predicate, object, options = {})
73
+ # @overload initialize(subject, predicate, object, **options)
74
74
  # @param [RDF::Term] subject
75
75
  # A symbol is converted to an interned {Node}.
76
- # @param [RDF::URI] predicate
77
- # @param [RDF::Resource] object
76
+ # @param [RDF::URI] predicate
77
+ # @param [RDF::Resource] object
78
78
  # if not a {Resource}, it is coerced to {Literal} or {Node} depending on if it is a symbol or something other than a {Term}.
79
79
  # @param [Hash{Symbol => Object}] options
80
80
  # @option options [RDF::Term] :graph_name (nil)
@@ -244,7 +244,7 @@ module RDF
244
244
  end
245
245
 
246
246
  ##
247
- # Generates a Fixnum hash value as a quad.
247
+ # Generates a Integer hash value as a quad.
248
248
  def hash
249
249
  @hash ||= to_quad.hash
250
250
  end
@@ -337,9 +337,14 @@ module RDF
337
337
  def to_triple
338
338
  [subject, predicate, object]
339
339
  end
340
+ alias_method :to_a, :to_triple
340
341
 
341
- alias_method :to_a, :to_triple
342
- alias_method :to_ary, :to_triple
342
+ ##
343
+ # @deprecated use {#to_a} or {#to_triple} instead
344
+ # @see #to_triple
345
+ def to_ary
346
+ to_triple
347
+ end
343
348
 
344
349
  ##
345
350
  # Canonicalizes each unfrozen term in the statement
@@ -375,7 +380,7 @@ module RDF
375
380
  # @param [Symbol] predicate_key
376
381
  # @param [Symbol] object_key
377
382
  # @return [Hash{Symbol => RDF::Term}]
378
- def to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object, graph_key = :graph_name)
383
+ def to_h(subject_key = :subject, predicate_key = :predicate, object_key = :object, graph_key = :graph_name)
379
384
  {subject_key => subject, predicate_key => predicate, object_key => object, graph_key => graph_name}
380
385
  end
381
386
 
@@ -392,17 +397,40 @@ module RDF
392
397
  ##
393
398
  # Returns a graph containing this statement in reified form.
394
399
  #
395
- # @param [Hash{Symbol => Object}] options
400
+ # @param [RDF::Term] subject (nil)
401
+ # Subject of reification.
402
+ # @param [RDF::Term] id (nil)
403
+ # Node identifier, when subject is anonymous
404
+ # @param [RDF::Term] graph_name (nil)
405
+ # Note, in RDF 1.1, a graph name MUST be an {Resource}.
396
406
  # @return [RDF::Graph]
397
407
  # @see http://www.w3.org/TR/rdf-primer/#reification
398
- def reified(options = {})
399
- RDF::Graph.new(graph_name: options[:graph_name]) do |graph|
400
- subject = options[:subject] || RDF::Node.new(options[:id])
408
+ def reified(subject: nil, id: nil, graph_name: nil)
409
+ RDF::Graph.new(graph_name: graph_name) do |graph|
410
+ subject = subject || RDF::Node.new(id)
401
411
  graph << [subject, RDF.type, RDF[:Statement]]
402
412
  graph << [subject, RDF.subject, self.subject]
403
413
  graph << [subject, RDF.predicate, self.predicate]
404
414
  graph << [subject, RDF.object, self.object]
405
415
  end
406
416
  end
417
+
418
+ protected
419
+ ##
420
+ # @overload #to_hash
421
+ # Returns the terms of this statement as a `Hash`.
422
+ #
423
+ # @param (see #to_h)
424
+ # @return (see #to_h)
425
+ # @deprecated Use {#to_h} instead.
426
+ def method_missing(meth, *args)
427
+ case meth
428
+ when :to_hash
429
+ warn "[DEPRECATION] Statement#to_hash is deprecated, use Statement#to_h instead. Called from #{Gem.location_of_caller.join(':')}"
430
+ self.to_h
431
+ else
432
+ super
433
+ end
434
+ end
407
435
  end
408
436
  end
data/lib/rdf/model/uri.rb CHANGED
@@ -143,6 +143,7 @@ module RDF
143
143
  # @return [RDF::URI] an immutable, frozen URI object
144
144
  def self.intern(*args)
145
145
  str = args.first
146
+ args << {} unless args.last.is_a?(Hash) # FIXME: needed until #to_hash is removed to avoid DEPRECATION warning.
146
147
  (cache[(str = str.to_s).to_sym] ||= self.new(*args)).freeze
147
148
  end
148
149
 
@@ -200,10 +201,10 @@ module RDF
200
201
  end
201
202
 
202
203
  ##
203
- # @overload URI(uri, options = {})
204
+ # @overload URI(uri, **options)
204
205
  # @param [URI, String, #to_s] uri
205
206
  #
206
- # @overload URI(options = {})
207
+ # @overload URI(**options)
207
208
  # @param [Hash{Symbol => Object}] options
208
209
  # @option [String, #to_s] :scheme The scheme component.
209
210
  # @option [String, #to_s] :user The user component.
@@ -281,7 +282,7 @@ module RDF
281
282
  #
282
283
  # @return [Boolean] `true` or `false`
283
284
  # @see http://en.wikipedia.org/wiki/URI_scheme
284
- # @see {NON_HIER_SCHEMES}
285
+ # @see NON_HIER_SCHEMES
285
286
  # @since 1.0.10
286
287
  def hier?
287
288
  !NON_HIER_SCHEMES.include?(scheme)
@@ -807,7 +808,7 @@ module RDF
807
808
  ##
808
809
  # Returns a hash code for this URI.
809
810
  #
810
- # @return [Fixnum]
811
+ # @return [Integer]
811
812
  def hash
812
813
  @hash ||= (value.hash * -1)
813
814
  end
@@ -819,7 +820,7 @@ module RDF
819
820
  def object
820
821
  @object ||= parse(@value)
821
822
  end
822
- alias_method :to_hash, :object
823
+ alias_method :to_h, :object
823
824
 
824
825
  ##{
825
826
  # Parse a URI into it's components
@@ -1287,6 +1288,23 @@ module RDF
1287
1288
  ""
1288
1289
  end
1289
1290
  end
1291
+
1292
+ protected
1293
+ ##
1294
+ # @overload #to_hash
1295
+ # Returns object representation of this URI, broken into components
1296
+ #
1297
+ # @return (see #object)
1298
+ # @deprecated Use {#to_h} instead.
1299
+ def method_missing(meth, *args)
1300
+ case meth
1301
+ when :to_hash
1302
+ warn "[DEPRECATION] URI#to_hash is deprecated, use URI#to_h instead. Called from #{Gem.location_of_caller.join(':')}"
1303
+ self.to_h
1304
+ else
1305
+ super
1306
+ end
1307
+ end
1290
1308
  end
1291
1309
 
1292
1310
  # RDF::IRI is a synonym for RDF::URI
data/lib/rdf/nquads.rb CHANGED
@@ -106,7 +106,7 @@ module RDF
106
106
  # @param [Hash{Symbol => Object}] options = ({})
107
107
  # @return [String]
108
108
  # @since 0.4.0
109
- def format_statement(statement, options = {})
109
+ def format_statement(statement, **options)
110
110
  format_quad(*statement.to_quad, options)
111
111
  end
112
112
 
@@ -119,7 +119,7 @@ module RDF
119
119
  # @param [RDF::Term] graph_name
120
120
  # @param [Hash{Symbol => Object}] options = ({})
121
121
  # @return [String]
122
- def format_quad(subject, predicate, object, graph_name, options = {})
122
+ def format_quad(subject, predicate, object, graph_name, **options)
123
123
  s = "%s %s %s " % [subject, predicate, object].map { |value| format_term(value, options) }
124
124
  s += format_term(graph_name, options) + " " if graph_name
125
125
  s + "."
@@ -91,8 +91,9 @@ module RDF::NTriples
91
91
  # @param [String] input
92
92
  # @param [{Symbol => Object}] options
93
93
  # From {RDF::Reader#initialize}
94
+ # @option options [RDF::Util::Logger] :logger ([])
94
95
  # @return [RDF::Term]
95
- def self.unserialize(input, options = {})
96
+ def self.unserialize(input, **options)
96
97
  case input
97
98
  when nil then nil
98
99
  else self.new(input, {logger: []}.merge(options)).read_value
@@ -102,27 +103,27 @@ module RDF::NTriples
102
103
  ##
103
104
  # (see unserialize)
104
105
  # @return [RDF::Resource]
105
- def self.parse_subject(input, options = {})
106
+ def self.parse_subject(input, **options)
106
107
  parse_uri(input, options) || parse_node(input, options)
107
108
  end
108
109
 
109
110
  ##
110
111
  # (see unserialize)
111
112
  # @return [RDF::URI]
112
- def self.parse_predicate(input, options = {})
113
+ def self.parse_predicate(input, *options)
113
114
  parse_uri(input, intern: true)
114
115
  end
115
116
 
116
117
  ##
117
118
  # (see unserialize)
118
- def self.parse_object(input, options = {})
119
+ def self.parse_object(input, **options)
119
120
  parse_uri(input, options) || parse_node(input, options) || parse_literal(input, options)
120
121
  end
121
122
 
122
123
  ##
123
124
  # (see unserialize)
124
125
  # @return [RDF::Node]
125
- def self.parse_node(input, options = {})
126
+ def self.parse_node(input, **options)
126
127
  if input =~ NODEID
127
128
  RDF::Node.new($1)
128
129
  end
@@ -130,18 +131,19 @@ module RDF::NTriples
130
131
 
131
132
  ##
132
133
  # (see unserialize)
134
+ # @param [Boolean] intern (false) Use Interned URI
133
135
  # @return [RDF::URI]
134
- def self.parse_uri(input, options = {})
136
+ def self.parse_uri(input, intern: false, **options)
135
137
  if input =~ URIREF
136
138
  uri_str = unescape($1)
137
- RDF::URI.send(options[:intern] ? :intern : :new, unescape($1))
139
+ RDF::URI.send(intern ? :intern : :new, unescape($1))
138
140
  end
139
141
  end
140
142
 
141
143
  ##
142
144
  # (see unserialize)
143
145
  # @return [RDF::Literal]
144
- def self.parse_literal(input, options = {})
146
+ def self.parse_literal(input, **options)
145
147
  case input
146
148
  when LITERAL_WITH_LANGUAGE
147
149
  RDF::Literal.new(unescape($1), language: $4)
@@ -218,12 +220,13 @@ module RDF::NTriples
218
220
  end
219
221
 
220
222
  ##
223
+ # @param [Boolean] intern (false) Use Interned Node
221
224
  # @return [RDF::URI]
222
225
  # @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar (uriref)
223
- def read_uriref(options = {})
226
+ def read_uriref(intern: false, **options)
224
227
  if uri_str = match(URIREF)
225
228
  uri_str = self.class.unescape(uri_str)
226
- uri = RDF::URI.send(intern? && options[:intern] ? :intern : :new, uri_str)
229
+ uri = RDF::URI.send(intern? && intern ? :intern : :new, uri_str)
227
230
  uri.validate! if validate?
228
231
  uri.canonicalize! if canonicalize?
229
232
  uri