Class: LocalAuthority
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- LocalAuthority
- Defined in:
- app/models/local_authority.rb
Class Method Summary collapse
- .entries_by_term(model, term, query) ⇒ Object
- .harvest_rdf(name, sources, opts = {}) ⇒ Object
- .harvest_tsv(name, sources, opts = {}) ⇒ Object
- .register_vocabulary(model, term, name) ⇒ Object
Class Method Details
.entries_by_term(model, term, query) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/local_authority.rb', line 70 def self.entries_by_term(model, term, query) return if query.empty? lowQuery = query.downcase hits = [] # move lc_subject into it's own table since being part of the usual structure caused it to be too slow. # When/if we move to having multiple dictionaries for subject we will need to also do a check for the appropriate dictionary. if (term == 'subject' && model == 'generic_files') # and local_authoritiy = lc_subject sql = SubjectLocalAuthorityEntry.where("lowerLabel like ?", "#{lowQuery}%").select("label, uri").limit(25).to_sql SubjectLocalAuthorityEntry.find_by_sql(sql).each do |hit| hits << {:uri => hit.uri, :label => hit.label} end else dterm = DomainTerm.where(:model => model, :term => term).first if dterm = dterm..collect(&:id).uniq sql = LocalAuthorityEntry.where("local_authority_id in (?)", ).where("lower(label) like ?", "#{lowQuery}%").select("label, uri").limit(25).to_sql LocalAuthorityEntry.find_by_sql(sql).each do |hit| hits << {:uri => hit.uri, :label => hit.label} end end end return hits end |
.harvest_rdf(name, sources, opts = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/local_authority.rb', line 23 def self.harvest_rdf(name, sources, opts = {}) return unless self.where(:name => name).empty? = self.create(:name => name) format = opts.fetch(:format, :ntriples) predicate = opts.fetch(:predicate, RDF::SKOS.prefLabel) entries = [] sources.each do |uri| RDF::Reader.open(uri, :format => format) do |reader| reader.each_statement do |statement| if statement.predicate == predicate entries << LocalAuthorityEntry.new(:local_authority => , :label => statement.object.to_s, :uri => statement.subject.to_s) end end end end LocalAuthorityEntry.import entries end |
.harvest_tsv(name, sources, opts = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/local_authority.rb', line 43 def self.harvest_tsv(name, sources, opts = {}) return unless self.where(:name => name).empty? = self.create(:name => name) prefix = opts.fetch(:prefix, "") entries = [] sources.each do |uri| open(uri) do |f| f.each_line do |tsv| fields = tsv.split(/\t/) entries << LocalAuthorityEntry.new(:local_authority => , :uri => "#{prefix}#{fields[0]}/", :label => fields[2]) end end end LocalAuthorityEntry.import entries end |
.register_vocabulary(model, term, name) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'app/models/local_authority.rb', line 61 def self.register_vocabulary(model, term, name) = self.find_by_name(name) return if .blank? model = model.to_s.sub(/RdfDatastream$/, '').underscore.pluralize domain_term = DomainTerm.find_or_create_by_model_and_term(:model => model, :term => term) return if domain_term..include? domain_term. << end |