class SearchyJson::Extracter

Attributes

data[R]
exact_matches[R]
negative_keywords[R]
positive_keywords[R]
query[R]

Public Class Methods

new(data, query) click to toggle source
# File lib/searchy/extracter.rb, line 5
def initialize(data, query)
  @data = data
  @query = query
  @negative_keywords = query.negative_keywords
  @positive_keywords = query.positive_keywords
end

Public Instance Methods

call() click to toggle source
# File lib/searchy/extracter.rb, line 12
def call
  query.exact_matches? ? exact_extract : match_extract
end

Private Instance Methods

contain_negative?(negative_diff) click to toggle source
# File lib/searchy/extracter.rb, line 44
def contain_negative?(negative_diff)
  negative_diff != negative_keywords.count
end
contain_positive?(positive_diff) click to toggle source
# File lib/searchy/extracter.rb, line 48
def contain_positive?(positive_diff)
  positive_diff != positive_keywords.count
end
exact_extract() click to toggle source
# File lib/searchy/extracter.rb, line 34
def exact_extract
  result = []

  data.each do |primary_field, keywords|
    result << primary_field if full_positive_match?(keywords)
  end

  result
end
full_positive_match?(keywords) click to toggle source
# File lib/searchy/extracter.rb, line 52
def full_positive_match?(keywords)
  (keywords & positive_keywords).count == positive_keywords.count
end
keywords_diff_size(keywords, type) click to toggle source
# File lib/searchy/extracter.rb, line 56
def keywords_diff_size(keywords, type)
  (send("#{type}_keywords") - keywords).count
end
match_extract() click to toggle source
# File lib/searchy/extracter.rb, line 18
def match_extract
  result = {}

  data.each do |primary_field, keywords|
    negative_diff_size = keywords_diff_size(keywords, :negative)
    next if contain_negative?(negative_diff_size)

    positive_diff_size = keywords_diff_size(keywords, :positive)
    next unless contain_positive?(positive_diff_size)

    result[primary_field] = positive_diff_size
  end

  result.sort_by { |_key, index| index }.map(&:first)
end