Class: Chef::Cookbook::Metadata::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/chef/cookbook/metadata/version.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = "") ⇒ Version

Returns a new instance of Version.



30
31
32
# File 'lib/chef/cookbook/metadata/version.rb', line 30

def initialize(str="")
  @major, @minor, @patch = _parse(str)
end

Instance Attribute Details

#majorObject

Returns the value of attribute major.



26
27
28
# File 'lib/chef/cookbook/metadata/version.rb', line 26

def major
  @major
end

#minorObject

Returns the value of attribute minor.



26
27
28
# File 'lib/chef/cookbook/metadata/version.rb', line 26

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



26
27
28
# File 'lib/chef/cookbook/metadata/version.rb', line 26

def patch
  @patch
end

Instance Method Details

#<=>(v) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/cookbook/metadata/version.rb', line 53

def <=>(v)
  major, minor, patch = (
    [ :major, :minor, :patch ].collect do |method|
      self.send(method) <=> v.send(method)
    end
  )

  Chef::Log.debug "(#{self.to_s}/#{v.to_s}) major,minor,patch: #{[major,minor,patch].join(',')}"

  # all these returns feels like C, surely there is a better way!

  if major == 0 && minor == 0 && patch == 0
    comp = 0
  end

  if major == 1
    comp = 1
  end

  if major == 0 && minor == 1 && patch == -1
    comp = 1
  end

  if minor == 1 && major == 0 && patch == 0
    comp = 1
  end

  if patch == 1 && major == 0 && minor == 0
    comp = 1
  end

  return (comp || -1)
end

#_parse(str = "") ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/chef/cookbook/metadata/version.rb', line 34

def _parse(str="")
  @major, @minor, @patch = case str.to_s
    when /^(\d+)\.(\d+)\.(\d+)$/
      [ $1.to_i, $2.to_i, $3.to_i ]
    when /^(\d+)\.(\d+)$/
      [ $1.to_i, $2.to_i, 0 ]
    else
      raise "Metadata version '#{str.to_s}' does not match 'x.y.z' or 'x.y'"
  end
end

#inspectObject



45
46
47
# File 'lib/chef/cookbook/metadata/version.rb', line 45

def inspect
  "#{@major}.#{@minor}.#{@patch}"
end

#to_sObject



49
50
51
# File 'lib/chef/cookbook/metadata/version.rb', line 49

def to_s
  "#{@major}.#{@minor}.#{@patch}"
end