Sha256: e1da75fdf26faeffebf4a457c12a9e3cbd6f2b21a31f18738dc0a80589ef3b19

Contents?: true

Size: 1.32 KB

Versions: 28

Compression:

Stored size: 1.32 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], active: res.fetch(:active, true) }.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)) # rubocop:disable Security/YAMLLoad # TODO: Explore how to change this to safe_load.  Many tests fail when making this change.
        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

28 entries across 28 versions & 1 rubygems

Version Path
qa-5.6.0 lib/qa/authorities/local/file_based_authority.rb
qa-5.5.2 lib/qa/authorities/local/file_based_authority.rb
qa-4.3.0 lib/qa/authorities/local/file_based_authority.rb
qa-3.1.0 lib/qa/authorities/local/file_based_authority.rb
qa-2.3.0 lib/qa/authorities/local/file_based_authority.rb
qa-5.5.1 lib/qa/authorities/local/file_based_authority.rb
qa-5.5.0 lib/qa/authorities/local/file_based_authority.rb
qa-5.4.0 lib/qa/authorities/local/file_based_authority.rb
qa-5.3.1 lib/qa/authorities/local/file_based_authority.rb
qa-5.3.0 lib/qa/authorities/local/file_based_authority.rb
qa-5.2.0 lib/qa/authorities/local/file_based_authority.rb
qa-5.1.0 lib/qa/authorities/local/file_based_authority.rb
qa-5.0.0 lib/qa/authorities/local/file_based_authority.rb
qa-4.2.4 lib/qa/authorities/local/file_based_authority.rb
qa-4.2.3 lib/qa/authorities/local/file_based_authority.rb
qa-4.2.2 lib/qa/authorities/local/file_based_authority.rb
qa-4.2.1 lib/qa/authorities/local/file_based_authority.rb
qa-4.2.0 lib/qa/authorities/local/file_based_authority.rb
qa-4.1.1 lib/qa/authorities/local/file_based_authority.rb
qa-4.1.0 lib/qa/authorities/local/file_based_authority.rb