module PragmaticContext::Contextualizable
Public Class Methods
included(base)
click to toggle source
# File lib/pragmatic_context/contextualizable.rb, line 5 def Contextualizable.included(base) base.extend ClassMethods end
Public Instance Methods
as_jsonld(opts = nil)
click to toggle source
# File lib/pragmatic_context/contextualizable.rb, line 42 def as_jsonld(opts = nil) # We iterate over terms_with_context because we want to look at our own # fields (and not the fields in 'as_json') to case on their class. In the # case where we want to serialize directly, we rely on the field value as # sourced from as_json context = jsonld_context terms_with_context = context.keys json_results = as_json(opts).slice(*terms_with_context) results = {} results['@id'] = self.class.context_id_factory.call(self) if self.class.context_id_factory results['@type'] = self.class.contextualized_type if self.class.contextualized_type terms_with_context.each do |term| # Don't use idiomatic case here since Mongoid relations return proxies # that fail the Contextualizable test value = self.send(term) if (value.is_a? Contextualizable) results[term] = self.send(term).as_jsonld elsif (value.is_a? Array) results[term] = self.send(term).each_with_index.map do |element, idx| if (element.is_a? Contextualizable) element.as_jsonld else json_results[term][idx] end end elsif (value.is_a? Hash) self.send(term).each do |key, value| results["#{term}:#{key}"] = value end else results[term] = json_results[term] end end results.merge("@context" => context) end
jsonld_context()
click to toggle source
# File lib/pragmatic_context/contextualizable.rb, line 78 def jsonld_context self.class.contextualizer.definitions_for_terms(terms) end
uncontextualized_terms()
click to toggle source
# File lib/pragmatic_context/contextualizable.rb, line 82 def uncontextualized_terms terms_with_context = self.class.contextualizer.definitions_for_terms(terms).keys terms - terms_with_context end
Private Instance Methods
terms()
click to toggle source
# File lib/pragmatic_context/contextualizable.rb, line 89 def terms as_json.keys end