class Puppet::Pops::Types::PSemVerRangeType

An unparameterized type that represents all VersionRange instances

@api public

Constants

DEFAULT

Public Class Methods

convert(version_range) click to toggle source

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
43 def self.convert(version_range)
44   case version_range
45   when nil, SemanticPuppet::VersionRange
46     version_range
47   when String
48     SemanticPuppet::VersionRange.parse(version_range)
49   else
50     raise ArgumentError, "Unable to convert a #{version_range.class.name} to a SemVerRange"
51   end
52 end
covered_by?(a, b) click to toggle source

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
59 def self.covered_by?(a, b)
60   b.begin <= a.begin && (b.end > a.end || b.end == a.end && (!b.exclude_end? || a.exclude_end?))
61 end
include?(range, version) click to toggle source

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
20 def self.include?(range, version)
21   case version
22   when SemanticPuppet::Version
23     range.include?(version)
24   when String
25     begin
26       range.include?(SemanticPuppet::Version.parse(version))
27     rescue SemanticPuppet::Version::ValidationFailure
28       false
29     end
30   else
31     false
32   end
33 end
merge(a, b) click to toggle source

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
71 def self.merge(a, b)
72   if a.include?(b.begin) || b.include?(a.begin)
73     max = [a.end, b.end].max
74     exclude_end = false
75     if a.exclude_end?
76       exclude_end = max == a.end && (max > b.end || b.exclude_end?)
77     elsif b.exclude_end?
78       exclude_end = max == b.end && (max > a.end || a.exclude_end?)
79     end
80     SemanticPuppet::VersionRange.new([a.begin, b.begin].min, max, exclude_end)
81   elsif a.exclude_end? && a.end == b.begin
82     # Adjacent, a before b
83     SemanticPuppet::VersionRange.new(a.begin, b.end, b.exclude_end?)
84   elsif b.exclude_end? && b.end == a.begin
85     # Adjacent, b before a
86     SemanticPuppet::VersionRange.new(b.begin, a.end, a.exclude_end?)
87   else
88     # No overlap
89     nil
90   end
91 end
new_function(type) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_range_type.rb
109 def self.new_function(type)
110   @new_function ||= Puppet::Functions.create_loaded_function(:new_VersionRange, type.loader) do
111     local_types do
112       type 'SemVerRangeString = String[1]'
113       type 'SemVerRangeHash = Struct[{min=>Variant[Default,SemVer],Optional[max]=>Variant[Default,SemVer],Optional[exclude_max]=>Boolean}]'
114     end
115 
116     # Constructs a VersionRange from a String with a format specified by
117     #
118     # https://github.com/npm/node-semver#range-grammar
119     #
120     # The logical or || operator is not implemented since it effectively builds
121     # an array of ranges that may be disparate. The {{SemanticPuppet::VersionRange}} inherits
122     # from the standard ruby range. It must be possible to describe that range in terms
123     # of min, max, and exclude max.
124     #
125     # The Puppet Version type is parameterized and accepts multiple ranges so creating such
126     # constraints is still possible. It will just require several parameters rather than one
127     # parameter containing the '||' operator.
128     #
129     dispatch :from_string do
130       param 'SemVerRangeString', :str
131     end
132 
133     # Constructs a VersionRange from a min, and a max version. The Boolean argument denotes
134     # whether or not the max version is excluded or included in the range. It is included by
135     # default.
136     #
137     dispatch :from_versions do
138       param 'Variant[Default,SemVer]', :min
139       param 'Variant[Default,SemVer]', :max
140       optional_param 'Boolean', :exclude_max
141     end
142 
143     # Same as #from_versions but each argument is instead given in a Hash
144     #
145     dispatch :from_hash do
146       param 'SemVerRangeHash', :hash_args
147     end
148 
149     def from_string(str)
150       SemanticPuppet::VersionRange.parse(str)
151     end
152 
153     def from_versions(min, max = :default, exclude_max = false)
154       min = SemanticPuppet::Version::MIN if min == :default
155       max = SemanticPuppet::Version::MAX if max == :default
156       SemanticPuppet::VersionRange.new(min, max, exclude_max)
157     end
158 
159     def from_hash(hash)
160       from_versions(hash['min'], hash.fetch('max') { :default }, hash.fetch('exclude_max') { false })
161     end
162   end
163 end
register_ptype(loader, ir) click to toggle source
   # File lib/puppet/pops/types/p_sem_ver_range_type.rb
 8 def self.register_ptype(loader, ir)
 9   create_ptype(loader, ir, 'AnyType')
10 end

Private Class Methods

range_pattern() click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_range_type.rb
173 def self.range_pattern
174   part = '(?<part>[0-9A-Za-z-]+)'
175   parts = "(?<parts>#{part}(?:\\.\\g<part>)*)"
176 
177   qualifier = "(?:-#{parts})?(?:\\+\\g<parts>)?"
178 
179   xr = '(?<xr>[xX*]|0|[1-9][0-9]*)'
180   partial = "(?<partial>#{xr}(?:\\.\\g<xr>(?:\\.\\g<xr>#{qualifier})?)?)"
181 
182   hyphen = "(?:#{partial}\\s+-\\s+\\g<partial>)"
183   simple = "(?<simple>(?:<|>|>=|<=|~|\\^)?\\g<partial>)"
184 
185   "#{hyphen}|#{simple}(?:\\s+\\g<simple>)*"
186 end

Public Instance Methods

eql?(o) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_range_type.rb
101 def eql?(o)
102   self.class == o.class
103 end
from_hash(hash) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_range_type.rb
159 def from_hash(hash)
160   from_versions(hash['min'], hash.fetch('max') { :default }, hash.fetch('exclude_max') { false })
161 end
from_string(str) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_range_type.rb
149 def from_string(str)
150   SemanticPuppet::VersionRange.parse(str)
151 end
from_versions(min, max = :default, exclude_max = false) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_range_type.rb
153 def from_versions(min, max = :default, exclude_max = false)
154   min = SemanticPuppet::Version::MIN if min == :default
155   max = SemanticPuppet::Version::MAX if max == :default
156   SemanticPuppet::VersionRange.new(min, max, exclude_max)
157 end
hash?() click to toggle source
Calls superclass method
    # File lib/puppet/pops/types/p_sem_ver_range_type.rb
105 def hash?
106   super ^ @version_range.hash
107 end
instance?(o, guard = nil) click to toggle source
   # File lib/puppet/pops/types/p_sem_ver_range_type.rb
97 def instance?(o, guard = nil)
98   o.is_a?(SemanticPuppet::VersionRange)
99 end
roundtrip_with_string?() click to toggle source
   # File lib/puppet/pops/types/p_sem_ver_range_type.rb
93 def roundtrip_with_string?
94   true
95 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_range_type.rb
169 def _assignable?(o, guard)
170   self == o
171 end