Class: MyPrecious::PyPackageInfo::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/myprecious/python_packages.rb

Overview

Represents a full PEP-440 version

Constant Summary collapse

NOT_PRE =
['z', 0]

Instance Method Summary collapse

Constructor Details

#initialize(final, epoch: 0, pre: [], post: nil, dev: nil, local: nil) ⇒ Version

Returns a new instance of Version.



596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/myprecious/python_packages.rb', line 596

def initialize(final, epoch: 0, pre: [], post: nil, dev: nil, local: nil)
  @epoch = (epoch || 0).to_i
  @final = final.kind_of?(FinalVersion) ? final : FinalVersion.new(final)
  @pre = normalize_part(pre[1]) {|n| n && [pre[0], n]}
  @post = normalize_part(post) {|n| n && [n] }
  @dev = normalize_part(dev) {|n| n}
  @local = case local
  when nil then nil
  when Array then local
  else local.to_s.split(/[._-]/).map {|part| try_to_i(part)}
  end
end

Instance Method Details

#<=>(rhs) ⇒ Object



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/myprecious/python_packages.rb', line 633

def <=>(rhs)
  return nil unless rhs.kind_of?(self.class)
  steps = Enumerator.new do |comps|
    %i[epoch final pre_comp post_comp dev_comp].each do |attr|
      comps << (send(attr) <=> rhs.send(attr))
    end
    
    case [local, rhs.local].count(&:nil?)
    when 2 then comps << 0
    when 1 then comps << (local.nil? ? -1 : 1)
    else comps << (local <=> rhs.local)
    end
  end
  steps.find {|v| v != 0} || 0
end

#inspectObject



610
611
612
# File 'lib/myprecious/python_packages.rb', line 610

def inspect
  "#<#{self.class.name} #{to_s.inspect}>"
end

#pre_groupObject



625
626
627
# File 'lib/myprecious/python_packages.rb', line 625

def pre_group
  @pre && @pre[0]
end

#pre_numObject



629
630
631
# File 'lib/myprecious/python_packages.rb', line 629

def pre_num
  @pre && @pre[1]
end

#prerelease?Boolean

Returns:

  • (Boolean)


650
651
652
# File 'lib/myprecious/python_packages.rb', line 650

def prerelease?
  !!(@pre || @dev)
end

#to_sObject



614
615
616
617
618
619
620
621
622
623
# File 'lib/myprecious/python_packages.rb', line 614

def to_s
  [].tap do |parts|
    parts << "#{epoch}!" unless epoch == 0
    parts << final.to_s
    parts << "#{@pre[0]}#{@pre[1]}" if @pre
    parts << ".post#{@post}" if @post
    parts << ".dev#{@dev}" if @dev
    parts << "+#{local}" if local
  end.join('')
end