lib/linked_vocabs/controlled.rb in linked_vocabs-0.2.0 vs lib/linked_vocabs/controlled.rb in linked_vocabs-0.3.0

- old
+ new

@@ -9,11 +9,11 @@ # @TODO: introduce graph context for provenance module Controlled def self.included(klass) klass.extend ClassMethods - klass.property :hiddenLabel, :predicate => RDF::SKOS.hiddenLabel + klass.property :hiddenLabel, predicate: RDF::Vocab::SKOS.hiddenLabel klass.validates_with LinkedVocabs::Validators::AuthorityValidator end def qa_interface self.class.qa_interface @@ -52,28 +52,43 @@ uri_or_str = vocab_matches.first return super if self.class.uses_vocab_prefix?(uri_or_str) and not uri_or_str.kind_of? RDF::Node end def in_vocab? - return false unless self.class.uses_vocab_prefix?(rdf_subject.to_s) - self.class.vocabularies.each do |vocab, config| - return false if rdf_subject == config[:prefix] - return false if config[:class].strict? and not config[:class].respond_to? rdf_subject.to_s.gsub(config[:prefix], '').to_sym - end + vocab, config = self.class.matching_vocab(rdf_subject.to_s) + return false unless vocab + return false if rdf_subject == config[:prefix] + return false if config[:class].strict? and not config[:class].respond_to? rdf_subject.to_s.gsub(config[:prefix], '').to_sym true end def rdf_label - labels = Array(self.class.rdf_label) - labels += default_labels + labels = Array(self.class.rdf_label) + default_labels labels.each do |label| - values = get_values(label) if values.blank? + values = label_with_preferred_language(label) if values.blank? return values unless values.empty? end node? ? [] : [rdf_subject.to_s] end + def label_with_preferred_language(label) + values = get_values(label, :literal => true) + preferred_languages.each do |preferred_language| + result = filter_by_language(values, preferred_language) + return result.map(&:to_s) unless result.blank? + end + values.map(&:to_s) + end + + def filter_by_language(values, language) + values.select { |x| x.language == language} + end + + def preferred_languages + [:en, :"en-us"] + end + ## # Class methods for adding and using controlled vocabularies module ClassMethods def use_vocabulary(name, opts={}) raise ControlledVocabularyError, "Vocabulary undefined: #{name.to_s.upcase}" unless LinkedVocabs.vocabularies.include? name @@ -111,24 +126,27 @@ load_vocab(name) end end def uses_vocab_prefix?(str) - vocabularies.each do |vocab, config| - return true if str.start_with? config[:prefix] + !!matching_vocab(str) + end + + def matching_vocab(str) + vocabularies.find do |vocab, config| + str.start_with? config[:prefix] end - false end def qa_interface @qa_interface ||= QaRDF.new(self) end private def name_to_class(name) - "RDF::#{name.upcase.to_s}".constantize + "RDF::Vocab::#{name.upcase.to_s}".constantize end def load_vocab(name) return nil unless LinkedVocabs.vocabularies[name.to_sym].include? :source cache = ActiveTriples::Repositories.repositories[repository] @@ -177,11 +195,11 @@ end def solutions_from_sparql_query(query) # @TODO: labels should be taken from ActiveTriples::Resource. # However, the default labels there are hidden behind a private method. - labels = [RDF::SKOS.prefLabel, - RDF::DC.title, + labels = [RDF::Vocab::SKOS.prefLabel, + RDF::Vocab::DC.title, RDF::RDFS.label] labels << @parent.rdf_label unless @parent.rdf_label.nil? solutions = query.map { |solution| solution if @parent.uses_vocab_prefix? solution[:s] }.compact label_solutions = solutions.map { |solution| build_hit(solution) if labels.include? solution[:p] }.compact