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

find( search_term, where = '' ) click to toggle source
#

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
new( search_for_this_line = nil, search_in_this_file = nil, run_already = true ) click to toggle source
#

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

can_we_continue?() click to toggle source
#

can_we_continue?

#
# File lib/find_line_in_file/find_line_in_file.rb, line 102
def can_we_continue?
  @can_we_continue
end
check_whether_the_file_exists() click to toggle source
#

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
clear_dataset() click to toggle source
#

clear_dataset

#
# File lib/find_line_in_file/find_line_in_file.rb, line 95
def clear_dataset # No more need for @data to contain anything.
  @data = nil; remove_instance_variable(:@data)
end
ewarn(i) click to toggle source
#

ewarn

#
# File lib/find_line_in_file/find_line_in_file.rb, line 116
def ewarn(i)
  ::Colours.ewarn(i)
end
file?()
find_input_in_dataset() click to toggle source
#

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
line_number?()
Alias for: number?
number?() click to toggle source
#

number?

#
# File lib/find_line_in_file/find_line_in_file.rb, line 164
def number?
  @result
end
Also aliased as: line_number?
read_in_dataset() click to toggle source
#

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()
Alias for: report_result
report_result() click to toggle source
#

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
Also aliased as: report
reset() click to toggle source
#

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()
Alias for: result?
result?() click to toggle source
#

result?

#
# File lib/find_line_in_file/find_line_in_file.rb, line 171
def result?
  @result
end
Also aliased as: result
run() click to toggle source
#

run (run tag)

#
# File lib/find_line_in_file/find_line_in_file.rb, line 239
def run
  check_whether_the_file_exists
  if can_we_continue?
    read_in_dataset
    find_input_in_dataset
    clear_dataset
    return @result
  end
end
search_for(i = nil)
search_for_which_file?() click to toggle source
#

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
Also aliased as: file?
set_search_for_this_line(i = nil) click to toggle source
#

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
Also aliased as: search_for
set_search_in_this_file(i = nil) click to toggle source
#

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
Also aliased as: set_this_file, use_this_file
set_this_file(i = nil)
sfile(i) click to toggle source
#

sfile

#
# File lib/find_line_in_file/find_line_in_file.rb, line 109
def sfile(i)
  ::Colours.sfile(i)
end
simp(i) click to toggle source
#

simp

#
# File lib/find_line_in_file/find_line_in_file.rb, line 123
def simp(i)
  ::Colours.simp(i)
end
swarn(i) click to toggle source
#

swarn

#
# File lib/find_line_in_file/find_line_in_file.rb, line 130
def swarn(i)
  ::Colours.swarn(i)
end
use_this_file(i = nil)