22# frozen_string_literal: true
33require 'json'
44require 'bigdecimal'
5+ require 'set'
56
67module JSON ::LD
78 class Context
@@ -554,7 +555,7 @@ def create_term_definition(local_context, term, defined)
554555 end
555556
556557 # Since keywords cannot be overridden, term must not be a keyword. Otherwise, an invalid value has been detected, which is an error.
557- if KEYWORDS . include? ( term ) && ! %w( @vocab @language @version ) . include? ( term )
558+ if KEYWORDS . include? ( term ) && ( term != ' @vocab' && term != ' @language' && term != ' @version' )
558559 raise JsonLdError ::KeywordRedefinition , "term must not be a keyword: #{ term . inspect } " if
559560 @options [ :validate ]
560561 elsif !term_valid? ( term ) && @options [ :validate ]
@@ -606,7 +607,7 @@ def create_term_definition(local_context, term, defined)
606607 else
607608 :error
608609 end
609- unless %w( @id @vocab ) . include? ( type ) || type . is_a? ( RDF ::URI ) && type . absolute?
610+ unless ( type == ' @id' || type == ' @vocab' ) || type . is_a? ( RDF ::URI ) && type . absolute?
610611 raise JsonLdError ::InvalidTypeMapping , "unknown mapping for '@type': #{ type . inspect } on term #{ term . inspect } "
611612 end
612613 #log_debug("") {"type_mapping: #{type.inspect}"}
@@ -633,7 +634,7 @@ def create_term_definition(local_context, term, defined)
633634 container = value [ '@container' ]
634635 raise JsonLdError ::InvalidReverseProperty ,
635636 "unknown mapping for '@container' to #{ container . inspect } on term #{ term . inspect } " unless
636- container . is_a? ( String ) && [ '@set' , '@index' ] . include? ( container )
637+ container . is_a? ( String ) && ( container == '@set' || container == '@index' )
637638 definition . container_mapping = check_container ( container , local_context , defined , term )
638639 end
639640 definition . reverse_property = true
@@ -791,7 +792,7 @@ def from_vocabulary(graph)
791792 ( statements [ statement . subject ] ||= [ ] ) << statement
792793
793794 # Keep track of predicate ranges
794- if [ RDF ::RDFS . range , RDF ::SCHEMA . rangeIncludes ] . include? ( statement . predicate )
795+ if [ RDF ::RDFS . range , RDF ::SCHEMA . rangeIncludes ] . include? ( statement . predicate )
795796 ( ranges [ statement . subject ] ||= [ ] ) << statement . object
796797 end
797798 end
@@ -886,7 +887,7 @@ def container(term)
886887 # @param [Term, #to_s] term in unexpanded form
887888 # @return [Boolean]
888889 def as_array? ( term )
889- return true if %w( @graph @list ) . include? ( term )
890+ return true if term == ' @graph' || term == ' @list'
890891 term = find_definition ( term )
891892 term && ( term . as_set || term . container_mapping == '@list' )
892893 end
@@ -1138,7 +1139,7 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
11381139 tl_value ||= '@null'
11391140 preferred_values = [ ]
11401141 preferred_values << '@reverse' if tl_value == '@reverse'
1141- if %w( @id @reverse ) . include? ( tl_value ) && value . is_a? ( Hash ) && value . has_key? ( '@id' )
1142+ if ( tl_value == ' @id' || tl_value == ' @reverse' ) && value . is_a? ( Hash ) && value . has_key? ( '@id' )
11421143 t_iri = compact_iri ( value [ '@id' ] , vocab : true , document_relative : true )
11431144 if ( r_td = term_definitions [ t_iri ] ) && r_td . id == value [ '@id' ]
11441145 preferred_values . concat ( %w( @vocab @id @none ) )
@@ -1204,6 +1205,8 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
12041205 end
12051206 end
12061207
1208+ RDF_LITERAL_NATIVE_TYPES = Set . new ( [ RDF ::XSD . boolean , RDF ::XSD . integer , RDF ::XSD . double ] ) . freeze
1209+
12071210 ##
12081211 # If active property has a type mapping in the active context set to @id or @vocab, a JSON object with a single member @id whose value is the result of using the IRI Expansion algorithm on value is returned.
12091212 #
@@ -1246,7 +1249,7 @@ def expand_value(property, value, useNativeTypes: false, **options)
12461249 when RDF ::Literal
12471250 #log_debug("Literal") {"datatype: #{value.datatype.inspect}"}
12481251 res = { }
1249- if useNativeTypes && [ RDF :: XSD . boolean , RDF :: XSD . integer , RDF :: XSD . double ] . include? ( value . datatype )
1252+ if useNativeTypes && RDF_LITERAL_NATIVE_TYPES . include? ( value . datatype )
12501253 res [ '@value' ] = value . object
12511254 res [ '@type' ] = uri ( coerce ( property ) ) if coerce ( property )
12521255 else
@@ -1407,7 +1410,7 @@ def dup
14071410 def coerce ( property )
14081411 # Map property, if it's not an RDF::Value
14091412 # @type is always is an IRI
1410- return '@id' if [ RDF . type , '@type' ] . include? ( property )
1413+ return '@id' if property == RDF . type || property == '@type'
14111414 term_definitions [ property ] && term_definitions [ property ] . type_mapping
14121415 end
14131416
0 commit comments