Module: YardGhurt::Util::ClassMethods

Included in:
YardGhurt::Util
Defined in:
lib/yard_ghurt/util.rb

Overview

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#parse_sem_ver(str) ⇒ Hash

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.

Parameters:

  • str (String, Object)

    the object to parse; to_s() will be called on it

Returns:

  • (Hash)

    the Semantic Version parts: {:major, :minor, :patch} defaults all values to 0 if the version cannot be parsed

See Also:

Since:

  • 1.2.1



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object

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

Parameters:

  • filename (String)

    the file to remove

  • output (true, false) (defaults to: true)

    whether to log it to stdout

Since:

  • 1.0.0



51
52
53
54
55
56
# 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) ⇒ true, false

Convert str to true or false.

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

Parameters:

  • str (String, Object)

    the String (or Object) to convert

Returns:

  • (true, false)

    the boolean value of str

See Also:

Since:

  • 1.0.0



68
69
70
# File 'lib/yard_ghurt/util.rb', line 68

def to_bool(str)
  return TRUE_BOOLS.include?(str.to_s.downcase)
end

#yard_sem_verHash

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.

Returns:

  • (Hash)

    YARD's version parts

See Also:

Since:

  • 1.2.1



114
115
116
117
118
119
120
121
122
123
124
125
126
# 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