class Puppet::Pops::Types::PSemVerRangeType
An unparameterized type that represents all VersionRange instances
@api public
Constants
- DEFAULT
Public Class Methods
Creates a {SemanticPuppet::VersionRange} from the given version_range argument. If the argument is `nil` or a {SemanticPuppet::VersionRange}, it is returned. If it is a {String}, it will be parsed into a {SemanticPuppet::VersionRange}. Any other class will raise an {ArgumentError}.
@param version_range [SemanticPuppet::VersionRange,String,nil] the version range to convert @return [SemanticPuppet::VersionRange] the converted version range @raise [ArgumentError] when the argument cannot be converted into a version range
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 44 def self.convert(version_range) 45 case version_range 46 when nil, SemanticPuppet::VersionRange 47 version_range 48 when String 49 SemanticPuppet::VersionRange.parse(version_range) 50 else 51 raise ArgumentError, "Unable to convert a #{version_range.class.name} to a SemVerRange" 52 end 53 end
Checks if range a is a sub-range of (i.e. completely covered by) range b @param a [SemanticPuppet::VersionRange] the first range @param b [SemanticPuppet::VersionRange] the second range
@return [Boolean] `true` if a is completely covered by b
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 60 def self.covered_by?(a, b) 61 b.begin <= a.begin && (b.end > a.end || b.end == a.end && (!b.exclude_end? || a.exclude_end?)) 62 end
Check if a version is included in a version range. The version can be a string or a `SemanticPuppet::SemVer`
@param range [SemanticPuppet::VersionRange] the range to match against @param version [SemanticPuppet::Version,String] the version to match @return [Boolean] `true` if the range includes the given version
@api public
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 21 def self.include?(range, version) 22 case version 23 when SemanticPuppet::Version 24 range.include?(version) 25 when String 26 begin 27 range.include?(SemanticPuppet::Version.parse(version)) 28 rescue SemanticPuppet::Version::ValidationFailure 29 false 30 end 31 else 32 false 33 end 34 end
Merge two ranges so that the result matches all versions matched by both. A merge is only possible when the ranges are either adjacent or have an overlap.
@param a [SemanticPuppet::VersionRange] the first range @param b [SemanticPuppet::VersionRange] the second range @return [SemanticPuppet::VersionRange,nil] the result of the merge
@api public
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 72 def self.merge(a, b) 73 if a.include?(b.begin) || b.include?(a.begin) 74 max = [a.end, b.end].max 75 exclude_end = false 76 if a.exclude_end? 77 exclude_end = max == a.end && (max > b.end || b.exclude_end?) 78 elsif b.exclude_end? 79 exclude_end = max == b.end && (max > a.end || a.exclude_end?) 80 end 81 SemanticPuppet::VersionRange.new([a.begin, b.begin].min, max, exclude_end) 82 elsif a.exclude_end? && a.end == b.begin 83 # Adjacent, a before b 84 SemanticPuppet::VersionRange.new(a.begin, b.end, b.exclude_end?) 85 elsif b.exclude_end? && b.end == a.begin 86 # Adjacent, b before a 87 SemanticPuppet::VersionRange.new(b.begin, a.end, a.exclude_end?) 88 else 89 # No overlap 90 nil 91 end 92 end
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 110 def self.new_function(type) 111 @new_function ||= Puppet::Functions.create_loaded_function(:new_VersionRange, type.loader) do 112 local_types do 113 type 'SemVerRangeString = String[1]' 114 type 'SemVerRangeHash = Struct[{min=>Variant[Default,SemVer],Optional[max]=>Variant[Default,SemVer],Optional[exclude_max]=>Boolean}]' 115 end 116 117 # Constructs a VersionRange from a String with a format specified by 118 # 119 # https://github.com/npm/node-semver#range-grammar 120 # 121 # The logical or || operator is not implemented since it effectively builds 122 # an array of ranges that may be disparate. The {{SemanticPuppet::VersionRange}} inherits 123 # from the standard ruby range. It must be possible to describe that range in terms 124 # of min, max, and exclude max. 125 # 126 # The Puppet Version type is parameterized and accepts multiple ranges so creating such 127 # constraints is still possible. It will just require several parameters rather than one 128 # parameter containing the '||' operator. 129 # 130 dispatch :from_string do 131 param 'SemVerRangeString', :str 132 end 133 134 # Constructs a VersionRange from a min, and a max version. The Boolean argument denotes 135 # whether or not the max version is excluded or included in the range. It is included by 136 # default. 137 # 138 dispatch :from_versions do 139 param 'Variant[Default,SemVer]', :min 140 param 'Variant[Default,SemVer]', :max 141 optional_param 'Boolean', :exclude_max 142 end 143 144 # Same as #from_versions but each argument is instead given in a Hash 145 # 146 dispatch :from_hash do 147 param 'SemVerRangeHash', :hash_args 148 end 149 150 def from_string(str) 151 SemanticPuppet::VersionRange.parse(str) 152 end 153 154 def from_versions(min, max = :default, exclude_max = false) 155 min = SemanticPuppet::Version::MIN if min == :default 156 max = SemanticPuppet::Version::MAX if max == :default 157 SemanticPuppet::VersionRange.new(min, max, exclude_max) 158 end 159 160 def from_hash(hash) 161 from_versions(hash['min'], hash.fetch('max') { :default }, hash.fetch('exclude_max') { false }) 162 end 163 end 164 end
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 9 def self.register_ptype(loader, ir) 10 create_ptype(loader, ir, 'AnyType') 11 end
Private Class Methods
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 174 def self.range_pattern 175 part = '(?<part>[0-9A-Za-z-]+)' 176 parts = "(?<parts>#{part}(?:\\.\\g<part>)*)" 177 178 qualifier = "(?:-#{parts})?(?:\\+\\g<parts>)?" 179 180 xr = '(?<xr>[xX*]|0|[1-9][0-9]*)' 181 partial = "(?<partial>#{xr}(?:\\.\\g<xr>(?:\\.\\g<xr>#{qualifier})?)?)" 182 183 hyphen = "(?:#{partial}\\s+-\\s+\\g<partial>)" 184 simple = "(?<simple>(?:<|>|>=|<=|~|\\^)?\\g<partial>)" 185 186 "#{hyphen}|#{simple}(?:\\s+\\g<simple>)*" 187 end
Public Instance Methods
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 102 def eql?(o) 103 self.class == o.class 104 end
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 160 def from_hash(hash) 161 from_versions(hash['min'], hash.fetch('max') { :default }, hash.fetch('exclude_max') { false }) 162 end
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 150 def from_string(str) 151 SemanticPuppet::VersionRange.parse(str) 152 end
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 154 def from_versions(min, max = :default, exclude_max = false) 155 min = SemanticPuppet::Version::MIN if min == :default 156 max = SemanticPuppet::Version::MAX if max == :default 157 SemanticPuppet::VersionRange.new(min, max, exclude_max) 158 end
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 106 def hash? 107 super ^ @version_range.hash 108 end
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 98 def instance?(o, guard = nil) 99 o.is_a?(SemanticPuppet::VersionRange) 100 end
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 94 def roundtrip_with_string? 95 true 96 end
Protected Instance Methods
# File lib/puppet/pops/types/p_sem_ver_range_type.rb 170 def _assignable?(o, guard) 171 self == o 172 end