module Ginko::NameQuery
Public Instance Methods
search(query, options={})
click to toggle source
# File lib/ginko/name_query.rb, line 4 def search(query, options={}) limit = options[:limit] || 10 if query.nil? || query.empty? # parse after limiting number of items for performance reason parse(data.take(limit)) else result[query] if cached?(query) parse(update(query)).take(limit) end end
Private Instance Methods
cached?(query)
click to toggle source
# File lib/ginko/name_query.rb, line 34 def cached?(query) result[query].nil? end
data()
click to toggle source
# File lib/ginko/name_query.rb, line 59 def data raise 'Called abstract method' end
filter_with_query(data, query)
click to toggle source
# File lib/ginko/name_query.rb, line 17 def filter_with_query(data, query) data if query.nil? || query.empty? query = /\A#{query}/ filtered = data.select do |item| item.fetch('name').match(query) || item.fetch('name_h').match(query) || item.fetch('name_k').match(query) || item.fetch('name_e').match(query) end end
item_klass()
click to toggle source
# File lib/ginko/name_query.rb, line 51 def item_klass raise 'Called abstract method' end
parse(data)
click to toggle source
# File lib/ginko/name_query.rb, line 28 def parse(data) data.map do |item| item.is_a?(item_klass) ? item : item_klass.new(item) end end
result()
click to toggle source
# File lib/ginko/name_query.rb, line 55 def result raise 'Called abstract method' end
scope(query)
click to toggle source
# File lib/ginko/name_query.rb, line 38 def scope(query) matched_query = result.keys.select do |key| query.include?(key.to_s) end.sort.last return data if matched_query.nil? || matched_query.empty? @result[matched_query] end
update(query)
click to toggle source
# File lib/ginko/name_query.rb, line 46 def update(query) scoped = scope(query) result[query] = filter_with_query(scoped, query) end