Skip to content

Commit 15f9fb9

Browse files
committed
More use of keyword arguments.
1 parent 3ec34b8 commit 15f9fb9

File tree

6 files changed

+78
-78
lines changed

6 files changed

+78
-78
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ env:
66
rvm:
77
- 2.2.5
88
- 2.3.1
9-
- jruby-9.0.5.0
9+
- jruby-9.1.2.0
1010
- rbx
1111
cache: bundler
1212
sudo: false

Gemfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ group :debug do
2828
end
2929

3030
platforms :rbx do
31-
gem 'rubysl', '~> 2.0'
31+
gem 'rubysl', '~> 2.0'
3232
gem 'rubinius', '~> 2.0'
3333
end
34+
35+
platforms :jruby do
36+
gem 'gson', '~> 0.6'
37+
end

lib/json/ld/api.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ class API
7070
# If set to a value that is not `false`, the JSON-LD processor must modify the output of the Compaction Algorithm or the Expansion Algorithm by coalescing all properties associated with each subject via the Flattening Algorithm. The value of `flatten must` be either an _IRI_ value representing the name of the graph to flatten, or `true`. If the value is `true`, then the first graph encountered in the input document is selected and flattened.
7171
# @option options [String] :processingMode ("json-ld-1.0")
7272
# If set to "json-ld-1.0", the JSON-LD processor must produce exactly the same results as the algorithms defined in this specification. If set to another value, the JSON-LD processor is allowed to extend or modify the algorithms defined in this specification to enable application-specific optimizations. The definition of such optimizations is beyond the scope of this specification and thus not defined. Consequently, different implementations may implement different optimizations. Developers must not define modes beginning with json-ld as they are reserved for future versions of this specification.
73-
# @option options [String] :produceGeneralizedRdf (false)
74-
# Unless the produce generalized RDF flag is set to true, RDF triples containing a blank node predicate are excluded from output.
75-
# @option options [Boolean] :useNativeTypes (false)
76-
# If set to `true`, the JSON-LD processor will use native datatypes for expression xsd:integer, xsd:boolean, and xsd:double values, otherwise, it will use the expanded form.
77-
# @option options [Boolean] :useRdfType (false)
78-
# If set to `true`, the JSON-LD processor will treat `rdf:type` like a normal property instead of using `@type`.
7973
# @option options [Boolean] :rename_bnodes (true)
8074
# Rename bnodes as part of expansion, or keep them the same.
8175
# @option options [Boolean] :unique_bnodes (false)
@@ -141,6 +135,9 @@ def initialize(input, context, options = {}, &block)
141135
end
142136
end
143137
end
138+
139+
# This is used internally only
140+
private :initialize
144141

145142
##
146143
# Expands the given input according to the steps in the Expansion Algorithm. The input must be copied, expanded and returned
@@ -152,8 +149,8 @@ def initialize(input, context, options = {}, &block)
152149
# The JSON-LD object to copy and perform the expansion upon.
153150
# @param [Hash{Symbol => Object}] options
154151
# See options in {JSON::LD::API#initialize}
155-
# @option options [Boolean] :framing Internal use for framing
156-
# @option options [Boolean] :keep_free_floating_nodes Internal use for framing
152+
# @option options [String, #read, Hash, Array, JSON::LD::Context] :expandContext
153+
# A context that is used to initialize the active context when expanding a document.
157154
# @raise [JsonLdError]
158155
# @yield jsonld
159156
# @yieldparam [Array<Hash>] jsonld
@@ -209,9 +206,9 @@ def self.compact(input, context, options = {})
209206

210207
# 1) Perform the Expansion Algorithm on the JSON-LD input.
211208
# This removes any existing context to allow the given context to be cleanly applied.
212-
expanded = options[:expanded] ? input : API.expand(input, options)
209+
expanded_input = options[:expanded] ? input : API.expand(input, options)
213210

214-
API.new(expanded, context, options) do
211+
API.new(expanded_input, context, options) do
215212
log_debug(".compact") {"expanded input: #{expanded.to_json(JSON_STATE) rescue 'malformed json'}"}
216213
result = compact(value, nil)
217214

@@ -299,7 +296,7 @@ def self.flatten(input, context, options = {})
299296
# @param [Hash{Symbol => Object}] options
300297
# See options in {JSON::LD::API#initialize}
301298
# Other options passed to {JSON::LD::API.expand}
302-
# @option options ['@last', '@always', '@never', '@link'] :embed ('@link')
299+
# @option options ['@last', '@always', '@never', '@link'] :embed ('@last')
303300
# a flag specifying that objects should be directly embedded in the output,
304301
# instead of being referred to by their IRI.
305302
# @option options [Boolean] :explicit (false)
@@ -361,7 +358,7 @@ def self.frame(input, frame, options = {})
361358

362359
# Get framing nodes from expanded input, replacing Blank Node identifiers as necessary
363360
old_logger, @options[:logger] = @options[:logger], []
364-
create_node_map(value, framing_state[:graphs], '@merged')
361+
create_node_map(value, framing_state[:graphs], graph: '@merged')
365362
@options[:logger] = old_logger
366363
framing_state[:subjects] = framing_state[:graphs]['@merged']
367364
log_debug(".frame") {"subjects: #{framing_state[:subjects].to_json(JSON_STATE) rescue 'malformed json'}"}
@@ -454,18 +451,20 @@ def self.toRdf(input, options = {}, &block)
454451
# @param [Array<RDF::Statement>] input
455452
# @param [Hash{Symbol => Object}] options
456453
# See options in {JSON::LD::API#initialize}
454+
# @option options [Boolean] :useRdfType (false)
455+
# If set to `true`, the JSON-LD processor will treat `rdf:type` like a normal property instead of using `@type`.
457456
# @yield jsonld
458457
# @yieldparam [Hash] jsonld
459458
# The JSON-LD document in expanded form
460459
# @yieldreturn [Object] returned object
461460
# @return [Object, Hash]
462461
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
463462
def self.fromRdf(input, options = {}, &block)
464-
options = {useNativeTypes: false}.merge!(options)
463+
useRdfType = options.fetch(:useRdfType, false)
465464
result = nil
466465

467466
API.new(nil, nil, options) do |api|
468-
result = api.from_statements(input)
467+
result = api.from_statements(input, useRdfType: useRdfType)
469468
end
470469

471470
block_given? ? yield(result) : result

lib/json/ld/context.rb

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -737,40 +737,41 @@ def reverse_term(term)
737737
# @param [String] value
738738
# A keyword, term, prefix:suffix or possibly relative IRI
739739
# @param [Hash{Symbol => Object}] options
740-
# @option options [Boolean] documentRelative (false)
741-
# @option options [Boolean] vocab (false)
742-
# @option options [Hash] local_context
740+
# @param [Boolean] documentRelative (false)
741+
# @param [Boolean] vocab (false)
742+
# @param [RDF::URI] base
743+
# @param [Hash] local_context
743744
# Used during Context Processing.
744-
# @option options [Hash] defined
745+
# @param [Hash] defined
745746
# Used during Context Processing.
747+
# @param [Boolean] quiet (false)
748+
# @param [Hash] options ({})
746749
# @return [RDF::URI, String]
747750
# IRI or String, if it's a keyword
748751
# @raise [JSON::LD::JsonLdError::InvalidIRIMapping] if the value cannot be expanded
749752
# @see http://json-ld.org/spec/latest/json-ld-api/#iri-expansion
750-
def expand_iri(value, options = {})
753+
def expand_iri(value, documentRelative: false, vocab: false, local_context: nil, defined: {}, quiet: false, **options)
751754
return value unless value.is_a?(String)
752755

753756
return value if KEYWORDS.include?(value)
754757
log_depth(options) do
755-
log_debug("expand_iri") {"value: #{value.inspect}"} unless options[:quiet]
756-
local_context = options[:local_context]
757-
defined = options.fetch(:defined, {})
758+
log_debug("expand_iri") {"value: #{value.inspect}"} unless quiet
758759

759760
# If local context is not null, it contains a key that equals value, and the value associated with the key that equals value in defined is not true, then invoke the Create Term Definition subalgorithm, passing active context, local context, value as term, and defined. This will ensure that a term definition is created for value in active context during Context Processing.
760761
if local_context && local_context.has_key?(value) && !defined[value]
761762
log_depth {create_term_definition(local_context, value, defined)}
762763
end
763764

764765
# If vocab is true and the active context has a term definition for value, return the associated IRI mapping.
765-
if options[:vocab] && (v_td = term_definitions[value])
766-
log_debug("") {"match with #{v_td.id}"} unless options[:quiet]
766+
if vocab && (v_td = term_definitions[value])
767+
log_debug("") {"match with #{v_td.id}"} unless quiet
767768
return v_td.id
768769
end
769770

770771
# If value contains a colon (:), it is either an absolute IRI or a compact IRI:
771772
if value.include?(':')
772773
prefix, suffix = value.split(':', 2)
773-
log_debug("") {"prefix: #{prefix.inspect}, suffix: #{suffix.inspect}, vocab: #{vocab.inspect}"} unless options[:quiet]
774+
log_debug("") {"prefix: #{prefix.inspect}, suffix: #{suffix.inspect}, vocab: #{self.vocab.inspect}"} unless quiet
774775

775776
# If prefix is underscore (_) or suffix begins with double-forward-slash (//), return value as it is already an absolute IRI or a blank node identifier.
776777
return RDF::Node.new(namer.get_sym(suffix)) if prefix == '_'
@@ -789,15 +790,15 @@ def expand_iri(value, options = {})
789790
RDF::URI(value)
790791
end
791792

792-
log_debug("") {"=> #{result.inspect}"} unless options[:quiet]
793+
log_debug("") {"=> #{result.inspect}"} unless quiet
793794
return result
794795
end
795-
log_debug("") {"=> #{result.inspect}"} unless options[:quiet]
796+
log_debug("") {"=> #{result.inspect}"} unless quiet
796797

797-
result = if options[:vocab] && vocab
798+
result = if vocab && self.vocab
798799
# If vocab is true, and active context has a vocabulary mapping, return the result of concatenating the vocabulary mapping with value.
799-
vocab + value
800-
elsif options[:documentRelative] && base = options.fetch(:base, self.base)
800+
self.vocab + value
801+
elsif documentRelative && (base ||= self.base)
801802
# Otherwise, if document relative is true, set value to the result of resolving value against the base IRI. Only the basic algorithm in section 5.2 of [RFC3986] is used; neither Syntax-Based Normalization nor Scheme-Based Normalization are performed. Characters additionally allowed in IRI references are treated in the same way that unreserved characters are treated in URI references, per section 6.5 of [RFC3987].
802803
value = RDF::URI(value)
803804
value.absolute? ? value : RDF::URI(base).join(value)
@@ -807,7 +808,7 @@ def expand_iri(value, options = {})
807808
else
808809
RDF::URI(value)
809810
end
810-
log_debug("") {"=> #{result}"} unless options[:quiet]
811+
log_debug("") {"=> #{result}"} unless quiet
811812
result
812813
end
813814
end
@@ -816,35 +817,34 @@ def expand_iri(value, options = {})
816817
# Compacts an absolute IRI to the shortest matching term or compact IRI
817818
#
818819
# @param [RDF::URI] iri
819-
# @param [Hash{Symbol => Object}] options ({})
820-
# @option options [Object] :value
820+
# @param [Object] value
821821
# Value, used to select among various maps for the same IRI
822-
# @option options [Boolean] :vocab
822+
# @param [Boolean] :vocab
823823
# specifies whether the passed iri should be compacted using the active context's vocabulary mapping
824-
# @option options [Boolean] :reverse
824+
# @param [Boolean] reverse
825825
# specifies whether a reverse property is being compacted
826+
# @param [Boolean] quiet (false)
827+
# @param [Hash{Symbol => Object}] options ({})
826828
#
827829
# @return [String] compacted form of IRI
828830
# @see http://json-ld.org/spec/latest/json-ld-api/#iri-compaction
829-
def compact_iri(iri, options = {})
831+
def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **options)
830832
return if iri.nil?
831833
iri = iri.to_s
832-
log_debug("compact_iri(#{iri.inspect}", options) {options.inspect} unless options[:quiet]
834+
log_debug("compact_iri(#{iri.inspect}", options) {[value, vocab, reverse].inspect} unless quiet
833835
log_depth(options) do
834836

835-
value = options.fetch(:value, nil)
836-
837-
if options[:vocab] && inverse_context.has_key?(iri)
838-
log_debug("") {"vocab and key in inverse context"} unless options[:quiet]
837+
if vocab && inverse_context.has_key?(iri)
838+
log_debug("") {"vocab and key in inverse context"} unless quiet
839839
default_language = self.default_language || @none
840840
containers = []
841841
tl, tl_value = "@language", "@null"
842842
containers << '@index' if index?(value)
843-
if options[:reverse]
843+
if reverse
844844
tl, tl_value = "@type", "@reverse"
845845
containers << '@set'
846846
elsif list?(value)
847-
log_debug("") {"list(#{value.inspect})"} unless options[:quiet]
847+
log_debug("") {"list(#{value.inspect})"} unless quiet
848848
# if value is a list object, then set type/language and type/language value to the most specific values that work for all items in the list as follows:
849849
containers << "@list" unless index?(value)
850850
list = value['@list']
@@ -865,25 +865,25 @@ def compact_iri(iri, options = {})
865865
end
866866
common_language ||= item_language
867867
if item_language != common_language && value?(item)
868-
log_debug("") {"-- #{item_language} conflicts with #{common_language}, use @none"} unless options[:quiet]
868+
log_debug("") {"-- #{item_language} conflicts with #{common_language}, use @none"} unless quiet
869869
common_language = '@none'
870870
end
871871
common_type ||= item_type
872872
if item_type != common_type
873873
common_type = '@none'
874-
log_debug("") {"#{item_type} conflicts with #{common_type}, use @none"} unless options[:quiet]
874+
log_debug("") {"#{item_type} conflicts with #{common_type}, use @none"} unless quiet
875875
end
876876
end
877877

878878
common_language ||= '@none'
879879
common_type ||= '@none'
880-
log_debug("") {"common type: #{common_type}, common language: #{common_language}"} unless options[:quiet]
880+
log_debug("") {"common type: #{common_type}, common language: #{common_language}"} unless quiet
881881
if common_type != '@none'
882882
tl, tl_value = '@type', common_type
883883
else
884884
tl_value = common_language
885885
end
886-
log_debug("") {"list: containers: #{containers.inspect}, type/language: #{tl.inspect}, type/language value: #{tl_value.inspect}"} unless options[:quiet]
886+
log_debug("") {"list: containers: #{containers.inspect}, type/language: #{tl.inspect}, type/language value: #{tl_value.inspect}"} unless quiet
887887
else
888888
if value?(value)
889889
if value.has_key?('@language') && !index?(value)
@@ -897,7 +897,7 @@ def compact_iri(iri, options = {})
897897
tl, tl_value = '@type', '@id'
898898
end
899899
containers << '@set'
900-
log_debug("") {"value: containers: #{containers.inspect}, type/language: #{tl.inspect}, type/language value: #{tl_value.inspect}"} unless options[:quiet]
900+
log_debug("") {"value: containers: #{containers.inspect}, type/language: #{tl.inspect}, type/language value: #{tl_value.inspect}"} unless quiet
901901
end
902902

903903
containers << '@none'
@@ -914,17 +914,17 @@ def compact_iri(iri, options = {})
914914
else
915915
preferred_values.concat([tl_value, '@none'])
916916
end
917-
log_debug("") {"preferred_values: #{preferred_values.inspect}"} unless options[:quiet]
917+
log_debug("") {"preferred_values: #{preferred_values.inspect}"} unless quiet
918918
if p_term = select_term(iri, containers, tl, preferred_values)
919-
log_debug("") {"=> term: #{p_term.inspect}"} unless options[:quiet]
919+
log_debug("") {"=> term: #{p_term.inspect}"} unless quiet
920920
return p_term
921921
end
922922
end
923923

924924
# At this point, there is no simple term that iri can be compacted to. If vocab is true and active context has a vocabulary mapping:
925-
if options[:vocab] && vocab && iri.start_with?(vocab) && iri.length > vocab.length
926-
suffix = iri[vocab.length..-1]
927-
log_debug("") {"=> vocab suffix: #{suffix.inspect}"} unless options[:quiet]
925+
if vocab && self.vocab && iri.start_with?(self.vocab) && iri.length > self.vocab.length
926+
suffix = iri[self.vocab.length..-1]
927+
log_debug("") {"=> vocab suffix: #{suffix.inspect}"} unless quiet
928928
return suffix unless term_definitions.has_key?(suffix)
929929
end
930930

@@ -944,7 +944,7 @@ def compact_iri(iri, options = {})
944944
end
945945

946946
if !candidates.empty?
947-
log_debug("") {"=> compact iri: #{candidates.term_sort.first.inspect}"} unless options[:quiet]
947+
log_debug("") {"=> compact iri: #{candidates.term_sort.first.inspect}"} unless quiet
948948
return candidates.term_sort.first
949949
end
950950

@@ -960,18 +960,18 @@ def compact_iri(iri, options = {})
960960
end
961961

962962
if !candidates.empty?
963-
log_debug("") {"=> standard prefies: #{candidates.term_sort.first.inspect}"} unless options[:quiet]
963+
log_debug("") {"=> standard prefies: #{candidates.term_sort.first.inspect}"} unless quiet
964964
return candidates.term_sort.first
965965
end
966966
end
967967

968-
if !options[:vocab]
968+
if !vocab
969969
# transform iri to a relative IRI using the document's base IRI
970970
iri = remove_base(iri)
971-
log_debug("") {"=> relative iri: #{iri.inspect}"} unless options[:quiet]
971+
log_debug("") {"=> relative iri: #{iri.inspect}"} unless quiet
972972
return iri
973973
else
974-
log_debug("") {"=> absolute iri: #{iri.inspect}"} unless options[:quiet]
974+
log_debug("") {"=> absolute iri: #{iri.inspect}"} unless quiet
975975
return iri
976976
end
977977
end
@@ -986,14 +986,13 @@ def compact_iri(iri, options = {})
986986
# Associated property used to find coercion rules
987987
# @param [Hash, String] value
988988
# Value (literal or IRI) to be expanded
989+
# @param [Boolean] useNativeTypes (false) use native representations
989990
# @param [Hash{Symbol => Object}] options
990-
# @option options [Boolean] :useNativeTypes (false) use native representations
991991
#
992992
# @return [Hash] Object representation of value
993993
# @raise [RDF::ReaderError] if the iri cannot be expanded
994994
# @see http://json-ld.org/spec/latest/json-ld-api/#value-expansion
995-
def expand_value(property, value, options = {})
996-
options = {useNativeTypes: false}.merge!(options)
995+
def expand_value(property, value, useNativeTypes: false, **options)
997996
log_depth(options) do
998997
log_debug("expand_value") {"property: #{property.inspect}, value: #{value.inspect}"}
999998

@@ -1021,7 +1020,7 @@ def expand_value(property, value, options = {})
10211020
when RDF::Literal
10221021
log_debug("Literal") {"datatype: #{value.datatype.inspect}"}
10231022
res = {}
1024-
if options[:useNativeTypes] && [RDF::XSD.boolean, RDF::XSD.integer, RDF::XSD.double].include?(value.datatype)
1023+
if useNativeTypes && [RDF::XSD.boolean, RDF::XSD.integer, RDF::XSD.double].include?(value.datatype)
10251024
res['@value'] = value.object
10261025
res['@type'] = uri(coerce(property)) if coerce(property)
10271026
else

0 commit comments

Comments
 (0)