rdf 3.1.1 → 3.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -58,6 +58,9 @@ module RDF
58
58
  class Vocabulary
59
59
  extend ::Enumerable
60
60
 
61
+ autoload :Format, 'rdf/vocab/writer'
62
+ autoload :Writer, 'rdf/vocab/writer'
63
+
61
64
  class << self
62
65
  ##
63
66
  # Enumerates known RDF vocabulary classes.
@@ -67,16 +70,103 @@ module RDF
67
70
  # @return [Enumerator]
68
71
  def each(&block)
69
72
  if self.equal?(Vocabulary)
70
- # This is needed since all vocabulary classes are defined using
71
- # Ruby's autoloading facility, meaning that `@@subclasses` will be
72
- # empty until each subclass has been touched or require'd.
73
- RDF::VOCABS.each { |v| require "rdf/vocab/#{v}" unless v == :rdf }
74
- @@subclasses.select(&:name).each(&block)
73
+ if @vocabs
74
+ @vocabs.select(&:name).each(&block)
75
+ else
76
+ # This is needed since all vocabulary classes are defined using
77
+ # Ruby's autoloading facility, meaning that `@@subclasses` will be
78
+ # empty until each subclass has been touched or require'd.
79
+ RDF::VOCABS.each { |v, p| RDF.const_get(p[:class_name].to_sym) unless v == :rdf }
80
+ @@subclasses.select(&:name).each(&block)
81
+ end
75
82
  else
76
83
  __properties__.each(&block)
77
84
  end
78
85
  end
79
86
 
87
+ ##
88
+ # A hash of all vocabularies by prefix showing relevant URI and
89
+ # associated vocabulary Class Name
90
+ #
91
+ # @return [Hash{Symbol => Hash{Symbol => String}}]
92
+ def vocab_map
93
+ # Create an initial duplicate of RDF::VOCABS. We want to
94
+ # ensure the hash itself is modifiable but the values are
95
+ # frozen.
96
+ @vocab_map ||= RDF::VOCABS.transform_values(&:freeze)
97
+ end
98
+
99
+ ##
100
+ # Return the vocabulary based on it's class_name symbol
101
+ #
102
+ # @param [Symbol] sym
103
+ # @return [RDF::Vocabulary]
104
+ def from_sym(sym)
105
+ RDF.const_get(sym.to_sym)
106
+ end
107
+
108
+ ##
109
+ # Register a vocabulary for internal prefix lookups. Parameters
110
+ # of interest include `:uri`, `:class_name`, `:source`, and `:skip`.
111
+ #
112
+ # @param prefix [Symbol] the prefix to use
113
+ # @param vocab [String, Class] either the URI or the vocab class
114
+ # @param params [Hash{Symbol => String}] Relevant parameters
115
+ # @return [Hash] The parameter hash, but frozen
116
+ def register(prefix, vocab, **params)
117
+ # check the input
118
+ raise ArgumentError, "#{prefix} must be symbol-able" unless
119
+ [String, Symbol].any? { |c| prefix.is_a? c }
120
+
121
+ # note an explicit uri: param overrides
122
+ case vocab
123
+ when String then params[:uri] ||= vocab
124
+ when Class
125
+ raise ArgumentError, 'vocab must be an RDF::(Strict)Vocabulary' unless
126
+ vocab.ancestors.include? RDF::Vocabulary
127
+ params[:class] = vocab
128
+ params[:uri] ||= vocab.to_uri.to_s
129
+ end
130
+
131
+ # fill in the class name
132
+ params[:class_name] ||= prefix.to_s.upcase
133
+
134
+ # now freeze and assign
135
+ vocab_map[prefix.to_s.to_sym] = params.freeze
136
+ end
137
+
138
+ ##
139
+ # Limits iteration over vocabularies to just those selected
140
+ #
141
+ # @example limit to set of vocabularies by symbol
142
+ # RDF::Vocabulary.limit_vocabs(:rdf, :rdfs
143
+ # RDF::Vocabulary.find_term('http://www.w3.org/2000/01/rdf-schema#Resource').pname
144
+ # # => 'rdfs:Resource'
145
+ #
146
+ # @example limit to set of vocabularies by class name
147
+ # RDF::Vocabulary.limit_vocabs(RDF::RDFV, RDF::RDFS)
148
+ # RDF::Vocabulary.find_term('http://www.w3.org/2000/01/rdf-schema#Resource').pname
149
+ # # => 'rdfs:Resource'
150
+ #
151
+ # @param [Array<symbol, RDF::Vocabulary>] vocabs
152
+ # A list of vocabularies (symbols or classes) which may
153
+ # be returned by {Vocabulary.each}. Also limits
154
+ # vocabularies that will be inspeced for other methods.
155
+ # Set to nil, or an empty array to reset.
156
+ # @return [Array<RDF::Vocabulary>]
157
+ def limit_vocabs(*vocabs)
158
+ @vocabs = if Array(vocabs).empty?
159
+ nil
160
+ else
161
+ vocabs.map do |vocab|
162
+ vocab = :rdfv if vocab == :rdf
163
+ vocab.is_a?(Symbol) && RDF::VOCABS.key?(vocab) ?
164
+ RDF.const_get(RDF::VOCABS[vocab][:class_name].to_sym) :
165
+ vocab
166
+ end.compact
167
+ end
168
+ end
169
+
80
170
  ##
81
171
  # Is this a strict vocabulary, or a liberal vocabulary allowing arbitrary properties?
82
172
  def strict?; false; end
@@ -291,7 +381,9 @@ module RDF
291
381
  prefix, suffix = pname.to_s.split(":", 2)
292
382
  if prefix == "rdf"
293
383
  RDF[suffix]
294
- elsif vocab = RDF::Vocabulary.each.detect {|v| v.__name__ && v.__prefix__ == prefix.to_sym}
384
+ elsif vocab_detail = RDF::Vocabulary.vocab_map[prefix.to_sym]
385
+ vocab = vocab_detail[:class] ||
386
+ RDF::Vocabulary.from_sym(vocab_detail[:class_name])
295
387
  suffix.to_s.empty? ? vocab.to_uri : vocab[suffix]
296
388
  else
297
389
  (RDF::Vocabulary.find_term(pname) rescue nil) || RDF::URI(pname, validate: true)
@@ -817,11 +909,10 @@ module RDF
817
909
  # @return [RDF::Vocabulary]
818
910
  attr_reader :vocab
819
911
 
820
- # Attributes of this vocabulary term, used for finding `label` and `comment` and to serialize the term back to RDF.
821
- # @return [Hash{Symbol,Resource => Term, #to_s}]
912
+ # Attributes of this vocabulary term, used for finding `label` and `comment` and to serialize the term back to RDF.
913
+ # @return [Hash{Symbol,Resource => Term, #to_s}]
822
914
  attr_reader :attributes
823
915
 
824
-
825
916
  ##
826
917
  # @overload new(uri, attributes:, **options)
827
918
  # @param [URI, String, #to_s] uri
@@ -882,7 +973,10 @@ module RDF
882
973
  #
883
974
  # @return [RDF::URI]
884
975
  def dup
885
- self.class.new((@value || @object).dup, attributes: attributes).extend(Term)
976
+ term = super.extend(Term)
977
+ term.instance_variable_set(:@vocab, vocab)
978
+ term.instance_variable_set(:@attributes, attributes)
979
+ term
886
980
  end
887
981
 
888
982
  ##
@@ -943,7 +1037,7 @@ module RDF
943
1037
  ##
944
1038
  # Values of an attributes as {RDF::Value}
945
1039
  #
946
- # @property [Symbol] prop
1040
+ # @param [Symbol] prop
947
1041
  # @return [RDF::Value, Array<RDF::Value>]
948
1042
  def attribute_value(prop)
949
1043
  values = attributes[prop]
@@ -511,11 +511,12 @@ module RDF
511
511
  # @since 0.3.0
512
512
  def format_term(term, **options)
513
513
  case term
514
- when String then format_literal(RDF::Literal(term, **options), **options)
515
- when RDF::List then format_list(term, **options)
516
- when RDF::Literal then format_literal(term, **options)
517
- when RDF::URI then format_uri(term, **options)
518
- when RDF::Node then format_node(term, **options)
514
+ when String then format_literal(RDF::Literal(term, **options), **options)
515
+ when RDF::List then format_list(term, **options)
516
+ when RDF::Literal then format_literal(term, **options)
517
+ when RDF::URI then format_uri(term, **options)
518
+ when RDF::Node then format_node(term, **options)
519
+ when RDF::Statement then format_rdfstar(term, **options)
519
520
  else nil
520
521
  end
521
522
  end
@@ -562,6 +563,21 @@ module RDF
562
563
  format_term(value.subject, **options)
563
564
  end
564
565
 
566
+ ##
567
+ # Formats a referenced triple.
568
+ #
569
+ # @example
570
+ # <<<s> <p> <o>>> <p> <o> .
571
+ #
572
+ # @param [RDF::Statement] value
573
+ # @param [Hash{Symbol => Object}] options = ({})
574
+ # @return [String]
575
+ # @raise [NotImplementedError] unless implemented in subclass
576
+ # @abstract
577
+ def format_rdfstar(value, **options)
578
+ raise NotImplementedError.new("#{self.class}#format_statement") # override in subclasses
579
+ end
580
+
565
581
  protected
566
582
 
567
583
  ##
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-01-08 00:00:00.000000000 Z
13
+ date: 2020-09-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: link_header
@@ -279,7 +279,7 @@ files:
279
279
  - lib/rdf/vocab/xsd.rb
280
280
  - lib/rdf/vocabulary.rb
281
281
  - lib/rdf/writer.rb
282
- homepage: https://ruby-rdf.github.com/
282
+ homepage: https://github.com/ruby-rdf/rdf
283
283
  licenses:
284
284
  - Unlicense
285
285
  metadata:
@@ -303,7 +303,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
303
303
  - !ruby/object:Gem::Version
304
304
  version: '0'
305
305
  requirements: []
306
- rubygems_version: 3.1.1
306
+ rubygems_version: 3.1.3
307
307
  signing_key:
308
308
  specification_version: 4
309
309
  summary: A Ruby library for working with Resource Description Framework (RDF) data.