Module: PragmaticContext::Contextualizable

Defined in:
lib/pragmatic_context/contextualizable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/pragmatic_context/contextualizable.rb', line 5

def Contextualizable.included(base)
  base.extend ClassMethods
end

Instance Method Details

#as_jsonld(opts = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pragmatic_context/contextualizable.rb', line 30

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
  terms_with_context = self.class.contextualizer.definitions_for_terms(terms).keys
  json_results = as_json(opts).slice(*terms_with_context)
  results = {}
  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? 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

#contextObject



55
56
57
# File 'lib/pragmatic_context/contextualizable.rb', line 55

def context
  self.class.contextualizer.definitions_for_terms(terms)
end

#uncontextualized_termsObject



59
60
61
62
# File 'lib/pragmatic_context/contextualizable.rb', line 59

def uncontextualized_terms
  terms_with_context = self.class.contextualizer.definitions_for_terms(terms).keys
  terms - terms_with_context
end