Class: Searrrch
- Inherits:
-
Object
- Object
- Searrrch
- Defined in:
- lib/searrrch.rb
Overview
This defines a really simple API for interfacing with search operations.
This is indeed simple… I wanted it to be simple… so I made it simple. So.. SIMPLE!
Constant Summary collapse
- OPERATOR_EXPRESSION =
/(\w+):[\ ]?([\w\p{Han}\p{Katakana}\p{Hiragana}\p{Hangul}ー,]+|(["'])(\\?.)*?\3)/
- VERSION =
1 2 3 About this regexp:
1. looks for word character (english word basically, plus _ and numbers - might catch others) the : must be right after.. or else fails.... and after : might have one space (en or ja) 2. then look for word characters.. supporting Japanese, Korean, Chinese and latin alphabet plus numbers and such also support ',' for you cool kids that expect something like a "list of ids" 3. and also accept any char if quoted - in which case the same quotation should be quoted as well
'0.0.5'
Instance Method Summary collapse
-
#as_array(key, expects = :string) {|arr| ... } ⇒ Object
Same as to_array, but yield the value of the array to a block if a value is found.
-
#each_value(key, expects = :string) ⇒ Object
yield the value of the given operator to each of the contained elements.
- #freetext(expects = :string) ⇒ Object
-
#initialize(query, explode_comma = false) ⇒ Searrrch
constructor
iterates over the entire string identifying each of the elements this code only checks for: 1.
-
#to_array(key, expects = :string) ⇒ Object
yield the block if there is data in the given block.
Constructor Details
#initialize(query, explode_comma = false) ⇒ Searrrch
iterates over the entire string identifying each of the elements this code only checks for:
1. simple spaces
2. japanese spaces
3. : char
All other chars are treated as normal char.
Both key and value must have only the other regular chars.
Everything after the last option will be considered free text search
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/searrrch.rb', line 28 def initialize(query, explode_comma=false) query = query.to_s @operators = {} offset = 0 while m = OPERATOR_EXPRESSION.match(query, offset) key = m[1].downcase.to_sym value = m[2] value = value[1, value.length - 2] if ["'", '"'].include?(value[0]) offset = m.end(2) @operators[key] ||= [] if explode_comma value.split(',').each{ |v| @operators[key] << v } else @operators[key] << value end end @freetext = query[offset, query.length].strip end |
Instance Method Details
#as_array(key, expects = :string) {|arr| ... } ⇒ Object
Same as to_array, but yield the value of the array to a block if a value is found
66 67 68 69 |
# File 'lib/searrrch.rb', line 66 def as_array(key, expects = :string) arr =to_array(key, expects) yield arr if arr.length > 0 end |
#each_value(key, expects = :string) ⇒ Object
yield the value of the given operator to each of the contained elements.
50 51 52 53 54 55 |
# File 'lib/searrrch.rb', line 50 def each_value(key, expects = :string) return if @operators[key.to_sym].nil? @operators[key.to_sym].each do |value| yield(convert(value, expects)) end end |
#freetext(expects = :string) ⇒ Object
71 72 73 |
# File 'lib/searrrch.rb', line 71 def freetext(expects = :string) convert(@freetext, expects) end |
#to_array(key, expects = :string) ⇒ Object
yield the block if there is data in the given block
58 59 60 61 62 |
# File 'lib/searrrch.rb', line 58 def to_array(key, expects = :string) res = [] each_value(key, expects) { |v| res << v } res end |