Class: Cheatorious::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/cheatorious/search.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cheat_model) ⇒ Search

Returns a new instance of Search.



11
12
13
14
# File 'lib/cheatorious/search.rb', line 11

def initialize(cheat_model)
  cheat_model  = Utils.deserialize(cheat_model, Utils.serialization_type(cheat_model)) if cheat_model.kind_of?(String)
  @cheat_model = cheat_model
end

Class Method Details

.execute(cheat_model, query = "", writer = Writer::Text, options = {}) ⇒ Object



5
6
7
8
# File 'lib/cheatorious/search.rb', line 5

def execute(cheat_model, query = "", writer = Writer::Text, options = {})
  s = self.new(cheat_model)
  s.execute(query, writer, options)
end

Instance Method Details

#execute(query = "", writer = Writer::Text, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cheatorious/search.rb', line 16

def execute(query = "", writer = Writer::Text, options = {})
  info = @cheat_model[:info]
  options.delete("reverse") if options["section"] && options["reverse"]
  
  # Filtering
  filtered = @cheat_model[:cheatsheet][:root].dup
  unless print_full?(query)
    filtered, results_count = depth_search(query, filtered, options)
  end
  
  # Writing
  w = writer.new
  print_full?(query) ? w.header(info[:name], info[:author], info[:version], info[:description]) : w.search_header(query, results_count, options)
  write_contents(filtered, w, options)
  w.footer 
  
  return w.result
end