Sha256: cd553cf16e1a3c664de9fa4fb8727b4921be6cefc35c1392cb9214d08792dc06

Contents?: true

Size: 1.38 KB

Versions: 29

Compression:

Stored size: 1.38 KB

Contents

# -*- coding: utf-8 -*-
#
# @file 
# @brief  クエリーの解析
# @author ongaeshi
# @date   2010/10/21

require 'rubygems'
require 'rack'

module Milkode
  class Query
    attr_reader :query_string

    OPTIONS = [
               ['package',  'p'],
               ['filepath', 'fpath', 'f'],
               ['suffix',   's'],
              ]

    def initialize(str)
      @query_string = str
      init_hash
      parse
    end

    def escape_html
      Rack::Utils::escape_html(@query_string)
    end

    def empty?
      keywords.size == 0 && packages.size == 0 && fpaths.size == 0 && suffixs.size == 0
    end

    def keywords
      @hash['keywords']
    end

    def packages
      calc_param(0)
    end

    def fpaths
      calc_param(1)
    end

    def suffixs
      calc_param(2)
    end

    private

    def calc_param(index)
      OPTIONS[index].inject([]){|result, item| result.concat @hash[item] }
    end

    def init_hash
      @hash = {}
      @hash['keywords'] = []

      OPTIONS.flatten.each do |key|
        @hash[key] = []
      end
    end

    def parse
      kp = OPTIONS.flatten.join('|')
      parts = @query_string.scan(/(?:(#{kp}):)?(?:"(.+)"|(\S+))/)

      parts.each do |key, quoted_value, value|
        text = quoted_value || value
        unless (key)
          @hash['keywords'] << text
        else
          @hash[key] << text
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
milkode-0.8.1 lib/milkode/cdweb/lib/query.rb
milkode-0.8.0 lib/milkode/cdweb/lib/query.rb
milkode-0.7.1 lib/milkode/cdweb/lib/query.rb
milkode-0.7.0 lib/milkode/cdweb/lib/query.rb
milkode-0.6.3 lib/milkode/cdweb/lib/query.rb
milkode-0.6.2 lib/milkode/cdweb/lib/query.rb
milkode-0.6.1 lib/milkode/cdweb/lib/query.rb
milkode-0.6.0 lib/milkode/cdweb/lib/query.rb
milkode-0.5.3 lib/milkode/cdweb/lib/query.rb
milkode-0.5.2 lib/milkode/cdweb/lib/query.rb
milkode-0.5.1 lib/milkode/cdweb/lib/query.rb
milkode-0.5.0 lib/milkode/cdweb/lib/query.rb
milkode-0.4.0 lib/milkode/cdweb/lib/query.rb
milkode-0.3.0 lib/milkode/cdweb/lib/query.rb
milkode-0.2.9 lib/milkode/cdweb/lib/query.rb
milkode-0.2.4 lib/milkode/cdweb/lib/query.rb
milkode-0.2.2 lib/milkode/cdweb/lib/query.rb
milkode-0.2.1 lib/milkode/cdweb/lib/query.rb
milkode-0.2.0 lib/milkode/cdweb/lib/query.rb
milkode-0.1.9 lib/milkode/cdweb/lib/query.rb