Class: VersionHelper::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhelper/versionhelper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arg) ⇒ Version

Params:

arg : list of arguments Integer : 1234 => 1.2.3.4 String : “1.2-3” => 1.2.3 Array : like multiple arguments multiple : each argument is converted to a number 1,2,3 => 1.2.3



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubyhelper/versionhelper.rb', line 18

def initialize(*arg)
  @v = []
  if arg.size == 1
    v = arg.first
    case v.class.to_s
    when 'String'
      v.gsub!(/\A\D/, '')
      v.gsub!(/\D/, '.')
      @v = v.split('.').map{|e| e.to_i}
    when 'Array'
      @v = v.map{|e| e.to_i}
    when 'Fixnum'
      v = v.to_i
      loop do
        break if v == 0
        @v << (v % 10)
        v /= 10
      end
    else
      raise ArgumentError, v.class.to_s
    end
  end
  if arg.size > 1
    @v = arg.map{|e| e.to_i}
  end
end

Instance Attribute Details

#vObject

Returns the value of attribute v.



9
10
11
# File 'lib/rubyhelper/versionhelper.rb', line 9

def v
  @v
end

Class Method Details

.to_a(version) ⇒ Object

Return an array with each number of the version

Raises:

  • (ArgumentError)


70
71
72
73
# File 'lib/rubyhelper/versionhelper.rb', line 70

def self.to_a(version)
  raise ArgumentError unless version.is_a? Version
  return version.v.dup
end

.to_h(version) ⇒ Object

Not work

Raises:

  • (ArgumentError)


82
83
84
85
# File 'lib/rubyhelper/versionhelper.rb', line 82

def self.to_h(version)
  raise ArgumentError unless version.is_a? Version
  return nil
end

.to_i(version) ⇒ Object

Return an integer with each number of the version

Raises:

  • (ArgumentError)


88
89
90
91
92
93
# File 'lib/rubyhelper/versionhelper.rb', line 88

def self.to_i(version)
  raise ArgumentError unless version.is_a? Version
  i = 0
  version.v.each{|e| i = i * 10 + e}
  return i
end

.to_s(version) ⇒ Object

Return an string with each number of the version, joined by ‘.’

Raises:

  • (ArgumentError)


76
77
78
79
# File 'lib/rubyhelper/versionhelper.rb', line 76

def self.to_s(version)
  raise ArgumentError unless version.is_a? Version
  return version.v.join('.')
end

Instance Method Details

#decr!(n = -1)) ⇒ Object



49
50
51
# File 'lib/rubyhelper/versionhelper.rb', line 49

def decr!(n=-1)
  @v[n] = @v[n] -= 1
end

#incr!(n = -1)) ⇒ Object



45
46
47
# File 'lib/rubyhelper/versionhelper.rb', line 45

def incr!(n=-1)
  @v[n] = @v[n] += 1
end

#to_aObject



53
54
55
# File 'lib/rubyhelper/versionhelper.rb', line 53

def to_a
  return Version.to_a(self)
end

#to_hObject



61
62
63
# File 'lib/rubyhelper/versionhelper.rb', line 61

def to_h
  return Version.to_h(self)
end

#to_iObject



65
66
67
# File 'lib/rubyhelper/versionhelper.rb', line 65

def to_i
  return Version.to_i(self)
end

#to_sObject



57
58
59
# File 'lib/rubyhelper/versionhelper.rb', line 57

def to_s
  return Version.to_s(self)
end