Sha256: 4476cc091a772290d0031a1324c14c31a9d48e9d75900db9ad76d46a196b023d

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Qa::Authorities
  class Local < Base

    include Qa::Authorities::LocalSubauthority

    def initialize *args
      super
      @sub_authority ||= args.first
      raise "No sub-authority provided" if sub_authority.nil?
    end

    def sub_authorities
      names
    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
      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(sub_authorities_path, "#{sub_authority}.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

3 entries across 3 versions & 1 rubygems

Version Path
qa-0.4.2 lib/qa/authorities/local.rb
qa-0.4.1 lib/qa/authorities/local.rb
qa-0.4.0 lib/qa/authorities/local.rb