2
2
# frozen_string_literal: true
3
3
require 'json'
4
4
require 'bigdecimal'
5
+ require 'set'
5
6
6
7
module JSON ::LD
7
8
class Context
@@ -112,7 +113,7 @@ def initialize(term,
112
113
def container_mapping = ( mapping )
113
114
mapping = Array ( mapping )
114
115
if @as_set = mapping . include? ( '@set' )
115
- mapping -= %w( @set )
116
+ mapping . delete ( ' @set' )
116
117
end
117
118
@container_mapping = mapping . first
118
119
end
@@ -554,7 +555,7 @@ def create_term_definition(local_context, term, defined)
554
555
end
555
556
556
557
# 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' )
558
559
raise JsonLdError ::KeywordRedefinition , "term must not be a keyword: #{ term . inspect } " if
559
560
@options [ :validate ]
560
561
elsif !term_valid? ( term ) && @options [ :validate ]
@@ -606,7 +607,7 @@ def create_term_definition(local_context, term, defined)
606
607
else
607
608
:error
608
609
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?
610
611
raise JsonLdError ::InvalidTypeMapping , "unknown mapping for '@type': #{ type . inspect } on term #{ term . inspect } "
611
612
end
612
613
#log_debug("") {"type_mapping: #{type.inspect}"}
@@ -615,7 +616,7 @@ def create_term_definition(local_context, term, defined)
615
616
616
617
if value . has_key? ( '@reverse' )
617
618
raise JsonLdError ::InvalidReverseProperty , "unexpected key in #{ value . inspect } on term #{ term . inspect } " if
618
- value . keys . any? { | k | %w( @id @ nest) . include? ( k ) }
619
+ value . key? ( '@id' ) || value . key? ( '@ nest' )
619
620
raise JsonLdError ::InvalidIRIMapping , "expected value of @reverse to be a string: #{ value [ '@reverse' ] . inspect } on term #{ term . inspect } " unless
620
621
value [ '@reverse' ] . is_a? ( String )
621
622
@@ -633,7 +634,7 @@ def create_term_definition(local_context, term, defined)
633
634
container = value [ '@container' ]
634
635
raise JsonLdError ::InvalidReverseProperty ,
635
636
"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' )
637
638
definition . container_mapping = check_container ( container , local_context , defined , term )
638
639
end
639
640
definition . reverse_property = true
@@ -654,7 +655,7 @@ def create_term_definition(local_context, term, defined)
654
655
( simple_term || ( ( processingMode || 'json-ld-1.0' ) == 'json-ld-1.0' ) )
655
656
elsif term . include? ( ':' )
656
657
# If term is a compact IRI with a prefix that is a key in local context then a dependency has been found. Use this algorithm recursively passing active context, local context, the prefix as term, and defined.
657
- prefix , suffix = term . split ( ':' )
658
+ prefix , suffix = term . split ( ':' , 2 )
658
659
create_term_definition ( local_context , prefix , defined ) if local_context . has_key? ( prefix )
659
660
660
661
definition . id = if td = term_definitions [ prefix ]
@@ -791,15 +792,15 @@ def from_vocabulary(graph)
791
792
( statements [ statement . subject ] ||= [ ] ) << statement
792
793
793
794
# 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 )
795
796
( ranges [ statement . subject ] ||= [ ] ) << statement . object
796
797
end
797
798
end
798
799
799
800
# Add term definitions for each class and property not in vocab, and
800
801
# for those properties having an object range
801
802
statements . each do |subject , values |
802
- types = values . select { | v | v . predicate == RDF . type } . map ( & :object )
803
+ types = values . each_with_object ( [ ] ) { | v , memo | memo << v . object if v . predicate == RDF . type }
803
804
is_property = types . any? { |t | t . to_s . include? ( "Property" ) }
804
805
805
806
term = subject . to_s . split ( /[\/ \# ]/ ) . last
@@ -886,7 +887,7 @@ def container(term)
886
887
# @param [Term, #to_s] term in unexpanded form
887
888
# @return [Boolean]
888
889
def as_array? ( term )
889
- return true if %w( @graph @list ) . include? ( term )
890
+ return true if term == ' @graph' || term == ' @list'
890
891
term = find_definition ( term )
891
892
term && ( term . as_set || term . container_mapping == '@list' )
892
893
end
@@ -979,12 +980,14 @@ def reverse_term(term)
979
980
# IRI or String, if it's a keyword
980
981
# @raise [JSON::LD::JsonLdError::InvalidIRIMapping] if the value cannot be expanded
981
982
# @see http://json-ld.org/spec/latest/json-ld-api/#iri-expansion
982
- def expand_iri ( value , documentRelative : false , vocab : false , local_context : nil , defined : { } , quiet : false , **options )
983
+ def expand_iri ( value , documentRelative : false , vocab : false , local_context : nil , defined : nil , quiet : false , **options )
983
984
return value unless value . is_a? ( String )
984
985
985
986
return value if KEYWORDS . include? ( value )
986
987
#log_debug("expand_iri") {"value: #{value.inspect}"} unless quiet
987
988
989
+ defined = defined || { } # if we initialized in the keyword arg we would allocate {} at each invokation, even in the 2 (common) early returns above.
990
+
988
991
# 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.
989
992
if local_context && local_context . has_key? ( value ) && !defined [ value ]
990
993
create_term_definition ( local_context , value , defined )
@@ -1003,7 +1006,7 @@ def expand_iri(value, documentRelative: false, vocab: false, local_context: nil,
1003
1006
1004
1007
# 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.
1005
1008
return RDF ::Node . new ( namer . get_sym ( suffix ) ) if prefix == '_'
1006
- return RDF ::URI ( value ) if suffix [ 0 , 2 ] == '//'
1009
+ return RDF ::URI ( value ) if suffix . start_with? ( '//' )
1007
1010
1008
1011
# If local context is not null, it contains a key that equals prefix, and the value associated with the key that equals prefix in defined is not true, invoke the Create Term Definition algorithm, passing active context, local context, prefix as term, and defined. This will ensure that a term definition is created for prefix in active context during Context Processing.
1009
1012
if local_context && local_context . has_key? ( prefix ) && !defined [ prefix ]
@@ -1138,7 +1141,7 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
1138
1141
tl_value ||= '@null'
1139
1142
preferred_values = [ ]
1140
1143
preferred_values << '@reverse' if tl_value == '@reverse'
1141
- if %w( @id @reverse ) . include? ( tl_value ) && value . is_a? ( Hash ) && value . has_key? ( '@id' )
1144
+ if ( tl_value == ' @id' || tl_value == ' @reverse' ) && value . is_a? ( Hash ) && value . has_key? ( '@id' )
1142
1145
t_iri = compact_iri ( value [ '@id' ] , vocab : true , document_relative : true )
1143
1146
if ( r_td = term_definitions [ t_iri ] ) && r_td . id == value [ '@id' ]
1144
1147
preferred_values . concat ( %w( @vocab @id @none ) )
@@ -1204,6 +1207,8 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
1204
1207
end
1205
1208
end
1206
1209
1210
+ RDF_LITERAL_NATIVE_TYPES = Set . new ( [ RDF ::XSD . boolean , RDF ::XSD . integer , RDF ::XSD . double ] ) . freeze
1211
+
1207
1212
##
1208
1213
# 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.
1209
1214
#
@@ -1246,7 +1251,7 @@ def expand_value(property, value, useNativeTypes: false, **options)
1246
1251
when RDF ::Literal
1247
1252
#log_debug("Literal") {"datatype: #{value.datatype.inspect}"}
1248
1253
res = { }
1249
- if useNativeTypes && [ RDF :: XSD . boolean , RDF :: XSD . integer , RDF :: XSD . double ] . include? ( value . datatype )
1254
+ if useNativeTypes && RDF_LITERAL_NATIVE_TYPES . include? ( value . datatype )
1250
1255
res [ '@value' ] = value . object
1251
1256
res [ '@type' ] = uri ( coerce ( property ) ) if coerce ( property )
1252
1257
else
@@ -1297,7 +1302,7 @@ def expand_value(property, value, useNativeTypes: false, **options)
1297
1302
def compact_value ( property , value , options = { } )
1298
1303
#log_debug("compact_value") {"property: #{property.inspect}, value: #{value.inspect}"}
1299
1304
1300
- num_members = value . keys . length
1305
+ num_members = value . length
1301
1306
1302
1307
num_members -= 1 if index? ( value ) && container ( property ) == '@index'
1303
1308
if num_members > 2
@@ -1407,7 +1412,7 @@ def dup
1407
1412
def coerce ( property )
1408
1413
# Map property, if it's not an RDF::Value
1409
1414
# @type is always is an IRI
1410
- return '@id' if [ RDF . type , '@type' ] . include? ( property )
1415
+ return '@id' if property == RDF . type || property == '@type'
1411
1416
term_definitions [ property ] && term_definitions [ property ] . type_mapping
1412
1417
end
1413
1418
@@ -1573,9 +1578,10 @@ def remove_base(iri)
1573
1578
#
1574
1579
# @return [Array<RDF::URI>]
1575
1580
def mappings
1576
- term_definitions . inject ( { } ) do |memo , ( t , td ) |
1577
- memo [ t ] = td ? td . id : nil
1578
- memo
1581
+ { } . tap do |memo |
1582
+ term_definitions . each_pair do |t , td |
1583
+ memo [ t ] = td ? td . id : nil
1584
+ end
1579
1585
end
1580
1586
end
1581
1587
@@ -1595,9 +1601,10 @@ def mapping(term)
1595
1601
# @return [Array<String>]
1596
1602
# @deprecated
1597
1603
def languages
1598
- term_definitions . inject ( { } ) do |memo , ( t , td ) |
1599
- memo [ t ] = td . language_mapping
1600
- memo
1604
+ { } . tap do |memo |
1605
+ term_definitions . each_pair do |t , td |
1606
+ memo [ t ] = td . language_mapping
1607
+ end
1601
1608
end
1602
1609
end
1603
1610
@@ -1610,7 +1617,7 @@ def check_container(container, local_context, defined, term)
1610
1617
end
1611
1618
1612
1619
val = Array ( container )
1613
- val -= %w( @set ) if has_set = val . include? ( '@set' )
1620
+ val . delete ( ' @set' ) if has_set = val . include? ( '@set' )
1614
1621
1615
1622
raise JsonLdError ::InvalidContainerMapping ,
1616
1623
"'@container' has more than one value other than @set" if val . length > 1
0 commit comments