Sha256: 37ff73e1f29c43890e3431fa9be706d5b691bf8b16c941d17df17cbc69bef592

Contents?: true

Size: 1000 Bytes

Versions: 2

Compression:

Stored size: 1000 Bytes

Contents

module SearchyJson
  class Query
    attr_reader :text

    NEGATIVE = /\A-/.freeze
    START_EXTRACT = /\A("|')/.freeze
    END_EXTRACT = /("|')\Z/.freeze

    def initialize(text)
      @text = text
    end

    def negative_keywords
      keywords_by_type[:negative]
    end

    def positive_keywords
      keywords_by_type[:positive]
    end

    def exact_matches?
      @exact_matches ||= !(text =~ START_EXTRACT && text =~ END_EXTRACT).nil?
    end

    private

    def keywords_by_type
      @keywords_by_type ||= exact_keywords_by_type
    end

    def exact_keywords_by_type
      negative = []
      positive = []

      keywords.each do |word|
        negative?(word) ? negative.push(word.gsub(NEGATIVE, '')) : positive.push(word)
      end

      { negative: negative, positive: positive }
    end

    def negative?(keyword)
      keyword =~ NEGATIVE
    end

    def keywords
      @keywords ||= text.gsub(START_EXTRACT, '').gsub(END_EXTRACT, '').upcase.split(' ')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
searchy-json-0.3.1 lib/searchy/query.rb
searchy-json-0.3.0 lib/searchy/query.rb