Class: Qa::LDF::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveTriples::RDFSource
Defined in:
lib/qa/ldf/model.rb

Overview

A base model for validatable authority values.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_graph(uri:, graph:) ⇒ Qa::LDF::Model

Builds a model from the graph.

Parameters:

  • graph (RDF::Graph)

Returns:



88
89
90
# File 'lib/qa/ldf/model.rb', line 88

def from_graph(uri:, graph:)
  new(uri) << graph
end

.from_qa_result(qa_result:) ⇒ Qa::LDF::Model

TODO:

Make ActiveTriples::RDFSource#default_labels public or protected.

Builds a model from a QA result hash.

Examples:

model =
  Qa::LDF::Model.from_qa_result(id:    'http://example.com/moomin',
                                label: 'Moomin'
model.to_uri    # => #<RDF::URI:0x... URI:http://example.com/moomin>
model.rdf_label # => ['Moomin']

Parameters:

  • qa_result (Hash<Symbol, String>)

Returns:



109
110
111
112
113
114
115
116
# File 'lib/qa/ldf/model.rb', line 109

def from_qa_result(qa_result:)
  qa_result.dup
  model = new(qa_result.delete(:id.to_s))
  model.set_value(model.send(:default_labels).first,
                  qa_result.delete(:label.to_s))

  model
end

Instance Method Details

#authorityQa::LDF::Authority

Examples:

# Regester the namespace with the authority class.
class MyAuthority < Authority
  register_namespace(namespace: 'http://example.com/my_authority#',
                     klass:     self)
end

model = Qa::LDF::Model.new('http://example.com/my_authority#moomin')

model.authority # => #<MyAuthority:0xbad1dea>

Returns:



24
25
26
# File 'lib/qa/ldf/model.rb', line 24

def authority
  Qa::LDF::Authority.for(namespace: authority_namespace)
end

#authority_namespaceString

Returns the namespace for the authority used by this model instance.

Examples:

# Regester the namespace with the authority class.
class MyAuthority < Authority
  register_namespace(namespace: 'http://example.com/my_authority#',
                     klass:     self)
end

model = Qa::LDF::Model.new('http://example.com/my_authority#moomin')

model.authority_namespace # => 'http://example.com/my_authority#'

Returns:

  • (String)

    the namespace for the authority used by this model instance.



43
44
45
46
47
48
49
# File 'lib/qa/ldf/model.rb', line 43

def authority_namespace
  return Qa::LDF::Authority.namespace if node?

  Qa::LDF::Authority
    .namespaces
    .find { |ns| to_uri.start_with?(ns) }
end

#fetchObject

Fetches from the cache client.

See Also:

  • ActiveTriples::RDFSource#fetch


55
56
57
58
59
60
61
62
# File 'lib/qa/ldf/model.rb', line 55

def fetch
  insert(authority.graph(to_uri))
rescue => e
  raise e unless block_given?
  yield(self)
ensure
  self
end

#lang_label(language: :en) ⇒ Object

Parameters:

  • language (Symbol) (defaults to: :en)

    a two letter (BCP47) language tag.

See Also:



70
71
72
73
74
75
76
77
78
79
# File 'lib/qa/ldf/model.rb', line 70

def lang_label(language: :en)
  labels = rdf_label

  label = labels.find do |literal|
    literal.respond_to?(:language) && literal.language == language
  end

  return labels.first unless label
  label
end