class YAVM::Version

Constants

VERSION_REGEX

Attributes

store[RW]

Public Class Methods

new(store, vobject = nil) click to toggle source
# File lib/yavm/version.rb, line 12
def initialize(store, vobject = nil)
  self.store = store

  case vobject
  when String
    parse(vobject)
  when Hash
    load(vobject)
  when nil
    empty
  end
end

Public Instance Methods

==(other) click to toggle source

rubocop:disable Style/RedundantSelf

# File lib/yavm/version.rb, line 59
def ==(other)
  self.major == other.major &&
    self.minor == other.minor &&
    self.patch == other.patch &&
    self.special == other.special
end
format(string = '') click to toggle source
# File lib/yavm/version.rb, line 45
def format(string = '')
  string.gsub(/(%-?[Mmpst%])/, {
    '%M' => major.to_s,
    '%m' => minor.to_s,
    '%p' => patch.to_s,
    '%s' => special.to_s,
    '%t' => meta.to_s,
    '%-s' => special.to_s.empty? ? '' : "-#{special}",
    '%-t' => meta.to_s.empty? ? '' : "+#{meta}",
    '%%' => '%'
  })
end
increment(what) click to toggle source
# File lib/yavm/version.rb, line 66
def increment(what)
  case what
  when :major
    self.major += 1
  when :minor
    self.minor += 1
  when :patch
    self.patch += 1
  else
    fail "Can't increment #{what}"
  end
end
major=(value) click to toggle source
# File lib/yavm/version.rb, line 79
def major=(value)
  clears(:minor, :patch, :special, :meta)
  @_version.major = value
end
minor=(value) click to toggle source
# File lib/yavm/version.rb, line 84
def minor=(value)
  clears(:patch, :special, :meta)
  @_version.minor = value
end
patch=(value) click to toggle source
# File lib/yavm/version.rb, line 89
def patch=(value)
  @_version.patch = value
end
tag() click to toggle source
# File lib/yavm/version.rb, line 41
def tag
  format('v%M.%m.%p%-s%-t')
end
to_hash() click to toggle source
# File lib/yavm/version.rb, line 29
def to_hash
  @_version.marshal_dump
end
to_json() click to toggle source
# File lib/yavm/version.rb, line 37
def to_json
  to_hash.to_json
end
to_s() click to toggle source
# File lib/yavm/version.rb, line 25
def to_s
  format('%M.%m.%p%-s')
end
to_yaml() click to toggle source
# File lib/yavm/version.rb, line 33
def to_yaml
  to_hash.to_yaml
end

Private Instance Methods

clears(*properties) click to toggle source
# File lib/yavm/version.rb, line 95
def clears(*properties)
  properties.each do |p|
    value = @_version.send(p).is_a?(Fixnum) ? 0 : ''
    @_version.send("#{p}=", value)
  end
end
empty() click to toggle source
# File lib/yavm/version.rb, line 102
def empty
  @_version = OpenStruct.new ({
    major: 0, minor: 0, patch: 0, special: '', meta: ''
  })
end
integerize!() click to toggle source
# File lib/yavm/version.rb, line 123
def integerize!
  @_version.major = @_version.major.to_i
  @_version.minor = @_version.minor.to_i
  @_version.patch = @_version.patch.to_i
end
load(hash) click to toggle source
# File lib/yavm/version.rb, line 117
def load(hash)
  @_version = OpenStruct.new(hash)
  integerize!
  stringify!
end
parse(string) click to toggle source
# File lib/yavm/version.rb, line 108
def parse(string)
  match = string.match(VERSION_REGEX)

  @_version = Hash[match.names.zip(match.captures)]
  @_version = OpenStruct.new(@_version)
  integerize!
  stringify!
end
stringify!() click to toggle source
# File lib/yavm/version.rb, line 129
def stringify!
  @_version.special ||= ''
  @_version.meta ||= ''
end