Sha256: 20ec8012c03323406a7dff5575a98f076980aaf860ebc42c70507e4a11d5ca24

Contents?: true

Size: 1.15 KB

Versions: 7

Compression:

Stored size: 1.15 KB

Contents

module Qa::Authorities
  class Local::FileBasedAuthority < Base
    attr_reader :subauthority
    def initialize(subauthority)
      @subauthority = subauthority
    end

    def search(q)
      r = q.blank? ? [] : terms.select { |term| /\b#{q.downcase}/.match(term[:term].downcase) }
      r.map do |res|
        { :id => res[:id], :label => res[:term] }.with_indifferent_access
      end
    end

    def all
      terms.map do |res|
        { :id => res[:id], :label => res[:term] }.with_indifferent_access
      end
    end

    def find(id)
      terms.find { |term| term[:id] == id } || {}
    end

    private

    def terms
      subauthority_hash = YAML.load(File.read(subauthority_filename))
      terms = subauthority_hash.with_indifferent_access.fetch(:terms, [])
      normalize_terms(terms)
    end

    def subauthority_filename
      File.join(Local.subauthorities_path, "#{subauthority}.yml")
    end

    def normalize_terms(terms)
      terms.map do |term|
        if term.is_a? String
          { :id => term, :term => term }.with_indifferent_access
        else
          term[:id] ||= term[:term]
          term
        end
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
qa-0.10.1 lib/qa/authorities/local/file_based_authority.rb
qa-0.10.0 lib/qa/authorities/local/file_based_authority.rb
qa-0.9.0 lib/qa/authorities/local/file_based_authority.rb
qa-0.8.0 lib/qa/authorities/local/file_based_authority.rb
qa-0.7.0 lib/qa/authorities/local/file_based_authority.rb
qa-0.6.0 lib/qa/authorities/local/file_based_authority.rb
qa-0.5.0 lib/qa/authorities/local/file_based_authority.rb