class RBT::SedWrapper

Constants

DEFAULT_SED_COMMAND
#

DEFAULT_SED_COMMAND

#
REPORT_INPUT_COMMAND
#

REPORT_INPUT_COMMAND

#
SED_COMMAND2
#

SED_COMMAND2

#
SED_COMMAND3
#

SED_COMMAND3

#

Public Class Methods

[](i = '') click to toggle source
#

RBT::SedWrapper[]

#
# File lib/rbt/misc/sed_wrapper.rb, line 217
def self.[](i = '')
  new(i)
end
new( i = DEFAULT_SED_COMMAND, run_already = true ) click to toggle source
#

initialize

#
# File lib/rbt/misc/sed_wrapper.rb, line 52
def initialize(
    i           = DEFAULT_SED_COMMAND,
    run_already = true
  )
  reset
  set_input(i)
  run if run_already
end

Public Instance Methods

batch_modify_the_files() click to toggle source
#

batch_modify_the_files

#
# File lib/rbt/misc/sed_wrapper.rb, line 166
def batch_modify_the_files
  what = ''.dup # String to store.
  @input.delete!("'")
  if @input.start_with?('s/')
    @input[0,1] = ''
  end
  splitted     = @input.split('/').reject(&:empty?) # @input must obviously have a '/' character.
  replace_this = splitted.first
  with_that    = splitted[1]
  case with_that
  when 'd'
    # ===================================================================== #
    # This will be assumed to mean 'delete' but only in certain cases.
    # ===================================================================== #
    if @input.count('/') < 3
      with_that = '' # Chop it off in this case.
    end
  end
  work_on_these_files.each {|this_file|
    opne 'Working on the file `'+sfile(this_file)+'`.'
    try_to_read_in_this_file(this_file) # <- This sets @dataset again.
    if @dataset
      @dataset.each {|line|
        if line.include? replace_this
          line.gsub!(/#{replace_this}/, with_that)
        end
        # ================================================================= #
        # Do the substitute-action here.
        # ================================================================= #
        what << line
      }
      opne 'Storing into the file `'+sfile(this_file)+'` now. '\
           '(Substitution on: '+sfancy(replace_this)+'; '\
           'Replacement on: '+sfancy(with_that)+')'
      save_what_into(what, this_file)
      what = '' # Reset again.
    end
  }
end
determine_on_which_files_to_work() click to toggle source
#

determine_on_which_files_to_work

#
# File lib/rbt/misc/sed_wrapper.rb, line 118
def determine_on_which_files_to_work
  _ = @input # Grab a copy of the remaining input first.
  if _.include? "'"
    rindex = _.rindex "'"
    string_for_files = _[(rindex+1)..-1]
    set_work_on_these_files string_for_files.strip.split(' ').map(&:chomp)
    @input = _[0..rindex]
  end
end
input?() click to toggle source
#

input?

#
# File lib/rbt/misc/sed_wrapper.rb, line 111
def input?
  @input
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method RBT::Base#reset
# File lib/rbt/misc/sed_wrapper.rb, line 86
def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === :work_on_these_files
  # ======================================================================= #
  @internal_hash[:work_on_these_files] = []
  # ======================================================================= #
  # === @dataset
  # ======================================================================= #
  @dataset = nil
end
run() click to toggle source
#

run (run tag)

#
# File lib/rbt/misc/sed_wrapper.rb, line 209
def run
  determine_on_which_files_to_work
  batch_modify_the_files
end
sanitize_input() click to toggle source
#

sanitize_input

#
# File lib/rbt/misc/sed_wrapper.rb, line 102
def sanitize_input
  if @input.start_with? 'sed -i'
    @input.sub!(/^sed -i /,'')
  end
end
set_input( i = DEFAULT_SED_COMMAND ) click to toggle source
#

set_input

#
# File lib/rbt/misc/sed_wrapper.rb, line 64
def set_input(
    i = DEFAULT_SED_COMMAND
  )
  i = i.first if i.is_a? Array
  i = DEFAULT_SED_COMMAND if i.nil?
  i = i.to_s.dup
  case i # case tag
  when 'sed2','s2','2'
    i = SED_COMMAND2
  when 'sed3','s3','3'
    i = SED_COMMAND3
  end
  @input = i
  if REPORT_INPUT_COMMAND
    opne "The sed command used will be: #{sfancy(i)}"
  end
  sanitize_input
end
set_work_on_these_files(i) click to toggle source
#

set_work_on_these_files

It is better to store the absolute path towards the file at hand.

#
# File lib/rbt/misc/sed_wrapper.rb, line 133
def set_work_on_these_files(i)
  unless i.is_a? Array
    i = [i]
  end
  if i.is_a? Array
    i.map! {|entry|
      File.absolute_path(entry)
    }
  end
  @internal_hash[:work_on_these_files] = i
end
target_file?()
these_files?()
try_to_read_in_this_file(i = target_file?) click to toggle source
#

try_to_read_in_this_file

#
# File lib/rbt/misc/sed_wrapper.rb, line 157
def try_to_read_in_this_file(i = target_file?)
  if File.exist? i
    @dataset = File.readlines(i)
  end
end
work_on_these_files()
work_on_these_files?() click to toggle source
#

work_on_these_files?

#
# File lib/rbt/misc/sed_wrapper.rb, line 148
def work_on_these_files?
  @internal_hash[:work_on_these_files]
end