Module: Richcss::VersionKit::Version::ComponentsHelper

Defined in:
lib/richcss/vendor/version_kit/lib/version_kit/version/components_helper.rb

Overview

Provides support for working with version components and comparing them.

Assumes identifiers converted to the appropriate class as the ones returned by the ::split_identifiers method.

Class Method Summary collapse

Class Method Details

.compare(first, second) ⇒ Fixnum, Nil

Compares two boolean values returning a comparison result if only one condition is truthy.

Parameters:

  • fist (Object)

    The first object to compare.

  • second (Object)

    The second object to compare.

Returns:

  • (Fixnum)

    See #<=>

  • (Nil)

    If the comparison didn’t produce any result.



130
131
132
133
134
135
136
137
138
# File 'lib/richcss/vendor/version_kit/lib/version_kit/version/components_helper.rb', line 130

def self.compare(first, second)
  if first && !second
    +1
  elsif second && !first
    -1
  else
    nil
  end
end

.compare_number_component(first, second) ⇒ Fixnum

Compares the number component of one version with the one of another version.

Parameters:

  • first (Array<Fixnum>)

    The component of the first version.

  • second (Array<Fixnum>)

    The component of the second version.

Returns:

  • (Fixnum)

    See #<=>



62
63
64
65
66
67
68
69
70
# File 'lib/richcss/vendor/version_kit/lib/version_kit/version/components_helper.rb', line 62

def self.compare_number_component(first, second)
  count = [first.count, second.count].max
  count.times do |index|
    result = first[index].to_i <=> second[index].to_i
    return result unless result.zero?
  end

  nil
end

.compare_pre_release_component(first, second) ⇒ Fixnum

Compares the pre-release component of one version with the one of another version.

Parameters:

  • first (Array<Fixnum>)

    The component of the first version.

  • second (Array<Fixnum>)

    The component of the second version.

Returns:

  • (Fixnum)

    See #<=>



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/richcss/vendor/version_kit/lib/version_kit/version/components_helper.rb', line 83

def self.compare_pre_release_component(first, second)
  result = (first.empty? ? 1 : 0) <=> (second.empty? ? 1 : 0)
  return result unless result.zero?

  count = [first.count, second.count].max
  count.times do |index|
    result = compare_pre_release_identifiers(first[index], second[index])
    return result unless result.zero?
  end

  nil
end

.compare_pre_release_identifiers(first, second) ⇒ Fixnum

Compares two pre-release identifiers.

Parameters:

  • fist (String, Fixnum)

    The first identifier to compare.

  • second (String, Fixnum)

    The second identifier to compare.

Returns:

  • (Fixnum)

    See #<=>



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/richcss/vendor/version_kit/lib/version_kit/version/components_helper.rb', line 106

def self.compare_pre_release_identifiers(first, second)
  result = compare(first, second)
  result ||= compare(first.is_a?(String), second.is_a?(String))
  return result if result

  if first.is_a?(Fixnum)
    first.to_i <=> second.to_i
  elsif first.is_a?(String)
    first.to_s <=> second.to_s
  end
end

.split_components(version) ⇒ Array<String,Fixnum>

Splits a string representing a component in a list of the single identifiers (separated by a dot). Identifiers composed only by digits are converted to an integer in the process.

Parameters:

  • components (Array<String>)

    The list of the components.

Returns:

  • (Array<String,Fixnum>)

    The list of the elements of the component.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/richcss/vendor/version_kit/lib/version_kit/version/components_helper.rb', line 19

def self.split_components(version)
  component_strings = version.scan(/[^-+]+/)
  (0...3).map do |index|
    indentifiers_string = component_strings[index]
    if indentifiers_string
      ComponentsHelper.split_identifiers(indentifiers_string)
    else
      []
    end
  end
end

.split_identifiers(component) ⇒ Array<String,Fixnum>

Splits a string representing a component in a list of the single identifiers (separated by a dot). Identifiers composed only by digits are converted to an integer in the process.

Parameters:

  • component (String)

    The string of the component to split in identifiers.

Returns:

  • (Array<String,Fixnum>)

    The list of the identifiers of the component.



41
42
43
44
45
46
47
48
49
# File 'lib/richcss/vendor/version_kit/lib/version_kit/version/components_helper.rb', line 41

def self.split_identifiers(component)
  component.split('.').map do |identifier|
    if identifier =~ /\A[0-9]+\Z/
      identifier.to_i
    else
      identifier
    end
  end
end

.validate_components?(components) ⇒ Bool

Checks whether the given components are valid.

rubocop:disable CyclomaticComplexity

Parameters:

  • components (Array<Array<String, Fixnum>>)

    The components to check.

Returns:

  • (Bool)

    If the given components are valid.



149
150
151
152
153
154
155
156
157
# File 'lib/richcss/vendor/version_kit/lib/version_kit/version/components_helper.rb', line 149

def self.validate_components?(components)
  components.is_a?(Array) &&
    components.map(&:class).uniq == [Array] &&
    components.count == 3 &&
    components.first.count == 3 &&
    (components[0].map(&:class) - [Fixnum]).empty? &&
    (components[1].map(&:class) - [String, Fixnum]).empty? &&
    (components[2].map(&:class) - [String, Fixnum]).empty?
end