class JSON::LD::Context::TermDefinition
Term Definitions specify how properties and values have to be interpreted as well as the current vocabulary mapping and the default language
Attributes
Base container mapping, without @set @return [Array<'@index', '@language', '@index', '@set', '@type', '@id', '@graph'>] Container mapping
Term-specific context @return [Hash{String => Object}]
Direction of term, `false` is used if there is explicit direction mapping mapping for this term. @return [“ltr”, “rtl”] direction_mapping
@return [RDF::URI] IRI map
Property used for data indexing; defaults to @index @return [Boolean]
Language mapping of term, `false` is used if there is an explicit language mapping for this term. @return [String] Language mapping
@return [String] Term used for nest properties
Indicate that term may be used as a prefix
Term is protected. @return [Boolean]
@return [Boolean] Reverse Property
This is a simple term definition, not an expanded term definition @return [Boolean]
@return [String] term name
@return [String] Type mapping
Public Class Methods
Create a new Term Mapping with an ID @param [String] term @param [String] id @param [String] type_mapping
Type mapping @param [Set<'@index', '@language', '@index', '@set', '@type', '@id', '@graph'>] container_mapping
@param [String] language_mapping
Language mapping of term, `false` is used if there is an explicit language mapping for this term
@param [“ltr”, “rtl”] direction_mapping
Direction mapping of term, `false` is used if there is an explicit direction mapping for this term
@param [Boolean] reverse_property
@param [Boolean] protected mark resulting context as protected @param [String] nest term used for nest properties @param [Boolean] simple
This is a simple term definition, not an expanded term definition
@param [Boolean] prefix
Term may be used as a prefix
# File lib/json/ld/context.rb, line 2068 def initialize(term, id: nil, index: nil, type_mapping: nil, container_mapping: nil, language_mapping: nil, direction_mapping: nil, reverse_property: false, nest: nil, protected: nil, simple: false, prefix: nil, context: nil) @term = term @id = id.to_s unless id.nil? @index = index.to_s unless index.nil? @type_mapping = type_mapping.to_s unless type_mapping.nil? self.container_mapping = container_mapping @language_mapping = language_mapping unless language_mapping.nil? @direction_mapping = direction_mapping unless direction_mapping.nil? @reverse_property = reverse_property @protected = protected @nest = nest unless nest.nil? @simple = simple @prefix = prefix unless prefix.nil? @context = context unless context.nil? end
Public Instance Methods
Check if term definitions are identical, modulo @protected @return [Boolean]
# File lib/json/ld/context.rb, line 2186 def ==(other) other.is_a?(TermDefinition) && id == other.id && term == other.term && type_mapping == other.type_mapping && container_mapping == other.container_mapping && nest == other.nest && language_mapping == other.language_mapping && direction_mapping == other.direction_mapping && reverse_property == other.reverse_property && simple == other.simple && index == other.index && context == other.context && prefix? == other.prefix? && as_set? == other.as_set? end
If container mapping was defined along with @set @return [Boolean]
# File lib/json/ld/context.rb, line 2182 def as_set?; @as_set || false; end
Set container mapping, from an array which may include @set
# File lib/json/ld/context.rb, line 2101 def container_mapping=(mapping) mapping = case mapping when Set then mapping when Array then Set.new(mapping) when String then Set[mapping] when nil then Set.new else raise "Shouldn't happen with #{mapping.inspect}" end if @as_set = mapping.include?('@set') mapping = mapping.dup mapping.delete('@set') end @container_mapping = mapping @index ||= '@index' if mapping.include?('@index') end
# File lib/json/ld/context.rb, line 2203 def inspect v = %w([TD) v << "id=#{@id}" v << "index=#{index.inspect}" unless index.nil? v << "term=#{@term}" v << "rev" if reverse_property v << "container=#{container_mapping}" if container_mapping v << "as_set=#{as_set?.inspect}" v << "lang=#{language_mapping.inspect}" unless language_mapping.nil? v << "dir=#{direction_mapping.inspect}" unless direction_mapping.nil? v << "type=#{type_mapping}" unless type_mapping.nil? v << "nest=#{nest.inspect}" unless nest.nil? v << "simple=true" if @simple v << "protected=true" if @protected v << "prefix=#{@prefix.inspect}" unless @prefix.nil? v << "has-context" unless context.nil? v.join(" ") + "]" end
This is an appropriate term to use as the prefix of a compact IRI @return [Boolean] simple
# File lib/json/ld/context.rb, line 2050 def prefix?; @prefix; end
Term is protected. @return [Boolean]
# File lib/json/ld/context.rb, line 2098 def protected?; !!@protected; end
This is a simple term definition, not an expanded term definition @return [Boolean] simple
# File lib/json/ld/context.rb, line 2046 def simple?; simple; end
Output Hash or String definition for this definition considering @language and @vocab
@param [Context] context @return [String, Hash{String => Array, String}]
# File lib/json/ld/context.rb, line 2123 def to_context_definition(context) cid = if context.vocab && id.start_with?(context.vocab) # Nothing to return unless it's the same as the vocab id == context.vocab ? context.vocab : id.to_s[context.vocab.length..-1] else # Find a term to act as a prefix iri, prefix = context.iri_to_term.detect {|i,p| id.to_s.start_with?(i.to_s)} iri && iri != id ? "#{prefix}:#{id.to_s[iri.length..-1]}" : id end if simple? cid.to_s unless cid == term && context.vocab else defn = {} defn[reverse_property ? '@reverse' : '@id'] = cid.to_s unless cid == term && !reverse_property if type_mapping defn['@type'] = if KEYWORDS.include?(type_mapping) type_mapping else context.compact_iri(type_mapping, vocab: true) end end cm = Array(container_mapping) cm << "@set" if as_set? && !cm.include?("@set") cm = cm.first if cm.length == 1 defn['@container'] = cm unless cm.empty? # Language set as false to be output as null defn['@language'] = (@language_mapping ? @language_mapping : nil) unless @language_mapping.nil? defn['@direction'] = (@direction_mapping ? @direction_mapping : nil) unless @direction_mapping.nil? defn['@context'] = @context if @context defn['@nest'] = @nest if @nest defn['@index'] = @index if @index defn['@prefix'] = @prefix unless @prefix.nil? defn end end
Turn this into a source for a new instantiation FIXME: context serialization @return [String]
# File lib/json/ld/context.rb, line 2165 def to_rb defn = [%(TermDefinition.new\(#{term.inspect})] %w(id index type_mapping container_mapping language_mapping direction_mapping reverse_property nest simple prefix context protected).each do |acc| v = instance_variable_get("@#{acc}".to_sym) v = v.to_s if v.is_a?(RDF::Term) if acc == 'container_mapping' v = v.to_a v << '@set' if as_set? v = v.first if v.length <= 1 end defn << "#{acc}: #{v.inspect}" if v end defn.join(', ') + ")" end