Sha256: 43753f4958f623407c934b0b37875a165a6cd3b2b6d3ab8bdc8a9567fbf0ad09

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

class SearchCop::HashParser
  attr_reader :query_info

  def initialize(query_info)
    @query_info = query_info
  end

  def parse(hash)
    res = hash.collect do |key, value|
      case key
        when :and
          value.collect { |val| parse val }.inject(:and)
        when :or
          value.collect { |val| parse val }.inject(:or)
        when :not
          parse(value).not
        when :query
          SearchCop::Parser.parse value, query_info
        else
          parse_attribute key, value
      end
    end

    res.inject :and
  end

  private

  def parse_attribute(key, value)
    collection = SearchCopGrammar::Attributes::Collection.new(query_info, key.to_s)

    if value.is_a?(Hash)
      raise(SearchCop::ParseError, "Unknown operator #{value.keys.first}") unless collection.valid_operator?(value.keys.first)

      if generator = collection.generator_for(value.keys.first)
        collection.generator generator, value.values.first
      else
        collection.send value.keys.first, value.values.first.to_s
      end
    else
      collection.send :matches, value.to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
search_cop-1.0.9 lib/search_cop/hash_parser.rb