Class: YAML_LD::Representation::IRTree

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-d851238cc6dc/lib/yaml_ld/representation.rb

Overview

Note:

Rails 6.0 not compatible with Psych 4.0, which defines RestrictedYamlTree.

Build a YAML AST with an RDF::Literal visitor

builder = Psych::Visitors::YAMLTree.new builder << { :foo => 'bar' } builder.tree # => # "..."

Instance Method Summary collapse

Instance Method Details

#datatypes(object) ⇒ Object

Retrive the literals from an object



226
227
228
229
230
231
232
233
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-d851238cc6dc/lib/yaml_ld/representation.rb', line 226

def datatypes(object)
  case object
  when Array        then object.inject([]) {|memo, o| memo += datatypes(o)}
  when Hash         then object.values.inject([]) {|memo, o| memo += datatypes(o)}
  when RDF::Literal then object.datatype? ? [object.datatype] : []
  else []
  end
end

#push(object) ⇒ Object Also known as: <<

Add the object to be emitted. If the :extended option is set when the visitor is created, tags are added to the document.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-d851238cc6dc/lib/yaml_ld/representation.rb', line 237

def push object
  start unless started?
  version = []
  version = [1,1] if @options[:header]

  case @options[:version]
  when Array
    version = @options[:version]
  when String
    version = @options[:version].split('.').map { |x| x.to_i }
  else
    version = [1,1]
  end if @options.key? :version

  dts = datatypes(object).uniq
  tags = dts.inject({}) do |memo, dt|
    # Find the most suitable voabulary, if any
    if memo.keys.any? {|u| dt.to_s.start_with?(u)}
      memo
      # Already have a prefix for this
    elsif vocab = RDF::Vocabulary.each.to_a.detect {|v| dt.to_s.index(v.to_uri.to_s) == 0}
      # Index by vocabulary URI
      memo.merge(vocab.to_uri.to_s => "!#{vocab.__prefix__}!")
    else
      memo
    end
  end
  
  @emitter.start_document version, tags.invert.to_a, false
  accept object
  @emitter.end_document !@emitter.streaming?
end

#visit_RDF_Literal(o) ⇒ Object

Emit an RDF Literal. If extended is set, use the datatype as an tag, otherwise, emit in expanded form.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-d851238cc6dc/lib/yaml_ld/representation.rb', line 274

def visit_RDF_Literal o
  tag = case o.datatype
  when nil then nil
  when RDF::XSD.string then nil
  when RDF.langString
    "https://www.w3.org/ns/i18n##{o.language}" if @options[:extendedYAML]
  else
    if o.datatype.to_s.start_with?('https://www.w3.org/ns/i18n#')
      o.datatype.to_s if @options[:extendedYAML]
    elsif @options[:extendedYAML]
      o.datatype.to_s
    else
      nil
    end
  end
  formatted = o.value
  register o, @emitter.scalar(formatted, nil, tag, false, false, Psych::Nodes::Scalar::PLAIN)
end