class Puppet::Pops::Types::PSemVerType
A Puppet Language Type that exposes the {{SemanticPuppet::Version}} and {{SemanticPuppet::VersionRange}}. The version type is parameterized with version ranges.
@api public
Constants
- DEFAULT
Attributes
ranges[R]
Public Class Methods
convert(version)
click to toggle source
Creates a SemVer version from the given version argument. If the argument is `nil` or a {SemanticPuppet::Version}, it is returned. If it is a {String}, it will be parsed into a {SemanticPuppet::Version}. Any other class will raise an {ArgumentError}.
@param version [SemanticPuppet::Version,String,nil] the version to convert @return [SemanticPuppet::Version] the converted version @raise [ArgumentError] when the argument cannot be converted into a version
# File lib/puppet/pops/types/p_sem_ver_type.rb 47 def self.convert(version) 48 case version 49 when nil, SemanticPuppet::Version 50 version 51 when String 52 SemanticPuppet::Version.parse(version) 53 else 54 raise ArgumentError, "Unable to convert a #{version.class.name} to a SemVer" 55 end 56 end
new(ranges)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 21 def initialize(ranges) 22 ranges = ranges.map { |range| range.is_a?(SemanticPuppet::VersionRange) ? range : SemanticPuppet::VersionRange.parse(range) } 23 ranges = merge_ranges(ranges) if ranges.size > 1 24 @ranges = ranges 25 end
new_function(type)
click to toggle source
@api private
# File lib/puppet/pops/types/p_sem_ver_type.rb 59 def self.new_function(type) 60 @new_function ||= Puppet::Functions.create_loaded_function(:new_Version, type.loader) do 61 local_types do 62 type 'PositiveInteger = Integer[0,default]' 63 type 'SemVerQualifier = Pattern[/\A(?<part>[0-9A-Za-z-]+)(?:\.\g<part>)*\Z/]' 64 type "SemVerPattern = Pattern[/\\A#{SemanticPuppet::Version::REGEX_FULL}\\Z/]" 65 type 'SemVerHash = Struct[{major=>PositiveInteger,minor=>PositiveInteger,patch=>PositiveInteger,Optional[prerelease]=>SemVerQualifier,Optional[build]=>SemVerQualifier}]' 66 end 67 68 # Creates a SemVer from a string as specified by http://semver.org/ 69 # 70 dispatch :from_string do 71 param 'SemVerPattern', :str 72 end 73 74 # Creates a SemVer from integers, prerelease, and build arguments 75 # 76 dispatch :from_args do 77 param 'PositiveInteger', :major 78 param 'PositiveInteger', :minor 79 param 'PositiveInteger', :patch 80 optional_param 'SemVerQualifier', :prerelease 81 optional_param 'SemVerQualifier', :build 82 end 83 84 # Same as #from_args but each argument is instead given in a Hash 85 # 86 dispatch :from_hash do 87 param 'SemVerHash', :hash_args 88 end 89 90 argument_mismatch :on_error do 91 param 'String', :str 92 end 93 94 def from_string(str) 95 SemanticPuppet::Version.parse(str) 96 end 97 98 def from_args(major, minor, patch, prerelease = nil, build = nil) 99 SemanticPuppet::Version.new(major, minor, patch, to_array(prerelease), to_array(build)) 100 end 101 102 def from_hash(hash) 103 SemanticPuppet::Version.new(hash['major'], hash['minor'], hash['patch'], to_array(hash['prerelease']), to_array(hash['build'])) 104 end 105 106 def on_error(str) 107 _("The string '%{str}' cannot be converted to a SemVer") % { str: str } 108 end 109 110 private 111 112 def to_array(component) 113 component ? [component] : nil 114 end 115 end 116 end
register_ptype(loader, ir)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 10 def self.register_ptype(loader, ir) 11 create_ptype(loader, ir, 'ScalarType', 12 'ranges' => { 13 KEY_TYPE => PArrayType.new(PVariantType.new([PSemVerRangeType::DEFAULT,PStringType::NON_EMPTY])), 14 KEY_VALUE => [] 15 } 16 ) 17 end
Public Instance Methods
eql?(o)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 31 def eql?(o) 32 self.class == o.class && @ranges == o.ranges 33 end
from_args(major, minor, patch, prerelease = nil, build = nil)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 98 def from_args(major, minor, patch, prerelease = nil, build = nil) 99 SemanticPuppet::Version.new(major, minor, patch, to_array(prerelease), to_array(build)) 100 end
from_hash(hash)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 102 def from_hash(hash) 103 SemanticPuppet::Version.new(hash['major'], hash['minor'], hash['patch'], to_array(hash['prerelease']), to_array(hash['build'])) 104 end
from_string(str)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 94 def from_string(str) 95 SemanticPuppet::Version.parse(str) 96 end
hash?()
click to toggle source
Calls superclass method
# File lib/puppet/pops/types/p_sem_ver_type.rb 35 def hash? 36 super ^ @ranges.hash 37 end
instance?(o, guard = nil)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 27 def instance?(o, guard = nil) 28 o.is_a?(SemanticPuppet::Version) && (@ranges.empty? || @ranges.any? {|range| range.include?(o) }) 29 end
on_error(str)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 106 def on_error(str) 107 _("The string '%{str}' cannot be converted to a SemVer") % { str: str } 108 end
to_array(component)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 112 def to_array(component) 113 component ? [component] : nil 114 end
Protected Instance Methods
_assignable?(o, guard)
click to toggle source
# File lib/puppet/pops/types/p_sem_ver_type.rb 122 def _assignable?(o, guard) 123 return false unless o.class == self.class 124 return true if @ranges.empty? 125 return false if o.ranges.empty? 126 127 # All ranges in o must be covered by at least one range in self 128 o.ranges.all? do |o_range| 129 @ranges.any? do |range| 130 PSemVerRangeType.covered_by?(o_range, range) 131 end 132 end 133 end
merge_ranges(ranges)
click to toggle source
@api private
# File lib/puppet/pops/types/p_sem_ver_type.rb 136 def merge_ranges(ranges) 137 result = [] 138 until ranges.empty? 139 unmerged = [] 140 x = ranges.pop 141 result << ranges.inject(x) do |memo, y| 142 merged = PSemVerRangeType.merge(memo, y) 143 if merged.nil? 144 unmerged << y 145 else 146 memo = merged 147 end 148 memo 149 end 150 ranges = unmerged 151 end 152 result.reverse! 153 end