Module: SearchDisplay

Extended by:
SearchDisplay
Included in:
SearchDisplay
Defined in:
app/models/utils/searchfile.rb

Instance Method Summary collapse

Instance Method Details

#check_for_title(line) ⇒ Object



5
6
7
# File 'app/models/utils/searchfile.rb', line 5

def check_for_title(line)
  line.match(/\*\*\*\* Snippet \d+:/)
end

#delete(file, ids) ⇒ Object



37
38
39
40
41
# File 'app/models/utils/searchfile.rb', line 37

def delete(file, ids)
  run(file).reject do |snip|
    ids.include?(snip.match(/\*\*\*\* Snippet (\d+):/).captures[0])
  end
end

#divide_snips(file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/utils/searchfile.rb', line 17

def divide_snips(file)
  snip_array = []
  snip_str = ""
  File.open(file, "r").each do |line|
    if check_for_title(line)
     snip_array << snip_str
    snip_str = line
   else
     snip_str << line
   end
  end
  snip_array << snip_str
  snip_array.shift
  snip_array
end

#run(file, text = "", ext = "") ⇒ Object



9
10
11
12
13
14
15
# File 'app/models/utils/searchfile.rb', line 9

def run(file,text="",ext="")
  if !search_snips(divide_snips(file), text, ext).empty?
     search_snips(divide_snips(file), text, ext)
  else
    ViewFormatter.no_results
  end
end

#search_snips(array, text, ext) ⇒ Object



33
34
35
# File 'app/models/utils/searchfile.rb', line 33

def search_snips(array,text,ext)
  array.select{ |snip| snip.include?(text) && snip.include?(ext)}
end