module SearchInFile

Constants

VERSION

Public Class Methods

content_of(file) click to toggle source
# File lib/search_in_file.rb, line 34
def self.content_of file
  class_name   = "#{extname( file )[1..-1].capitalize}Parser"
  parser_class = Object.const_get( class_name )
  file_content = parser_class.new.read_file( file )
end
each_file_in(d_path) { |f| ... } click to toggle source
# File lib/search_in_file.rb, line 56
def self.each_file_in d_path
  Find.find( d_path ){ |f| yield( f ) if is_document?( f ) && block_given? }
end
extname(file) click to toggle source
# File lib/search_in_file.rb, line 60
def self.extname file
  File.extname( file )
end
extname?(file, type) click to toggle source
# File lib/search_in_file.rb, line 64
def self.extname? file, type
  extname( file ) == type
end
find_all_in(d_path) click to toggle source
# File lib/search_in_file.rb, line 50
def self.find_all_in d_path
  f_paths = []
  Find.find( d_path ){ |f| f_paths << f if is_document?( f ) }
  f_paths
end
find_by_type_in(d_path, f_type) click to toggle source
# File lib/search_in_file.rb, line 44
def self.find_by_type_in d_path, f_type
  f_paths = []
  Find.find( d_path ){ |f| f_paths << f if extname?( f, f_type ) }
  f_paths
end
is_document?(f_name) click to toggle source
# File lib/search_in_file.rb, line 68
def self.is_document? f_name
  Settings.supported_docs.include?( extname(f_name) )
end
paragraphs_of(file) click to toggle source
# File lib/search_in_file.rb, line 40
def self.paragraphs_of file
  content_of( file ).split(/\r/)
end
search_in_directory( path, term ) click to toggle source
# File lib/search_in_file.rb, line 17
def self.search_in_directory( path, term )
  results = []
  each_file_in( path ) do |f_path| 
    f_result = search_in_file( f_path, term )
    results = results + f_result if !f_result.empty?
  end
  results
end
search_in_file( f_path, term ) click to toggle source
# File lib/search_in_file.rb, line 26
def self.search_in_file( f_path, term )
  term_paragraphs = []
  file_paragraphs = paragraphs_of( f_path )
  # search for phrase
  file_paragraphs.each{ |p| term_paragraphs << p if p.include?(term) }
  term_paragraphs.empty? ? [] : [{file: f_path, paragraphs: term_paragraphs}]    
end