class PrettySearch::MemoryCollection
PrettySearch::MemoryCollection
parses the given data_file into hash and keep everything in memory, and iterates through the records naively from beginning to end when searching
Public Class Methods
new(data_file, first: false)
click to toggle source
# File lib/pretty_search/collection/memory_collection.rb, line 7 def initialize(data_file, first: false) @data_file = data_file @first = first end
Public Instance Methods
search(query)
click to toggle source
@return [Array<PrettySearch::Document>]
# File lib/pretty_search/collection/memory_collection.rb, line 13 def search(query) data = Yajl::Parser.parse(File.new(@data_file)) if @first found = data.detect { |doc| query.match(doc) } if found Array(PrettySearch::Document.new(found)) else [] end else data.select { |doc| query.match(doc) } .map { |doc| PrettySearch::Document.new doc } end end