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

- old
+ new

@@ -6,11 +6,11 @@ ## # Adds add support for controlled vocabularies and # QuestioningAuthority to RdfResource classes. # @TODO: introduce graph context for provenance module Controlled - + def self.included(klass) klass.extend ClassMethods klass.property :hiddenLabel, :predicate => RDF::SKOS.hiddenLabel klass.validates_with LinkedVocabs::Validators::AuthorityValidator end @@ -18,21 +18,23 @@ def qa_interface self.class.qa_interface end delegate :search, :get_full_record, :response, :results, :to => :qa_interface - + # Override set_subject! to find terms when (and only when) they # exist in the vocabulary def set_subject!(uri_or_str) vocab_matches = [] begin uri = get_uri(uri_or_str) uri_or_str = uri rescue RuntimeError, NoMethodError end + return false if uri_or_str.is_a? RDF::Node + self.class.vocabularies.each do |vocab, config| if uri_or_str.start_with? config[:prefix] # @TODO: is it good to need a full URI for a non-strict vocab? return super if config[:strict] == false uri_stub = uri_or_str.to_s.gsub(config[:prefix], '') @@ -48,11 +50,11 @@ end return super if vocab_matches.empty? 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 @@ -159,11 +161,11 @@ response end def get_full_record(id, sub_authority) end - + private def sparql_starts_search(q) query = @sparql.query("SELECT DISTINCT ?s ?p ?o WHERE { ?s ?p ?o. FILTER(strstarts(lcase(?o), '#{q.downcase}'))}") solutions_from_sparql_query(query) @@ -171,24 +173,24 @@ def sparql_contains_search(q) query = @sparql.query("SELECT DISTINCT ?s ?p ?o WHERE { ?s ?p ?o. FILTER(contains(lcase(?o), '#{q.downcase}'))}") solutions_from_sparql_query(query) 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. + # However, the default labels there are hidden behind a private method. labels = [RDF::SKOS.prefLabel, RDF::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 return label_solutions.uniq unless label_solutions.empty? solutions.map { |solution| build_hit(solution) }.compact.uniq end - + def build_hit(solution) { :id => solution[:s].to_s, :label => solution[:o].to_s } end end end