Class: SplitIoClient::Semver

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/engine/matchers/semver.rb

Constant Summary collapse

METADATA_DELIMITER =
'+'
PRE_RELEASE_DELIMITER =
'-'
VALUE_DELIMITER =
'.'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Semver

Returns a new instance of Semver.



11
12
13
14
15
16
17
18
19
20
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 11

def initialize(version)
  @major = 0
  @minor = 0
  @patch = 0
  @pre_release = []
  @is_stable = false
  @version = ''
  @metadata = ''
  parse(version)
end

Instance Attribute Details

#is_stableObject (readonly)

Returns the value of attribute is_stable.



9
10
11
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 9

def is_stable
  @is_stable
end

#majorObject (readonly)

Returns the value of attribute major.



9
10
11
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 9

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



9
10
11
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 9

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



9
10
11
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 9

def patch
  @patch
end

#pre_releaseObject (readonly)

Returns the value of attribute pre_release.



9
10
11
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 9

def pre_release
  @pre_release
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 9

def version
  @version
end

Class Method Details

.build(version, logger) ⇒ type

Class builder

Parameters:

  • version (String)

    raw version as read from splitChanges response.

Returns:

  • (type)

    Semver instance



28
29
30
31
32
33
34
35
36
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 28

def self.build(version, logger)
  new(version)
rescue NoMethodError => e
  logger.error("Failed to parse Semver data, incorrect data type:  #{e}")
  nil
rescue StandardError => e
  logger.error("Failed to parse Semver data:  #{e}")
  nil
end

Instance Method Details

#compare(to_compare) ⇒ Object

Compare the current Semver object to a given Semver object, return:

0: if self == passed
1: if self > passed
-1: if self < passed

Parameters:

  • to_compare (trype)

    splitio.models.grammar.matchers.semver.Semver object



59
60
61
62
63
64
65
66
67
68
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 59

def compare(to_compare)
  return 0 if @version == to_compare.version

  # Compare major, minor, and patch versions numerically
  result = compare_attributes(to_compare)
  return result if result != 0

  # Compare pre-release versions lexically
  compare_pre_release(to_compare)
end

#remove_metadata_if_exists(old_version) ⇒ type

Check if there is any metadata characters in version.

Returns:

  • (type)

    String semver without the metadata



43
44
45
46
47
48
49
# File 'lib/splitclient-rb/engine/matchers/semver.rb', line 43

def (old_version)
  index = old_version.index(METADATA_DELIMITER)
  return old_version if index.nil?

  @metadata = old_version[index + 1, old_version.length]
  old_version[0, index]
end