class Qa::LDF::Model

A base model for validatable authority values.

Public Class Methods

from_graph(uri:, graph:) click to toggle source

Builds a model from the graph.

@param graph [RDF::Graph]

@return [Qa::LDF::Model] the built model

# File lib/qa/ldf/model.rb, line 88
def from_graph(uri:, graph:)
  new(uri) << graph
end
from_qa_result(qa_result:) click to toggle source

Builds a model from a QA result hash.

@example

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']

@param qa_result [Hash<Symbol, String>]

@return [Qa::LDF::Model] the built model

@todo Make ActiveTriples::RDFSource#default_labels public or

protected.
# 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

Public Instance Methods

authority() click to toggle source

@return [Qa::LDF::Authority]

@example

# 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>
# File lib/qa/ldf/model.rb, line 24
def authority
  Qa::LDF::Authority.for(namespace: authority_namespace)
end
authority_namespace() click to toggle source

@return [String] the namespace for the authority used by this model

instance.

@example

# 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#'
# 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
fetch() { |self| ... } click to toggle source

Fetches from the cache client.

@see ActiveTriples::RDFSource#fetch

# 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) click to toggle source

@param language [Symbol] a two letter (BCP47) language tag.

@see ActiveTriples::RDFSource.rdf_label @see www.w3.org/TR/rdf11-concepts/#section-Graph-Literal @see tools.ietf.org/html/bcp47

# 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