module YardGhurt::Util::ClassMethods

Public Instance Methods

parse_sem_ver(str) click to toggle source

Parse str as a non-strict Semantic Version:

\d+.\d+.\d+

Unlike the specification, minor and patch are optional. Also, pre-release and build metadata are ignored. This is used for checking the YARD version internally, so needs to be very flexible.

@param str [String,Object] the object to parse; +to_s()+ will be called on it @return [Hash] the Semantic Version parts: +{:major, :minor, :patch}+

defaults all values to +0+ if the version cannot be parsed

@see SEM_VER_REGEX @see yard_sem_ver @since 1.2.1

# File lib/yard_ghurt/util.rb, line 86
def parse_sem_ver(str)
  sem_ver = {
    major: 0,minor: 0,patch: 0,
  }

  match = SEM_VER_REGEX.match(str.to_s.gsub(/\s+/u,''))

  return sem_ver unless match

  sem_ver[:major] = match[:major].to_i
  sem_ver[:minor] = match[:minor].to_i unless match[:minor].nil?
  sem_ver[:patch] = match[:patch].to_i unless match[:patch].nil?

  return sem_ver
end
rm_exist(filename,output=true) click to toggle source

If filename exists, delete it, and if output is true, log it to stdout.

@param filename [String] the file to remove @param output [true,false] whether to log it to stdout

# File lib/yard_ghurt/util.rb, line 51
def rm_exist(filename,output=true)
  return unless File.exist?(filename)

  puts "[#{filename}]: - Deleted" if output
  File.delete(filename)
end
to_bool(str) click to toggle source

Convert str to true or false.

Even if str is not a String, +to_s()+ will be called, so should be safe.

@param str [String,Object] the String (or Object) to convert

@return [true,false] the boolean value of str

@see TRUE_BOOLS @see GHPSyncTask#arg_names

# File lib/yard_ghurt/util.rb, line 68
def to_bool(str)
  return TRUE_BOOLS.include?(str.to_s.downcase)
end
yard_sem_ver() click to toggle source

Returns YARD's version as a Hash of parts:

{ major: 0, minor: 0, patch: 0 }

If the version can not be parsed, it will return the exact same Hash as above with all values to 0.

On initial call, it will parse it and store it. On subsequent calls, it will return the stored value.

@return [Hash] YARD's version parts @see parse_sem_ver @since 1.2.1

# File lib/yard_ghurt/util.rb, line 114
def yard_sem_ver
  return @yard_sem_ver unless @yard_sem_ver.nil?

  require 'yard'

  if defined?(YARD::VERSION)
    ver = YARD::VERSION
  else
    ver = ''
  end

  return(@yard_sem_ver = parse_sem_ver(ver))
end