class Milkode::Query

Constants

OPTIONS

Attributes

query_string[R]

Public Class Methods

new(str) click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 24
def initialize(str)
  @query_string = str
  init_hash
  parse
  @wide_match_range = calc_param(6)
end

Public Instance Methods

conv_fuzzy_gotoline() click to toggle source

'cdstk.rb:11' -> 'g:cdstk.rb:11'

# File lib/milkode/cdweb/lib/query.rb, line 130
def conv_fuzzy_gotoline
  s = query_string.split.map {|v|
    if keywords[0].include? v
      "g:#{v}"
    else
      v
    end
  }.join(' ')

  Query.new(s)
end
conv_head_keyword_to_fpath_or_packages() click to toggle source

'name def test' -> 'fp:name def test'

# File lib/milkode/cdweb/lib/query.rb, line 117
def conv_head_keyword_to_fpath_or_packages
  s = query_string.split.map {|v|
    if keywords[0].include? v
      "fp:#{v}"
    else
      v
    end
  }.join(' ')

  Query.new(s)
end
conv_keywords_to_fpath() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 92
def conv_keywords_to_fpath
  s = query_string.split.map {|v|
    if keywords.include? v
      "f:#{v}"
    else
      v
    end
  }.join(' ')

  Query.new(s)
end
conv_keywords_to_fpath_or_packages() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 104
def conv_keywords_to_fpath_or_packages
  s = query_string.split.map {|v|
    if keywords.include? v
      "fp:#{v}"
    else
      v
    end
  }.join(' ')

  Query.new(s)
end
conv_wide_match_range(match_range) click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 142
def conv_wide_match_range(match_range)
  Query.new(query_string + " w:#{match_range}")
end
empty?() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 35
def empty?
  keywords.size == 0 && only_keywords
end
escape_html() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 31
def escape_html
  Rack::Utils::escape_html(@query_string)
end
fpath_or_packages() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 59
def fpath_or_packages
  calc_param(3)
end
fpaths() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 51
def fpaths
  calc_param(1)
end
gotolines() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 68
def gotolines
  calc_param(5)
end
keywords() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 43
def keywords
  @hash['keywords']
end
multi_match_keywords() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 63
def multi_match_keywords
  # 本当はkeywordsにしたかった・・
  calc_param(4)
end
only_keywords() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 39
def only_keywords
  packages.size == 0 && fpaths.size == 0 && suffixs.size == 0 && fpath_or_packages.size == 0 && gotolines.size == 0 && wide_match_range_empty?
end
packages() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 47
def packages
  calc_param(0)
end
suffixs() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 55
def suffixs
  calc_param(2)
end
wide_match_range() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 72
def wide_match_range
  a = @wide_match_range

  if a.empty?
    1
  else
    i = a[-1].to_i

    if (i == 0)
      0
    else
      i
    end
  end
end
wide_match_range_empty?() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 88
def wide_match_range_empty?
  @wide_match_range.empty?
end

Private Instance Methods

calc_param(index) click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 148
def calc_param(index)
  OPTIONS[index].inject([]){|result, item| result.concat @hash[item] }
end
init_hash() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 152
def init_hash
  @hash = {}
  @hash['keywords'] = []

  OPTIONS.flatten.each do |key|
    @hash[key] = []
  end
end
parse() click to toggle source
# File lib/milkode/cdweb/lib/query.rb, line 161
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