Class: FindLineInFile

Inherits:
Object
  • Object
show all
Includes:
Colours::E
Defined in:
lib/find_line_in_file/constants.rb,
lib/find_line_in_file/class_methods.rb,
lib/find_line_in_file/version/version.rb,
lib/find_line_in_file/find_line_in_file.rb

Overview

FindLineInFile

Constant Summary collapse

DEFAULT_FILE =
#

DEFAULT_FILE

#
'/home/x/DATA/PROGRAMMING_LANGUAGES/RUBY/src/'\
'find_line_in_file/test/testing_find_line_in_file.rb'
THIS_LINE =
#

THIS_LINE

#
''
NAME =
#

NAME

#
'FindLineInFile: '
ENCODING_ISO =
#

ENCODING_ISO

#
'ISO-8859-1'
ENCODING_UTF =
#

ENCODING_UTF

#
'UTF-8'
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.

#
ENCODING_UTF
MAIN_ENCODING =
#

MAIN_ENCODING

#
USE_THIS_ENCODING
VERSION =
#

FindLineInFile::VERSION

#
'1.0.18'
LAST_UPDATE =
#

FindLineInFile::LAST_UPDATE

#
'28.02.2020'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_for_this_line = nil, search_in_this_file = nil, run_already = true) ⇒ FindLineInFile

#

initialize

#


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

Class Method Details

.find(search_term, where = '') ⇒ Object

#

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']
#


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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

Instance Method Details

#can_we_continue?Boolean

#

can_we_continue?

#

Returns:

  • (Boolean)


102
103
104
# 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_existsObject

#

check_whether_the_file_exists

We find out whether our target file exists or whether it does not.

#


207
208
209
210
211
212
213
214
# 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_datasetObject

#

clear_dataset

#


95
96
97
# 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) ⇒ Object

#

ewarn

#


116
117
118
# File 'lib/find_line_in_file/find_line_in_file.rb', line 116

def ewarn(i)
  ::Colours.ewarn(i)
end

#find_input_in_datasetObject

#

find_input_in_dataset

#


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# 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?Boolean Also known as: line_number?

#

number?

#

Returns:

  • (Boolean)


164
165
166
# File 'lib/find_line_in_file/find_line_in_file.rb', line 164

def number?
  @result
end

#read_in_datasetObject

#

read_in_dataset

This here makes use of File.readlines(), so the Encoding must be checked.

#


154
155
156
157
158
159
# 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_resultObject Also known as: report

#

report_result

We only report if we have found something.

#


139
140
141
142
143
144
145
146
# 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

#resetObject

#

reset (reset tag)

#


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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?Boolean Also known as: result

#

result?

#

Returns:

  • (Boolean)


171
172
173
# File 'lib/find_line_in_file/find_line_in_file.rb', line 171

def result?
  @result
end

#runObject

#

run (run tag)

#


239
240
241
242
243
244
245
246
247
# 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_which_file?Boolean Also known as: file?

#

search_for_which_file?

#

Returns:

  • (Boolean)


88
89
90
# 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(i = nil) ⇒ Object Also known as: search_for

#

set_search_for_this_line

#


188
189
190
191
192
193
194
195
196
197
198
199
200
# 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(i = nil) ⇒ Object Also known as: set_this_file, use_this_file

#

set_search_in_this_file

#


178
179
180
181
182
# 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

#sfile(i) ⇒ Object

#

sfile

#


109
110
111
# File 'lib/find_line_in_file/find_line_in_file.rb', line 109

def sfile(i)
  ::Colours.sfile(i)
end

#simp(i) ⇒ Object

#

simp

#


123
124
125
# File 'lib/find_line_in_file/find_line_in_file.rb', line 123

def simp(i)
  ::Colours.simp(i)
end

#swarn(i) ⇒ Object

#

swarn

#


130
131
132
# File 'lib/find_line_in_file/find_line_in_file.rb', line 130

def swarn(i)
  ::Colours.swarn(i)
end