class FindLineInFile
#¶ ↑
require 'find_line_in_file/class_methods.rb' FindLineInFile.find
[search_term: 'foo', where: '/this/file.rb']
#¶ ↑
#¶ ↑
require 'find_line_in_file/constants.rb'
#¶ ↑
#¶ ↑
Constants
- DEFAULT_FILE
#¶ ↑
DEFAULT_FILE
¶ ↑#¶ ↑
- ENCODING_ISO
#¶ ↑
ENCODING_ISO
¶ ↑#¶ ↑
- ENCODING_UTF
#¶ ↑
ENCODING_UTF
¶ ↑#¶ ↑
- LAST_UPDATE
#¶ ↑
FindLineInFile::LAST_UPDATE
¶ ↑#¶ ↑
- MAIN_ENCODING
#¶ ↑
MAIN_ENCODING
¶ ↑#¶ ↑
- NAME
#¶ ↑
NAME
¶ ↑#¶ ↑
- THIS_LINE
#¶ ↑
THIS_LINE
¶ ↑#¶ ↑
- USE_THIS_ENCODING
#¶ ↑
USE_THIS_ENCODING
¶ ↑The class will default to UTF-8, unless specified otherwise. Thus, this is the default encoding to be used.
#¶ ↑
- VERSION
#¶ ↑
FindLineInFile::VERSION
¶ ↑#¶ ↑
Public Class Methods
#¶ ↑
FindLineInFile.find
¶ ↑
The first argument to this method should be the search string in question, that is, the line you want to find exactly.
The second argument should ideally be a Hash but it can also be a String, hence the check below in the method body. It will tell us the file location.
If the line has been found then this method will return an Integer number, aka the fileline - starting at line number 1. (There is, logically, no line number called 0, hence why the code here behaves in that way.)
Usage examples:
FindLineInFile.find(search_term, :in => 'test.txt') FindLineInFile[search_term: 'foo', where: '/this/file.rb']
#¶ ↑
# File lib/find_line_in_file/class_methods.rb, line 31 def self.find( search_term, where = '' ) if search_term.is_a? Hash # ===================================================================== # # === :where # ===================================================================== # if search_term.has_key? :where if where.is_a?(String) and where.empty? where = search_term.delete(:where) end end # ===================================================================== # # === :what # ===================================================================== # if search_term.has_key? :what if search_term.has_key? :in where = search_term.delete :in elsif search_term.has_key? :where where = search_term.delete :where end search_term = search_term.delete :what end # ===================================================================== # # === :search_term # # This should come last. # ===================================================================== # if search_term.has_key? :search_term search_term = search_term.delete(:search_term) end end if where.is_a? Hash if where.has_key? :in where = where.delete :in elsif where.has_key? :where where = where.delete :where end elsif where.is_a? String # Handle given Strings next. where = where.to_s.dup end # ======================================================================= # # `where` may still be a Hash at this point, so # we convert it into a string. # ======================================================================= # if where.is_a? Hash and where.empty? where = '' end _ = FindLineInFile.new(search_term, where) return _.result # We will return a Fixnum here, or nil otherwise. end
#¶ ↑
initialize¶ ↑
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 43 def initialize( search_for_this_line = nil, search_in_this_file = nil, run_already = true ) reset # ======================================================================= # # The second one has to come first, because the first argument may # sometimes be a Hash. If it is a Hash then it may overrule the @file # variable, so that is why the order is reverse. # ======================================================================= # set_search_in_this_file( search_in_this_file # This file is grepped. ) set_search_for_this_line( search_for_this_line # This line is sought. ) run if run_already end
Public Instance Methods
#¶ ↑
check_whether_the_file_exists
¶ ↑
We find out whether our target file exists or whether it does not.
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 207 def check_whether_the_file_exists search_for_this_file = search_for_which_file? unless File.exist? search_for_this_file opn; ewarn 'The file `'+sfile(search_for_this_file)+ swarn('` does not exist.') @can_we_continue = false end end
#¶ ↑
find_input_in_dataset
¶ ↑
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 219 def find_input_in_dataset # ======================================================================= # # Must add +1 because files start at 1, whereas Arrays start at entry 0. # ======================================================================= # @result = @data.find_index {|entry| entry.include? @search_for_this_line.to_s } if @result @result += 1 else opn; e "We may have not found anything for: `#{@search_for_this_line}`" pp @search_for_this_line opn; e 'Its encoding was: '+ simp(@search_for_this_line.to_s.encoding.to_s) end end
#¶ ↑
number?¶ ↑
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 164 def number? @result end
#¶ ↑
read_in_dataset
¶ ↑
This here makes use of File.readlines(), so the Encoding must be checked.
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 154 def read_in_dataset @data = File.readlines( search_for_which_file?, encoding: @use_this_encoding # We must specify our default encoding to use. ) end
#¶ ↑
report_result
¶ ↑
We only report if we have found something.
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 139 def report_result if can_we_continue? opn; e "We will try to find something in "\ "#{sfile(search_for_which_file?)} now." opn; e 'The variable @result (of class '+@result.class.to_s+') '+ 'is: '+simp(@result.to_s) end end
#¶ ↑
reset (reset tag)¶ ↑
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 66 def reset # ======================================================================= # # === @data # ======================================================================= # @data = nil # ======================================================================= # # === @result # ======================================================================= # @result = nil # ======================================================================= # # === @can_we_continue # ======================================================================= # @can_we_continue = true # ======================================================================= # # === @use_this_encoding # ======================================================================= # @use_this_encoding = USE_THIS_ENCODING end
#¶ ↑
result?¶ ↑
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 171 def result? @result end
#¶ ↑
search_for_which_file?¶ ↑
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 88 def search_for_which_file? @search_for_this_file end
#¶ ↑
set_search_for_this_line
¶ ↑
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 188 def set_search_for_this_line(i = nil) i = THIS_LINE if i.nil? if i.is_a? Hash if i.has_key? :this_file set_this_file(i.delete(:this_file)) end if i.has_key? :this_line i = i.delete(:this_line) end end i = i.to_s @search_for_this_line = i end
#¶ ↑
set_search_in_this_file
¶ ↑
#¶ ↑
# File lib/find_line_in_file/find_line_in_file.rb, line 178 def set_search_in_this_file(i = nil) i = DEFAULT_FILE if i.nil? i = i.first if i.is_a? Array @search_for_this_file = i end