Module: RgssDb::Utilities

Defined in:
lib/rgss_db/model/utilities.rb

Overview

Utilities module

Constant Summary collapse

INVALID_CHARACTERS =

Regular expression of invalid characters or sequence of characters

Returns:

  • (Regexp)
/[:*?"<>|]|(\bCON\b|\bPRN\b|\bAUX\b|\bNUL\b|\bCOM[1-9]\b|\bLPT[1-9]\b)/i

Class Method Summary collapse

Class Method Details

Gets the list of the default (pre-selected) indexes for a TTY selection menu

If a block is given, it will be used to evaluate the index selection

The block receives the following parameters “[Object, Integer, Object]“:

- The first one is the current menu option being evaluated
- The second one is the index of the menu option being evaluated
- The third argument iterates through each user option

If the block ever returns “true“ for the current menu option, its index will be saved

If no block is given, it will use “Array#include?“ to check if either the menu option or the menu index exists in the options user list

The following flags can be used to alter the behavior:

- all_if_empty: Select all options if the user's options list is empty

Parameters:

  • options_menu (Array)

    Menu options list

  • options_user (Array)

    List of user selected options

  • all_if_empty (Boolean) (defaults to: false)

    Whether to select all options if the user array is empty

  • block (Proc)

    Evaluation callback

Yield Parameters:

  • (Object)
  • (Integer)
  • (Object)

Returns:

  • (Array<Integer>)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rgss_db/model/utilities.rb', line 41

def self.menu_default_indexes(options_menu, options_user, all_if_empty: false, &block)
  # Checks if user's options list is empty (and populate it with all indexes if allowed)
  return (1..options_menu.size).to_a if options_user.empty? && all_if_empty
  return [] if options_user.empty?

  # Process the menu options list normally
  options_indexes = []
  if block_given?
    options_menu.each_with_index do |menu_option, index|
      menu_index = index + 1
      options_indexes << menu_index if options_user.any? { |opt_user| yield menu_option, menu_index, opt_user }
    end
  else
    options_menu.each_with_index do |menu_option, index|
      menu_index = index + 1
      options_indexes << menu_index if options_user.include?(menu_option) || options_user.include?(menu_index)
    end
  end
  options_indexes
end

.valid_path?(path) ⇒ Boolean

Checks whether the path is valid or not

Returns “true“ if the path is valid, otherwise “false“

Parameters:

  • path (String)

    Path

Returns:

  • (Boolean)


86
87
88
# File 'lib/rgss_db/model/utilities.rb', line 86

def self.valid_path?(path)
  validate_path(path).nil?
end

.validate_path(path) ⇒ MatchData

Validates the path

Returns a “MatchData“ if the path is invalid, otherwise “nil“

The “MatchData“ object contains the invalid characters

Parameters:

  • path (String)

    Path

Returns:

  • (MatchData)


73
74
75
# File 'lib/rgss_db/model/utilities.rb', line 73

def self.validate_path(path)
  path.to_s.match(INVALID_CHARACTERS)
end