class CaseParser

#

Constants

DEFAULT_FILE
#

Test it via compile.rb.

#
SHALL_WE_DEBUG
#

SHALL_WE_DEBUG

#
USE_THIS_ENCODING
#

USE_THIS_ENCODING

#
VERSION
#

VERSION

#

Public Class Methods

new( i = DEFAULT_FILE, run_already = true ) click to toggle source
#

initialize

#
# File lib/case_parser/case_parser.rb, line 50
def initialize(
    i           = DEFAULT_FILE,
    run_already = true
  )
  reset
  case run_already
  when :continue,
       :do_not_exit
    @may_we_exit_on_missing_file = false
    run_already = true
  end
  run(i) if run_already
end
parse(i, optional_run_already = :do_not_exit) click to toggle source
#

CaseParser.parse

This method will return a (sorted) array.

To use this, do something like:

CaseParser.parse(__FILE__)
#
# File lib/case_parser/class_methods.rb, line 19
def self.parse(i, optional_run_already = :do_not_exit)
  return CaseParser.new(i, optional_run_already).results
end

Public Instance Methods

append_to_main_array(i) click to toggle source
#

append_to_main_array

#
# File lib/case_parser/case_parser.rb, line 250
def append_to_main_array(i)
  @array_keeping_all_when_entries << i
end
dataset?() click to toggle source
#

dataset?

#
# File lib/case_parser/case_parser.rb, line 257
def dataset?
  @dataset
end
extract_case_menu() click to toggle source
#

extract_case_menu

This method will extract the case menu.

#
# File lib/case_parser/case_parser.rb, line 233
def extract_case_menu
  _ = []
  dataset?.each {|line|
    begin
      @seen_case = true if line.include? 'case'
      _ << remove_potential_comments(line) if @seen_case
      @seen_case = false if line =~ /end$/
    rescue ArgumentError # This is here to rescue against "invalid byte sequence in US-ASCII"
    end
  } if dataset?
  _.reject!(&:empty?)
  @dataset = _
end
extract_first_when( mode = :extract_only_first_entry ) click to toggle source
#

extract_first_when

Use this method here if you wish to extract the first word.

#
# File lib/case_parser/case_parser.rb, line 209
def extract_first_when(
    mode = :extract_only_first_entry
  )
  dataset?.each { |d|
    if d.include? 'when' # Work on when-entries only. This is not perfect, as we miss other entries.
      begin
        _ = d.chomp.strip.gsub(/when /,'') # Eliminate 'when ' here.
      rescue ArgumentError
        opn; e 'ArgumentError - invalid byte sequence in US-ASCII'
      end
      if _
        _ = _.split(',') if _.include? ',' # Split on ',' if they exist.
        _ = _.delete("'") if _.include? "'" # Eliminate "'" characters.
        append_to_main_array(_)
      end
    end
  }
end
feedback() click to toggle source
#

feedback

#
# File lib/case_parser/case_parser.rb, line 126
def feedback
  opn; pp results
end
found_entries?() click to toggle source
#

found_entries?

We return all entries found in a sorted manner here.

#
# File lib/case_parser/case_parser.rb, line 266
def found_entries?
  @array_keeping_all_when_entries.sort
end
read_file() click to toggle source
#

read_file

#
# File lib/case_parser/case_parser.rb, line 173
def read_file
  if File.exist? @file
    @dataset = File.readlines(@file, encoding: USE_THIS_ENCODING)
  else
    if @may_we_exit_on_missing_file
      opn; e red+'Can not continue. File `'+sfile(@file)+
             red+'` does not exist.'+rev
      exit
    end
  end
  if @shall_we_debug
    opn; pp dataset?
  end
end
red() click to toggle source
#

red

#
# File lib/case_parser/case_parser.rb, line 166
def red
  Colours::RED
end
remove_potential_comments(i) click to toggle source
#

remove_potential_comments

This gets rid of potential '#' comments.

#
# File lib/case_parser/case_parser.rb, line 158
def remove_potential_comments(i)
  i = i[0, i.index('#')].strip if i.include? '#' # If there is a #, return all up to that.
  return i
end
report_n_entries() click to toggle source
#

report_n_entries

#
# File lib/case_parser/case_parser.rb, line 191
def report_n_entries
  opn; e "We found #{simp(dataset?.size.to_s)} entries."
end
reset() click to toggle source
#

reset

#
# File lib/case_parser/case_parser.rb, line 96
def reset
  # ======================================================================== #
  # === @dataset
  # ======================================================================== #
  @dataset = nil
  # ======================================================================== #
  # === @seen_case
  #
  # Whether we saw "case" yet or did not.
  # ======================================================================== #
  @seen_case = false
  # ======================================================================== #
  # === @array_keeping_all_when_entries
  # ======================================================================== #
  @array_keeping_all_when_entries = []
  # ======================================================================== #
  # === @shall_we_debug
  #
  # If true then we will debug.
  # ======================================================================== #
  @shall_we_debug = SHALL_WE_DEBUG
  # ======================================================================== #
  # === @may_we_exit_on_missing_file
  # ======================================================================== #
  @may_we_exit_on_missing_file = true
end
results() click to toggle source
#

results

#
# File lib/case_parser/case_parser.rb, line 198
def results
  _ = @array_keeping_all_when_entries
  _.sort! if _
  _
end
run(which_file) click to toggle source
#

run

General run method. We will also set_file() in this method.

#
# File lib/case_parser/case_parser.rb, line 275
def run(which_file)
  set_file(which_file)
  read_file
  extract_case_menu
  extract_first_when
  sanitize_array
end
sanitize_array() click to toggle source
#

sanitize_array

This will try to sanitize the array.

#
# File lib/case_parser/case_parser.rb, line 135
def sanitize_array
  @array_keeping_all_when_entries.flatten!
  begin # Musct rescue this in case the encoding is bad.
    @array_keeping_all_when_entries.map! {|entry|
      entry.delete("'").strip
    }
  rescue; end
  # ======================================================================== #
  # Now, we remove all Regexes from this menu, because Regexes
  # are not really useful to keep for tab completion in most
  # scripts.
  # ======================================================================== #
  @array_keeping_all_when_entries.reject! {|entry|
    entry.start_with? '/' or
    entry.start_with? ':' # Symbols also get rejected.
  }
end
set_file(i) click to toggle source
#

set_file

Simply set the @file variable here.

#
# File lib/case_parser/case_parser.rb, line 69
def set_file(i)
  i = DEFAULT_FILE if i.nil?
  case i
  # ======================================================================== #
  # === :beautiful_menu
  # ======================================================================== #
  when :beautiful_menu
    i = ConvertGlobalEnv[
      '$RUBY_SRC/beautiful_url/lib/beautiful_url/toplevel_methods/menu.rb'
    ] # Hardcoded.
    unless File.exist? i
      opn; e 'Warning - no file exists at `'+sfile(i)+'`.'
    end
  when :dia_menu
    i = ConvertGlobalEnv['$RUBY_SRC/diamond_shell/lib/diamond_shell/menu/menu.rb']
    unless File.exist? i
      opn; e 'Warning - no file exists at `'+sfile(i)+'`.'
    end
  end
  i = i.to_s unless i.is_a? String
  i = ConvertGlobalEnv.convert(i) if i.include? '$'
  @file = i
end