Sha256: 4c1660b26b73fcbe215298b46f1b12f464cdcbeb512dde96c8d8a25ffe6279d2

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Qa::Authorities
  # This is really just a sub-authority for Local
  class Subauthority
    attr_reader :name
    def initialize(name)
      @name = name
    end

    def terms
      @terms ||= load_sub_authority_terms
    end

    def search(q)
      r = q.blank? ? terms : 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

    class << self
      def sub_authorities_path
        config_path = AUTHORITIES_CONFIG[:local_path]
        if config_path.starts_with?(File::Separator)
          return config_path
        end
        File.join(Rails.root, config_path)
      end

      def names
        @sub_auth_names ||=
          begin
            sub_auths = []
            Dir.foreach(sub_authorities_path) { |file| sub_auths << File.basename(file, File.extname(file)) }
            sub_auths
          end
      end
    end

    private
      def load_sub_authority_terms
        sub_authority_hash = YAML.load(File.read(sub_authority_filename))
        terms = sub_authority_hash.with_indifferent_access.fetch(:terms, [])
        normalize_terms(terms)
      end

      def sub_authority_filename
        File.join(self.class.sub_authorities_path, "#{name}.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

1 entries across 1 versions & 1 rubygems

Version Path
qa-0.0.2 lib/qa/authorities/local/subauthority.rb