class CmdExecutable::Parser

Parser for CmdExecutable

Attributes

raw[R]

Public Class Methods

new(raw) click to toggle source
# File lib/cmd_executable/parser.rb, line 34
def initialize(raw)
  @raw = raw
  @raw.freeze
end

Public Instance Methods

command() click to toggle source
# File lib/cmd_executable/parser.rb, line 46
def command
  parse if @command.nil?
  @command
end
validate?() click to toggle source
# File lib/cmd_executable/parser.rb, line 39
def validate?
  !@raw.nil? &&
    command_class_validate? &&
    !@raw.empty? &&
    !include_invalid_char?
end

Private Instance Methods

a_dot_only_regex() click to toggle source
# File lib/cmd_executable/parser.rb, line 77
def a_dot_only_regex
  /\A\.\Z/
end
basename_exist?(path) click to toggle source
# File lib/cmd_executable/parser.rb, line 85
def basename_exist?(path)
  path.match?(no_separator_at_the_right_end_regex)
end
command_class_validate?() click to toggle source
# File lib/cmd_executable/parser.rb, line 53
def command_class_validate?
  @raw.is_a?(String) ||
    @raw.is_a?(Symbol)
end
current_path?(path) click to toggle source
# File lib/cmd_executable/parser.rb, line 93
def current_path?(path)
  path.match?(current_path_at_the_left_regex)
end
current_path_at_the_left_regex() click to toggle source
# File lib/cmd_executable/parser.rb, line 81
def current_path_at_the_left_regex
  /\A\.#{File::SEPARATOR}/
end
escape_char(path) click to toggle source
# File lib/cmd_executable/parser.rb, line 112
def escape_char(path)
  path.gsub(/"/, '\"')
end
include_invalid_char?() click to toggle source
# File lib/cmd_executable/parser.rb, line 58
def include_invalid_char?
  @raw.match?(/\r\n|\r|\n/) ||
    @raw.match?(/\$\(.*\)/)
end
no_right_separator_exists?(dir) click to toggle source
# File lib/cmd_executable/parser.rb, line 89
def no_right_separator_exists?(dir)
  dir.match?(no_separator_at_the_right_end_regex)
end
no_separator_at_the_right_end_regex() click to toggle source
# File lib/cmd_executable/parser.rb, line 73
def no_separator_at_the_right_end_regex
  /(?<!#{File::SEPARATOR})\Z/
end
parse() click to toggle source
# File lib/cmd_executable/parser.rb, line 63
def parse
  raise CmdExecutable::ParserError, @raw unless validate?

  path = escape_char(@raw.to_s.chomp)
  @dirname = parse_dirname(path)
  @basename = parse_basename(path)
  @command = @dirname + @basename
  self
end
parse_basename(path) click to toggle source
# File lib/cmd_executable/parser.rb, line 106
def parse_basename(path)
  return '' unless basename_exist?(path)

  File.basename(path).split.first
end
parse_dirname(path) click to toggle source
# File lib/cmd_executable/parser.rb, line 97
def parse_dirname(path)
  return path unless basename_exist?(path)

  dir = File.dirname(path)
  return '' if dir.match?(a_dot_only_regex) && !current_path?(path)

  no_right_separator_exists?(dir) ? (dir + File::SEPARATOR) : dir
end