class Fias::Query::Finder

Public Class Methods

new(params, find) click to toggle source
# File lib/fias/query/finder.rb, line 4
def initialize(params, find)
  @params = params
  @find = find
end

Public Instance Methods

assumption() click to toggle source
# File lib/fias/query/finder.rb, line 9
def assumption
  find_endpoints
  return [] if @endpoints.blank?
  reject_inconsistent_chains
end

Private Instance Methods

endpoints_parents() click to toggle source
# File lib/fias/query/finder.rb, line 65
def endpoints_parents
  parents = @endpoints.values.slice(1..-1)
  return [] if parents.nil?
  parents
    .flatten
    .reverse
    .index_by { |endpoint| endpoint[:id] }
end
find(words) click to toggle source
# File lib/fias/query/finder.rb, line 32
def find(words)
  @find.call(words)
end
find_endpoint(key) click to toggle source
# File lib/fias/query/finder.rb, line 25
def find_endpoint(key)
  words = @params.split[key]
  endpoints = find(words)
  endpoints = reject_endpoints(endpoints, key)
  [key, endpoints]
end
find_endpoints() click to toggle source
# File lib/fias/query/finder.rb, line 17
def find_endpoints
  @endpoints = @params.split.keys.map do |key|
    find_endpoint(key)
  end
  @endpoints = Hash[@endpoints]
  inject_key_to_endpoints
end
inject_key_to_endpoints() click to toggle source
# File lib/fias/query/finder.rb, line 44
def inject_key_to_endpoints
  @endpoints.each do |key, endpoints|
    endpoints.each { |endpoint| endpoint[:key] = key }
  end
end
reject_endpoints(endpoints, key) click to toggle source
# File lib/fias/query/finder.rb, line 36
def reject_endpoints(endpoints, key)
  forms = @params.forms[key]

  endpoints.reject do |endpoint|
    (forms & endpoint[:forms]).blank?
  end
end
reject_inconsistent_chains() click to toggle source
# File lib/fias/query/finder.rb, line 50
def reject_inconsistent_chains
  starting_endpoints = @endpoints.values.first
  parents = endpoints_parents

  chains = starting_endpoints.map do |endpoint|
    overlaps = parents.keys & endpoint[:ancestry]

    if parents.blank? || overlaps.present?
      [endpoint] + endpoint[:ancestry].map { |id| parents[id] }.compact
    end
  end

  chains.compact
end