Class: ThorSCMVersion::Prerelease

Inherits:
Object
  • Object
show all
Defined in:
lib/thor-scmversion/prerelease.rb

Constant Summary collapse

TYPE_FORMAT =
/[A-Za-z0-9]+/
FORMAT =
/(#{TYPE_FORMAT})\.(\d+)/.freeze
DEFAULT_TYPE =
'alpha'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = DEFAULT_TYPE, version = 1) ⇒ Prerelease

initialize

Examples:

'beta', 1 #=> x.x.x-beta.1

Parameters:

  • type (String) (defaults to: DEFAULT_TYPE)

    Type of prerelease

  • version (responds_to? :to_i) (defaults to: 1)

    Version of the given type of prerelease



39
40
41
42
# File 'lib/thor-scmversion/prerelease.rb', line 39

def initialize(type = DEFAULT_TYPE, version = 1)
  @version = version.to_i
  @type = type.nil? || type.empty? ? DEFAULT_TYPE : type
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object

Delegate methods to @version so this object acts like an Integer



75
76
77
78
# File 'lib/thor-scmversion/prerelease.rb', line 75

def method_missing(method, *args, &blk)
  @version = self.version.send(method, *args, &blk)
  self
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



30
31
32
# File 'lib/thor-scmversion/prerelease.rb', line 30

def type
  @type
end

#versionObject (readonly)

Returns the value of attribute version.



29
30
31
# File 'lib/thor-scmversion/prerelease.rb', line 29

def version
  @version
end

Class Method Details

.from_string(str) ⇒ Prerelease

Reads the prerelease segment of the version. While semver provides a more general format, the format with thor-scmversion is more strictly defined:

Examples:

str = 'alpha.1'
str = 'beta.10'
str = 'rc.2'

Parameters:

  • str (String)

    String in the format /[A-Za-z]+.d+/

Returns:

Raises:



19
20
21
22
23
24
25
26
# File 'lib/thor-scmversion/prerelease.rb', line 19

def from_string(str)
  return nil if str.nil?
  matchdata = str.match(FORMAT)
  type, version = matchdata.captures unless matchdata.nil?
  raise InvalidPrereleaseFormatError.new(str) if (matchdata.nil? ||
                                                  matchdata.captures.size != 2)
  new(type, version)
end

Instance Method Details

#<(other) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
# File 'lib/thor-scmversion/prerelease.rb', line 62

def <(other)
  raise ArgumentError unless kind_of? other.class
  return (self <=> other) == -1
end

#<=>(other) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
# File 'lib/thor-scmversion/prerelease.rb', line 48

def <=>(other)
  raise ArgumentError unless kind_of? other.class
  if self.type == other.type
    self.version <=> other.version
  else
    self.type <=> other.type
  end
end

#==(other) ⇒ Object



67
68
69
70
71
72
# File 'lib/thor-scmversion/prerelease.rb', line 67

def ==(other)
  other &&
    kind_of?(other.class) &&
    self.version == other.version &&
    self.type == other.type
end

#>(other) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
# File 'lib/thor-scmversion/prerelease.rb', line 57

def >(other)
  raise ArgumentError unless kind_of? other.class
  return (self <=> other) == 1
end

#to_sObject



44
45
46
# File 'lib/thor-scmversion/prerelease.rb', line 44

def to_s
  "#{@type}.#{@version}"
end