Sha256: a2188f522b2d2f127f03c380ec54e3667868a3e04f46afd758d30eb57f235391

Contents?: true

Size: 958 Bytes

Versions: 18

Compression:

Stored size: 958 Bytes

Contents

# frozen_string_literal: true
module Thredded
  class SearchParser
    def initialize(query)
      @query = query
      @keywords = %w(in by order)
    end

    def parse
      parsed_input = parse_keywords
      parsed_input['text'] = parse_text
      parsed_input
    end

    def parse_keywords
      found_terms_hash = {}

      @keywords.each do |keyword|
        regex = Regexp.new(keyword + '\s*:\s*\w+', Regexp::IGNORECASE)
        keyword_scan = @query.scan(regex)
        @query = @query.gsub(regex, '')

        next unless keyword_scan.present?
        keyword_scan.each do |term|
          found_terms_hash[keyword] ||= []
          found_terms_hash[keyword] << term.delete(' ').split(':')[1]
        end
      end

      found_terms_hash
    end

    def parse_text
      regex = Regexp.new('\"[^"]*\"')
      found_terms = @query.scan(regex)
      @query = @query.sub(regex, '')
      found_terms.concat(@query.split(/\s+/))
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
thredded-0.12.1 app/models/concerns/thredded/search_parser.rb
thredded-0.12.0 app/models/concerns/thredded/search_parser.rb
thredded-0.11.1 app/models/concerns/thredded/search_parser.rb
thredded-0.11.0 app/models/concerns/thredded/search_parser.rb
thredded-0.10.1 app/models/concerns/thredded/search_parser.rb
thredded-0.10.0 app/models/concerns/thredded/search_parser.rb
thredded-0.9.4 app/models/concerns/thredded/search_parser.rb
thredded-0.9.3 app/models/concerns/thredded/search_parser.rb
thredded-0.9.2 app/models/concerns/thredded/search_parser.rb
thredded-0.9.1 app/models/concerns/thredded/search_parser.rb
thredded-0.8.4 lib/thredded/search_parser.rb
thredded-0.8.2 lib/thredded/search_parser.rb
thredded-0.7.0 lib/thredded/search_parser.rb
thredded-0.6.3 lib/thredded/search_parser.rb
thredded-0.6.2 lib/thredded/search_parser.rb
thredded-0.6.1 lib/thredded/search_parser.rb
thredded-0.6.0 lib/thredded/search_parser.rb
thredded-0.5.1 lib/thredded/search_parser.rb