class TTY::Platform

Detects system platform properties

@api public

Constants

LINUX_PATTERN
MAC_PATTERN
UNIX_PATTERN
VERSION
WINDOWS_PATTERN

Attributes

cpu[R]

Returns processor name, e.g. 'amdk6'

@api public

os[R]

Returns the system/OS name, e.g. 'darwin'

@api public

version[R]

Returns system's release version, e.g. '10.8.1'

@api public

Public Class Methods

new(arch = nil) click to toggle source

Create platform properties

@api public

# File lib/tty/platform.rb, line 38
def initialize(arch = nil)
  @cpu, @os, @version = *detect_system_properties(arch)
end

Public Instance Methods

architecture() click to toggle source

Queries for system architecture information

@return [String]

@api public

# File lib/tty/platform.rb, line 79
def architecture
  RbConfig::CONFIG['arch']
end
match_os?(matcher) click to toggle source

Check if platform matches given systems

@return [Boolean]

@api public

# File lib/tty/platform.rb, line 70
def match_os?(matcher)
  !!(@os =~ matcher)
end
to_a() click to toggle source

@return [Array]

@api public

# File lib/tty/platform.rb, line 86
def to_a
  [@cpu, @os, @version]
end
to_s() click to toggle source

String representation

@api public

# File lib/tty/platform.rb, line 93
def to_s
  to_a.compact.join('-')
end
windows_file_path?() click to toggle source

Detect if using windows path delimiter

@return [Boolean]

@api public

# File lib/tty/platform.rb, line 61
def windows_file_path?
  ::File::ALT_SEPARATOR == '\\'
end

Private Instance Methods

detect_system_properties(arch) click to toggle source

Infer system properties from architecture information

@return [Array[String, String String]]

@api private

# File lib/tty/platform.rb, line 104
def detect_system_properties(arch)
  parts = (arch || architecture).split('-', 2)

  if parts.length == 1
    @cpu, system = nil, parts.shift
  else
    @cpu, system = *parts
  end
  @os, @version = *find_os_and_version(system)
  [@cpu, @os, @version]
end
find_os_and_version(system) click to toggle source

@param [String] system

@api private

# File lib/tty/platform.rb, line 119
def find_os_and_version(system)
  case system
  when /aix(\d+(\.\d+)*)?/         then ['aix',       $1]
  when /bccwin(\d+(\.\d+)*)?/      then ['bccwin',    $1]
  when /bitrig(\d+(\.\d+)*)?/      then ['bitrig',    $1]
  when /cygwin/                    then ['cygwin',    nil]
  when /darwin(\d+(\.\d+)*)?/      then ['darwin',    $1]
  when /emx/                       then ['emx',       nil]
  when /freebsd(\d+(\.\d+)*)?/     then ['freebsd',   $1]
  when /linux(\d+(\.\d+)*)?/       then ['linux',     $1]
  when /mingw32/                   then ['mingw32',   nil]
  when /(mswin\d+)((\_|-)(\d+))?/  then [$1,          $4]
  when /netbsdelf/                 then ['netbsdelf', nil]
  when /openbsd(\d+(\.\d+)*)?/     then ['openbsd',   $1]
  when /solaris(\d+(\.\d+)*)?/     then ['solaris',   $1]
  when /wince(\d+(\.\d+)*)?/       then ['wince',     $1]
  else ['unknown', nil]
  end
end