Class: Puppet::Pops::Types::PSemVerRangeType
- Inherits:
-
PScalarType
- Object
- TypedModelObject
- PAnyType
- PScalarType
- Puppet::Pops::Types::PSemVerRangeType
- Defined in:
- lib/puppet/pops/types/p_sem_ver_range_type.rb
Overview
An unparameterized type that represents all VersionRange instances
Constant Summary collapse
- DEFAULT =
PSemVerRangeType.new
Class Method Summary collapse
-
.convert(version_range) ⇒ Semantic::VersionRange
Creates a Semantic::VersionRange from the given version_range argument.
-
.covered_by?(a, b) ⇒ Boolean
Checks if range a is a sub-range of (i.e. completely covered by) range b.
-
.include?(range, version) ⇒ Boolean
Check if a version is included in a version range.
-
.merge(a, b) ⇒ Semantic::VersionRange?
Merge two ranges so that the result matches all versions matched by both.
- .new_function(_, loader) ⇒ Object
- .register_ptype(loader, ir) ⇒ Object
Instance Method Summary collapse
Methods inherited from PAnyType
#==, #accept, #assignable?, #callable?, #callable_args?, #check_self_recursion, #generalize, #hash, #iterable?, #iterable_type, #kind_of_callable?, #name, #new_function, #normalize, #really_instance?, #resolve, #simple_name, simple_name, #to_alias_expanded_s, #to_s
Methods inherited from TypedModelObject
_ptype, create_ptype, register_ptypes
Methods included from Visitable
Methods included from PuppetObject
Class Method Details
.convert(version_range) ⇒ Semantic::VersionRange
Creates a Semantic::VersionRange from the given version_range argument. If the argument is ‘nil` or a Semantic::VersionRange, it is returned. If it is a String, it will be parsed into a Semantic::VersionRange. Any other class will raise an ArgumentError.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/puppet/pops/types/p_sem_ver_range_type.rb', line 43 def self.convert(version_range) case version_range when nil, Semantic::VersionRange version_range when String Semantic::VersionRange.parse(version_range) else raise ArgumentError, "Unable to convert a #{version_range.class.name} to a SemVerRange" end end |
.covered_by?(a, b) ⇒ Boolean
Checks if range a is a sub-range of (i.e. completely covered by) range b
59 60 61 |
# File 'lib/puppet/pops/types/p_sem_ver_range_type.rb', line 59 def self.covered_by?(a, b) b.begin <= a.begin && (b.end > a.end || b.end == a.end && (!b.exclude_end? || a.exclude_end?)) end |
.include?(range, version) ⇒ Boolean
Check if a version is included in a version range. The version can be a string or a ‘Semantic::SemVer`
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/puppet/pops/types/p_sem_ver_range_type.rb', line 20 def self.include?(range, version) case version when Semantic::Version range.include?(version) when String begin range.include?(Semantic::Version.parse(version)) rescue Semantic::Version::ValidationFailure false end else false end end |
.merge(a, b) ⇒ Semantic::VersionRange?
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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/puppet/pops/types/p_sem_ver_range_type.rb', line 71 def self.merge(a, b) if a.include?(b.begin) || b.include?(a.begin) max = [a.end, b.end].max exclude_end = false if a.exclude_end? exclude_end = max == a.end && (max > b.end || b.exclude_end?) elsif b.exclude_end? exclude_end = max == b.end && (max > a.end || a.exclude_end?) end Semantic::VersionRange.new([a.begin, b.begin].min, max, exclude_end) elsif a.exclude_end? && a.end == b.begin # Adjacent, a before b Semantic::VersionRange.new(a.begin, b.end, b.exclude_end?) elsif b.exclude_end? && b.end == a.begin # Adjacent, b before a Semantic::VersionRange.new(b.begin, a.end, a.exclude_end?) else # No overlap nil end end |
.new_function(_, loader) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/puppet/pops/types/p_sem_ver_range_type.rb', line 105 def self.new_function(_, loader) range_expr = "\\A#{range_pattern}\\Z" @@new_function ||= Puppet::Functions.create_loaded_function(:new_VersionRange, loader) do local_types do type 'SemVerRangeString = String[1]' type 'SemVerRangeHash = Struct[{min=>Variant[default,SemVer],Optional[max]=>Variant[default,SemVer],Optional[exclude_max]=>Boolean}]' end # Constructs a VersionRange from a String with a format specified by # # https://github.com/npm/node-semver#range-grammar # # The logical or || operator is not implemented since it effectively builds # an array of ranges that may be disparate. The {{Semantic::VersionRange}} inherits # from the standard ruby range. It must be possible to describe that range in terms # of min, max, and exclude max. # # The Puppet Version type is parameterized and accepts multiple ranges so creating such # constraints is still possible. It will just require several parameters rather than one # parameter containing the '||' operator. # dispatch :from_string do param 'SemVerRangeString', :str end # Constructs a VersionRange from a min, and a max version. The Boolean argument denotes # whether or not the max version is excluded or included in the range. It is included by # default. # dispatch :from_versions do param 'Variant[default,SemVer]', :min param 'Variant[default,SemVer]', :max optional_param 'Boolean', :exclude_max end # Same as #from_versions but each argument is instead given in a Hash # dispatch :from_hash do param 'SemVerRangeHash', :hash_args end def from_string(str) Semantic::VersionRange.parse(str) end def from_versions(min, max = :default, exclude_max = false) min = Semantic::Version::MIN if min == :default max = Semantic::Version::MAX if max == :default Semantic::VersionRange.new(min, max, exclude_max) end def from_hash(hash) from_versions(hash['min'], hash.fetch('max') { :default }, hash.fetch('exclude_max') { false }) end end end |
.register_ptype(loader, ir) ⇒ Object
8 9 10 |
# File 'lib/puppet/pops/types/p_sem_ver_range_type.rb', line 8 def self.register_ptype(loader, ir) create_ptype(loader, ir, 'ScalarType') end |
Instance Method Details
#eql?(o) ⇒ Boolean
97 98 99 |
# File 'lib/puppet/pops/types/p_sem_ver_range_type.rb', line 97 def eql?(o) self.class == o.class end |
#hash? ⇒ Boolean
101 102 103 |
# File 'lib/puppet/pops/types/p_sem_ver_range_type.rb', line 101 def hash? super ^ @version_range.hash end |
#instance?(o, guard = nil) ⇒ Boolean
93 94 95 |
# File 'lib/puppet/pops/types/p_sem_ver_range_type.rb', line 93 def instance?(o, guard = nil) o.is_a?(Semantic::VersionRange) end |