Class: Babushka::VersionOf

Inherits:
Object show all
Defined in:
lib/babushka/version_of.rb

Defined Under Namespace

Modules: Helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version = nil) ⇒ VersionOf

Returns a new instance of VersionOf.



36
37
38
39
40
41
42
43
44
45
# File 'lib/babushka/version_of.rb', line 36

def initialize name, version = nil
  @name = name.is_a?(VersionOf) ? name.name : name
  @version = if version.nil?
    name.version if name.respond_to?(:version)
  elsif version.is_a? VersionStr
    version
  else
    version.to_version
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



34
35
36
# File 'lib/babushka/version_of.rb', line 34

def name
  @name
end

#versionObject

Returns the value of attribute version.



34
35
36
# File 'lib/babushka/version_of.rb', line 34

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
# File 'lib/babushka/version_of.rb', line 56

def <=> other
  raise ArgumentError, "You can't compare the versions of two different things (#{name}, #{other.name})." unless name == other.name
  version <=> other.version
end

#==(other) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/babushka/version_of.rb', line 47

def == other
  if other.is_a? VersionOf
    name == other.name &&
    version == other.version
  else
    to_s == other.to_s
  end
end

#exact?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/babushka/version_of.rb', line 75

def exact?
  !version.nil? && version.operator == '=='
end

#inspectObject



79
80
81
# File 'lib/babushka/version_of.rb', line 79

def inspect
  "#<VersionOf #{name}#{", v#{version}" unless version.nil?}>"
end

#matches?(other) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
# File 'lib/babushka/version_of.rb', line 61

def matches? other
  if other.is_a? VersionStr
    version.nil? || other.send(version.operator, version)
  else
    matches? other.to_version
  end
end

#to_sObject



69
70
71
72
73
# File 'lib/babushka/version_of.rb', line 69

def to_s
  # == joins with a dash to produce versions like 'rack-1.4.1'; anything
  # else joins with space like 'rack >= 1.4'.
  [name, version].compact * (exact? ? '-' : ' ')
end