Skip to content

Commit 431785c

Browse files
authored
Merge pull request #339 from ruby-rdf/feature/issue-338-keyword-arguments
Feature/issue 338 keyword arguments
2 parents 11389f2 + 9f69025 commit 431785c

37 files changed

Lines changed: 384 additions & 320 deletions

lib/rdf.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def self.List(*args)
156156
# @overload Statement()
157157
# @return [RDF::URI] returns the IRI for `rdf:Statement`
158158
#
159-
# @overload Statement(options = {})
159+
# @overload Statement(**options)
160160
# @param [Hash{Symbol => Object}] options
161161
# @option options [RDF::Resource] :subject (nil)
162162
# @option options [RDF::URI] :predicate (nil)
@@ -165,7 +165,7 @@ def self.List(*args)
165165
# Note, a graph_name MUST be an IRI or BNode.
166166
# @return [RDF::Statement]
167167
#
168-
# @overload Statement(subject, predicate, object, options = {})
168+
# @overload Statement(subject, predicate, object, **options)
169169
# @param [RDF::Resource] subject
170170
# @param [RDF::URI] predicate
171171
# @param [RDF::Term] object

lib/rdf/changeset.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Changeset
4141
# @yield [changes]
4242
# @yieldparam [RDF::Changeset] changes
4343
# @return [void]
44-
def self.apply(mutable, options = {}, &block)
44+
def self.apply(mutable, **options, &block)
4545
self.new(&block).apply(mutable, options)
4646
end
4747

@@ -106,7 +106,7 @@ def readable?
106106
# @param [RDF::Mutable] mutable
107107
# @param [Hash{Symbol => Object}] options
108108
# @return [void]
109-
def apply(mutable, options = {})
109+
def apply(mutable, **options)
110110
mutable.apply_changeset(self)
111111
end
112112

lib/rdf/cli.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,23 +338,24 @@ def self.usage(options, banner: nil)
338338
# Execute one or more commands, parsing input as necessary
339339
#
340340
# @param [Array<String>] args
341+
# @param [IO] output
342+
# @param [Hash{Symbol => Object}] options
341343
# @return [Boolean]
342-
def self.exec(args, options = {})
343-
out = options[:output] || $stdout
344-
out.set_encoding(Encoding::UTF_8) if out.respond_to?(:set_encoding) && RUBY_PLATFORM == "java"
344+
def self.exec(args, output: $stdout, option_parser: self.options, **options)
345+
output.set_encoding(Encoding::UTF_8) if output.respond_to?(:set_encoding) && RUBY_PLATFORM == "java"
345346
cmds, args = args.partition {|e| commands.include?(e.to_s)}
346347

347348
if cmds.empty?
348-
usage(options.fetch(:option_parser, self.options))
349+
usage(option_parser)
349350
abort "No command given"
350351
end
351352

352353
if cmds.first == 'help'
353354
on_cmd = cmds[1]
354355
if on_cmd && COMMANDS.fetch(on_cmd.to_sym, {})[:help]
355-
usage(options.fetch(:option_parser, self.options), banner: "Usage: #{self.basename.split('/').last} #{COMMANDS[on_cmd.to_sym][:help]}")
356+
usage(option_parser, banner: "Usage: #{self.basename.split('/').last} #{COMMANDS[on_cmd.to_sym][:help]}")
356357
else
357-
usage(options.fetch(:option_parser, self.options))
358+
usage(option_parser)
358359
end
359360
return
360361
end
@@ -374,7 +375,7 @@ def self.exec(args, options = {})
374375

375376
# Run each command in sequence
376377
cmds.each do |command|
377-
COMMANDS[command.to_sym][:lambda].call(args, options)
378+
COMMANDS[command.to_sym][:lambda].call(args, output: output, **options)
378379
end
379380
rescue ArgumentError => e
380381
abort e.message
@@ -409,7 +410,7 @@ def self.commands
409410
# @yieldparam [Array<String>] argv
410411
# @yieldparam [Hash] opts
411412
# @yieldreturn [void]
412-
def self.add_command(command, options = {}, &block)
413+
def self.add_command(command, **options, &block)
413414
options[:lambda] = block if block_given?
414415
COMMANDS[command.to_sym] ||= options
415416
end
@@ -431,15 +432,19 @@ def self.formats(reader: false, writer: false)
431432
# yielding a reader
432433
#
433434
# @param [Array<String>] files
435+
# @param [String] evaluate from command-line, rather than referenced file
436+
# @param [Symbol] format (:ntriples) Reader symbol for finding reader
437+
# @param [Encoding] encoding set on the input
438+
# @param [Hash{Symbol => Object}] options sent to reader
434439
# @yield [reader]
435440
# @yieldparam [RDF::Reader]
436441
# @return [nil]
437-
def self.parse(files, options = {}, &block)
442+
def self.parse(files, evaluate: nil, format: :ntriples, encoding: Encoding::UTF_8, **options, &block)
438443
if files.empty?
439444
# If files are empty, either use options[:execute]
440-
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : $stdin
441-
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
442-
r = RDF::Reader.for(options[:format] || :ntriples)
445+
input = evaluate ? StringIO.new(evaluate) : $stdin
446+
input.set_encoding(encoding)
447+
r = RDF::Reader.for(format)
443448
(@readers ||= []) << r
444449
r.new(input, options) do |reader|
445450
yield(reader)

lib/rdf/format.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def self.each(&block)
7070
# @param [String, RDF::URI] filename
7171
# @return [Class]
7272
#
73-
# @overload for(options = {})
73+
# @overload for(**options)
7474
# Finds an RDF serialization format class based on various options.
7575
#
7676
# @param [Hash{Symbol => Object}] options

lib/rdf/mixin/enumerable.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def validate!
121121
# @return [Array<RDF::Statement>]
122122
# @see #each_statement
123123
# @see #enum_statement
124-
def statements(options = {})
124+
def statements(**options)
125125
Array(enum_statement)
126126
end
127127

@@ -184,7 +184,7 @@ def enum_statement
184184
# @return [Array<Array(RDF::Resource, RDF::URI, RDF::Term)>]
185185
# @see #each_triple
186186
# @see #enum_triple
187-
def triples(options = {})
187+
def triples(**options)
188188
enum_statement.map(&:to_triple) # TODO: optimize
189189
end
190190

@@ -245,7 +245,7 @@ def enum_triple
245245
# @return [Array<Array(RDF::Resource, RDF::URI, RDF::Term, RDF::Resource)>]
246246
# @see #each_quad
247247
# @see #enum_quad
248-
def quads(options = {})
248+
def quads(**options)
249249
enum_statement.map(&:to_quad) # TODO: optimize
250250
end
251251

@@ -738,11 +738,10 @@ def to_h
738738
# @see RDF::Writer.dump
739739
# @raise [RDF::WriterError] if no writer found
740740
# @since 0.2.0
741-
def dump(*args)
742-
options = args.last.is_a?(Hash) ? args.pop : {}
741+
def dump(*args, **options)
743742
writer = RDF::Writer.for(*args)
744743
raise RDF::WriterError, "No writer found using #{args.inspect}" unless writer
745-
writer.dump(self, nil, options)
744+
writer.dump(self, nil, **options)
746745
end
747746

748747
protected

lib/rdf/mixin/queryable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module Queryable
4141
# Returns an enumerable of statements (may be an enumerator) or query solutions, if passed an {RDF::Query}
4242
# @see RDF::Queryable#query_pattern
4343
# @note Since 2.0, this may return an Enumerable or an Enumerator in addition to Solutions
44-
def query(pattern, options = {}, &block)
44+
def query(pattern, **options, &block)
4545
raise TypeError, "#{self} is not readable" if respond_to?(:readable?) && !readable?
4646

4747
case pattern
@@ -111,7 +111,7 @@ def query(pattern, options = {}, &block)
111111
# @see RDF::Queryable#query
112112
# @see RDF::Query#execute
113113
# @since 0.3.0
114-
def query_execute(query, options = {}, &block)
114+
def query_execute(query, **options, &block)
115115
# By default, we let RDF.rb's built-in `RDF::Query#execute` handle BGP
116116
# query execution by breaking down the query into its constituent
117117
# triple patterns and invoking `RDF::Query::Pattern#execute` on each
@@ -139,7 +139,7 @@ def query_execute(query, options = {}, &block)
139139
# @see RDF::Queryable#query
140140
# @see RDF::Query::Pattern#execute
141141
# @since 0.2.0
142-
def query_pattern(pattern, options = {}, &block)
142+
def query_pattern(pattern, **options, &block)
143143
# By default, we let Ruby's built-in `Enumerable#grep` handle the
144144
# matching of statements by iterating over all statements and calling
145145
# `RDF::Query::Pattern#===` on each statement.

lib/rdf/model/dataset.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def supports?(feature)
119119
##
120120
# Implements basic query pattern matching over the Dataset, with handling
121121
# for a default graph.
122-
def query_pattern(pattern, options = {}, &block)
122+
def query_pattern(pattern, **options, &block)
123123
return super unless pattern.graph_name == DEFAULT_GRAPH
124124

125125
if block_given?

lib/rdf/model/graph.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ def self.load(url, graph_name: nil, **options, &block)
8787
end
8888

8989
##
90-
# @param [RDF::Resource] graph_name
90+
# @param [RDF::Resource] graph_name
9191
# The graph_name from the associated {RDF::Queryable} associated
9292
# with this graph as provided with the `:data` option
9393
# (only for {RDF::Queryable} instances supporting
9494
# named graphs).
95-
# @param [RDF::Queryable] :data (RDF::Repository.new)
95+
# @param [RDF::Queryable] data (RDF::Repository.new)
9696
# Storage behind this graph.
9797
#
9898
# @raise [ArgumentError] if a `data` does not support named graphs.
@@ -273,7 +273,7 @@ def ==(other)
273273
##
274274
# @private
275275
# @see RDF::Queryable#query_pattern
276-
def query_pattern(pattern, options = {}, &block)
276+
def query_pattern(pattern, **options, &block)
277277
pattern = pattern.dup
278278
pattern.graph_name = graph_name || false
279279
@data.query(pattern, &block)
@@ -294,7 +294,7 @@ def insert_statement(statement)
294294
def insert_statements(statements)
295295
enum = Enumerable::Enumerator.new do |yielder|
296296

297-
statements.send(method = statements.respond_to?(:each_statement) ? :each_statement : :each) do |s|
297+
statements.send(statements.respond_to?(:each_statement) ? :each_statement : :each) do |s|
298298
s = s.dup
299299
s.graph_name = graph_name
300300
yielder << s

lib/rdf/model/literal.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ def self.datatyped_class(uri)
104104

105105
##
106106
# @private
107-
def self.new(value, options = {})
108-
raise ArgumentError, "datatype with language must be rdf:langString" if options[:language] && (options[:datatype] || RDF.langString).to_s != RDF.langString.to_s
107+
def self.new(value, language: nil, datatype: nil, lexical: nil, validate: false, canonicalize: false, **options)
108+
raise ArgumentError, "datatype with language must be rdf:langString" if language && (datatype || RDF.langString).to_s != RDF.langString.to_s
109109

110110
klass = case
111111
when !self.equal?(RDF::Literal)
112112
self # subclasses can be directly constructed without type dispatch
113-
when typed_literal = datatyped_class(options[:datatype].to_s)
113+
when typed_literal = datatyped_class(datatype.to_s)
114114
typed_literal
115115
else case value
116116
when ::TrueClass then RDF::Literal::Boolean
@@ -126,9 +126,9 @@ def self.new(value, options = {})
126126
end
127127
end
128128
literal = klass.allocate
129-
literal.send(:initialize, value, options)
130-
literal.validate! if options[:validate]
131-
literal.canonicalize! if options[:canonicalize]
129+
literal.send(:initialize, value, language: language, datatype: datatype, **options)
130+
literal.validate! if validate
131+
literal.canonicalize! if canonicalize
132132
literal
133133
end
134134

@@ -147,28 +147,28 @@ def self.new(value, options = {})
147147
# depending on if there is language
148148
#
149149
# @param [Object] value
150-
# @option options [Symbol] :language (nil)
150+
# @param [Symbol] language (nil)
151151
# Language is downcased to ensure proper matching
152-
# @option options [String] :lexical (nil)
152+
# @param [String] lexical (nil)
153153
# Supplied lexical representation of this literal,
154-
# otherwise it comes from transforming `value` to a string form
155-
# See {#to_s}.
156-
# @option options [URI] :datatype (nil)
157-
# @option options [Boolean] :validate (false)
158-
# @option options [Boolean] :canonicalize (false)
154+
# otherwise it comes from transforming `value` to a string form..
155+
# @param [URI] datatype (nil)
156+
# @param [Boolean] validate (false)
157+
# @param [Boolean] canonicalize (false)
159158
# @raise [ArgumentError]
160159
# if there is a language and datatype is no rdf:langString
161160
# or datatype is rdf:langString and there is no language
162161
# @see http://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal
163162
# @see http://www.w3.org/TR/rdf11-concepts/#section-Datatypes
164-
def initialize(value, options = {})
163+
# @see #to_s
164+
def initialize(value, language: nil, datatype: nil, lexical: nil, validate: false, canonicalize: false, **options)
165165
@object = value.freeze
166-
@string = options[:lexical] if options[:lexical]
166+
@string = lexical if lexical
167167
@string = value if !defined?(@string) && value.is_a?(String)
168168
@string = @string.encode(Encoding::UTF_8).freeze if @string
169169
@object = @string if @string && @object.is_a?(String)
170-
@language = options[:language].to_s.downcase.to_sym if options[:language]
171-
@datatype = RDF::URI(options[:datatype]).freeze if options[:datatype]
170+
@language = language.to_s.downcase.to_sym if language
171+
@datatype = RDF::URI(datatype).freeze if datatype
172172
@datatype ||= self.class.const_get(:DATATYPE) if self.class.const_defined?(:DATATYPE)
173173
@datatype ||= @language ? RDF.langString : RDF::XSD.string
174174
raise ArgumentError, "datatype of rdf:langString requires a language" if !@language && @datatype == RDF::langString
@@ -441,7 +441,7 @@ def squish!
441441
#
442442
# @param [String] string
443443
# @return [String]
444-
# @see {RDF::Term#escape}
444+
# @see RDF::Term#escape
445445
def escape(string)
446446
string.gsub('\\', '\\\\').
447447
gsub("\t", '\\t').

lib/rdf/model/literal/boolean.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ class Boolean < Literal
1111
FALSES = %w(false 0).freeze
1212

1313
##
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))
2019
@object = case
2120
when true.equal?(value) then true
2221
when false.equal?(value) then false

0 commit comments

Comments
 (0)