Class: JSON::LD::Context::TermDefinition
- Inherits:
-
Object
- Object
- JSON::LD::Context::TermDefinition
- Defined in:
- lib/json/ld/context.rb
Overview
Term Definitions specify how properties and values have to be interpreted as well as the current vocabulary mapping and the default language
Instance Attribute Summary collapse
-
#container_mapping ⇒ Array<'@index', '@language', '@index', '@set', '@type', '@id', '@graph'>
Base container mapping, without @set.
-
#context ⇒ Hash{String => Object}
Term-specific context.
-
#direction_mapping ⇒ "ltr", "rtl"
Direction of term, ‘false` is used if there is explicit direction mapping mapping for this term.
-
#id ⇒ RDF::URI
IRI map.
-
#index ⇒ Boolean
Property used for data indexing; defaults to @index.
-
#language_mapping ⇒ String
Language mapping of term, ‘false` is used if there is an explicit language mapping for this term.
-
#nest ⇒ String
Term used for nest properties.
-
#prefix ⇒ Object
writeonly
Indicate that term may be used as a prefix.
-
#protected ⇒ Boolean
writeonly
Term is protected.
-
#reverse_property ⇒ Boolean
Reverse Property.
-
#simple ⇒ Boolean
This is a simple term definition, not an expanded term definition.
-
#term ⇒ String
Term name.
-
#type_mapping ⇒ String
Type mapping.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check if term definitions are identical, modulo @protected.
-
#as_set? ⇒ Boolean
If container mapping was defined along with @set.
-
#initialize(term, id: nil, index: nil, type_mapping: nil, container_mapping: nil, language_mapping: nil, direction_mapping: nil, reverse_property: false, nest: nil, protected: false, simple: false, prefix: nil, context: nil) ⇒ TermDefinition
constructor
Create a new Term Mapping with an ID.
- #inspect ⇒ Object
-
#prefix? ⇒ Boolean
This is an appropriate term to use as the prefix of a compact IRI.
-
#protected? ⇒ Boolean
Term is protected.
-
#simple? ⇒ Boolean
This is a simple term definition, not an expanded term definition.
-
#to_context_definition(context) ⇒ String, Hash{String => Array[String], String}
Output Hash or String definition for this definition considering @language and @vocab.
-
#to_rb ⇒ String
Turn this into a source for a new instantiation FIXME: context serialization.
Constructor Details
#initialize(term, id: nil, index: nil, type_mapping: nil, container_mapping: nil, language_mapping: nil, direction_mapping: nil, reverse_property: false, nest: nil, protected: false, simple: false, prefix: nil, context: nil) ⇒ TermDefinition
Create a new Term Mapping with an ID
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/json/ld/context.rb', line 115 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: false, 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 |
Instance Attribute Details
#container_mapping ⇒ Array<'@index', '@language', '@index', '@set', '@type', '@id', '@graph'>
Base container mapping, without @set
56 57 58 |
# File 'lib/json/ld/context.rb', line 56 def container_mapping @container_mapping end |
#context ⇒ Hash{String => Object}
Term-specific context
85 86 87 |
# File 'lib/json/ld/context.rb', line 85 def context @context end |
#direction_mapping ⇒ "ltr", "rtl"
Direction of term, ‘false` is used if there is explicit direction mapping mapping for this term.
67 68 69 |
# File 'lib/json/ld/context.rb', line 67 def direction_mapping @direction_mapping end |
#index ⇒ Boolean
Property used for data indexing; defaults to @index
78 79 80 |
# File 'lib/json/ld/context.rb', line 78 def index @index end |
#language_mapping ⇒ String
Language mapping of term, ‘false` is used if there is an explicit language mapping for this term.
63 64 65 |
# File 'lib/json/ld/context.rb', line 63 def language_mapping @language_mapping end |
#nest ⇒ String
Returns Term used for nest properties.
59 60 61 |
# File 'lib/json/ld/context.rb', line 59 def nest @nest end |
#prefix=(value) ⇒ Object (writeonly)
Indicate that term may be used as a prefix
81 82 83 |
# File 'lib/json/ld/context.rb', line 81 def prefix=(value) @prefix = value end |
#protected=(value) ⇒ Boolean (writeonly)
Term is protected.
89 90 91 |
# File 'lib/json/ld/context.rb', line 89 def protected=(value) @protected = value end |
#reverse_property ⇒ Boolean
Returns Reverse Property.
70 71 72 |
# File 'lib/json/ld/context.rb', line 70 def reverse_property @reverse_property end |
#simple ⇒ Boolean
This is a simple term definition, not an expanded term definition
74 75 76 |
# File 'lib/json/ld/context.rb', line 74 def simple @simple end |
#term ⇒ String
Returns term name.
49 50 51 |
# File 'lib/json/ld/context.rb', line 49 def term @term end |
#type_mapping ⇒ String
Returns Type mapping.
52 53 54 |
# File 'lib/json/ld/context.rb', line 52 def type_mapping @type_mapping end |
Instance Method Details
#==(other) ⇒ Boolean
Check if term definitions are identical, modulo @protected
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/json/ld/context.rb', line 232 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 |
#as_set? ⇒ Boolean
If container mapping was defined along with @set
228 |
# File 'lib/json/ld/context.rb', line 228 def as_set?; @as_set || false; end |
#inspect ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/json/ld/context.rb', line 249 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 |
#prefix? ⇒ Boolean
This is an appropriate term to use as the prefix of a compact IRI
97 |
# File 'lib/json/ld/context.rb', line 97 def prefix?; @prefix; end |
#protected? ⇒ Boolean
Term is protected.
145 |
# File 'lib/json/ld/context.rb', line 145 def protected?; !!@protected; end |
#simple? ⇒ Boolean
This is a simple term definition, not an expanded term definition
93 |
# File 'lib/json/ld/context.rb', line 93 def simple?; simple; end |
#to_context_definition(context) ⇒ String, Hash{String => Array[String], String}
Output Hash or String definition for this definition considering @language and @vocab
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/json/ld/context.rb', line 170 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['@context'] = @context if @context defn['@nest'] = @nest if @nest defn['@index'] = @index if @index defn['@prefix'] = @prefix unless @prefix.nil? defn end end |
#to_rb ⇒ String
Turn this into a source for a new instantiation FIXME: context serialization
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/json/ld/context.rb', line 211 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 |