Class: IPXACT::Identifier

Inherits:
Object
  • Object
show all
Defined in:
lib/ipxact/identifier.rb

Overview

Copyright © 2010 TIMA Laboratory

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Constant Summary collapse

ATTRIBUTES =
[
  :name, :library, :vendor, :version
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Identifier

Returns a new instance of Identifier.



26
27
28
29
30
# File 'lib/ipxact/identifier.rb', line 26

def initialize(args)
  ATTRIBUTES.each do |attr|
    instance_variable_set("@#{attr}", args[attr] || "")
  end
end

Class Method Details

.is_most_recent_version?(version_a, version_b) ⇒ Boolean

Decides whether version_a is strictly more recent than version_b. Accepts any relaxed version of semantic versioning markup (i.e., X(.Y(.Z)?)?).

Parameters:

  • version_a (String)

    a version number

  • version_b (String)

    a version number

Returns:

  • (Boolean)

    a Boolean that states if version_a > version_b



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ipxact/identifier.rb', line 40

def self.is_most_recent_version?(version_a, version_b)
  split_a = version_a.split('.')
  split_b = version_b.split('.')

  split_a.each_with_index do |el,i|
    return true if split_b.size <= i && el.to_i != 0
    return true if el.to_i > split_b[i].to_i
  end

  false
end

.is_same_version?(version_a, version_b) ⇒ Boolean

Decides whether version_a and version_b are the same version. Accepts any relaxed version of the semantic versioning markup (i.e., X(.Y(.Z)?)?). When comparing versions with different lengths, the method will ‘pad’ the shortest version string with 0s.

Examples:

IPXACT::is_same_version('1.0', '1.1')
# => false

IPXACT::is_same_version('1.0', '1.0')
# => true

IPXACT::is_same_version('1.0.0', '1.0')
# => true

IPXACT::is_same_version('1.0.1', '1.0')
# => false

Returns:

  • (Boolean)

    a Boolean that states if version_a == version_b (relaxed equivalence)



73
74
75
# File 'lib/ipxact/identifier.rb', line 73

def self.is_same_version?(version_a, version_b)
  return !(self.is_most_recent_version?(version_a, version_b) || self.is_most_recent_version?(version_b, version_a))
end

Instance Method Details

#==(other) ⇒ Boolean

Overrides the default equality. Two IPXACT::Identifier are defined as equal when they share the same 1) name; 2) library; 3) vendor and 4) version (based on the result of the is_same_version? method).

Parameters:

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/ipxact/identifier.rb', line 86

def ==(other)
  name == other.name &&
  library == other.library &&
  vendor == other.vendor &&
  IPXACT::Identifier.is_same_version?(version, other.version)
end